I love Haml for making my html templates more readable. Unfortunately this comes to an end when I use internationalization. I end up with markup like
To overcome this issue I created haml-i18n. Using haml-i18n instead of the above code you can just write
Haml-i18n will try to look up the I18n key
%p= I18n.translate('.user_name')
To overcome this issue I created haml-i18n. Using haml-i18n instead of the above code you can just write
%p User name
Haml-i18n will try to look up the I18n key
[view_folder].[template_name].user_name- or
user_nameif the template-scoped translation doesn’t exist.
I just created haml-i18n yesterday to fit one project’s needs, therefore it is still very basic. If you have any suggestions on how to improve it, please leave a comment.

Great idea!
ReplyDeleteIs t('.user_name') not short enough for you?
ReplyDeleteIf you just count the characters there’s not much of a difference. However, if you imagine a real-life haml-template with dozens of = t('.my_key') there is a significant advantage in readability of "My key" over = t('.my_key').
ReplyDeletewhat happens in this case :
ReplyDelete%p Example for a text really long here...
Okay, yeah, its a little nicer, but a gem seems somewhat overkill to me. And what if you have interpolations in your translated strings? How do you supply params with your gem? You can try out the Ruby Mine IDE, it automatically replaces t(..) calls with actual translations.
ReplyDelete@Nico: Personally I’m not into IDEs, i prefer simple editors. You are right, the functionality is still basic. For interpolations I thought of implementing a syntax like
ReplyDelete%p #{@items.count} items
instead of
%p= t '.items', count: @items.count
And for other interpolations than "count" maybe
%p Hello #{name: @user.name}
instead of
%p= t '.hello', name: @user.name
What do you think?
@Anonymous: It would try to retrieve the key '.example_for_a_text_really_long_here'. But normally I guess you wouldn’t want to use long texts as i18n keys.
Your suggestions sounds good to me.
ReplyDeleteDoes it also work for other cases, for example text in tag attributes?
Like:
%p{ title: User name }
If your gem proofs to work well, you could try to get it merged into the rails core.
No, but also a good idea! Although I think the interpolation might be a more frequent use case, so probably I will focus on it first.
ReplyDeleteWell, I would really appreciate that, but I doubt that they’d merge it into the rails core since not even haml is part of it ;)
Just wondering if there will be performance issues if a I18n. lookup is always tried when strings are found, even in non localized templates...
ReplyDeleteOoops, yeah, forget that little detail about haml not beeing in the core :)
ReplyDeleteBut this would obviously be useful for erb too.
About the performance issues: Yes, if you have a lot of non-i18n strings there will be unnecessary lookups for all of them.
ReplyDeleteHowever, I think in internationalized applications you usually (should) end up using only i18n strings.
If the lookup would be done after haml/erb template evaluation the gem could be used with erb too.
ReplyDeleteWär auch gut wenn das gem für strings in function calls funktionieren würde, z.B.:
ReplyDelete= some_ruby_function user_name: User name:
Oops, writing in German here accidentally ;-)
ReplyDeleteWould be good if this also worked for function calls like:
= some_ruby_function user_name: User name
This comment has been removed by the author.
ReplyDeletehttps://github.com/shaiguitar/haml-i18n-extractor
DeleteCheck out https://github.com/shaiguitar/haml-i18n-extractor .
ReplyDeleteIt is for dealing with template string extraction + yaml files in an easy automatic & fast way. It's WIP at the time of this comment, but it works!
See repo for more info.