Saturday, November 19, 2011

Internationalize Haml-Templates with haml-i18n

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

  %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_name if the template-scoped translation doesn’t exist.
If none of these keys exist, the haml template just acts as usual: it renders 'User name'.

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.

17 comments:

  1. Is t('.user_name') not short enough for you?

    ReplyDelete
  2. If 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').

    ReplyDelete
  3. what happens in this case :

    %p Example for a text really long here...

    ReplyDelete
  4. 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
  5. @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

    %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.

    ReplyDelete
  6. Your suggestions sounds good to me.

    Does 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.

    ReplyDelete
  7. 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.

    Well, 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 ;)

    ReplyDelete
  8. Just wondering if there will be performance issues if a I18n. lookup is always tried when strings are found, even in non localized templates...

    ReplyDelete
  9. Ooops, yeah, forget that little detail about haml not beeing in the core :)
    But this would obviously be useful for erb too.

    ReplyDelete
  10. About the performance issues: Yes, if you have a lot of non-i18n strings there will be unnecessary lookups for all of them.

    However, I think in internationalized applications you usually (should) end up using only i18n strings.

    ReplyDelete
  11. If the lookup would be done after haml/erb template evaluation the gem could be used with erb too.

    ReplyDelete
  12. Wär auch gut wenn das gem für strings in function calls funktionieren würde, z.B.:

    = some_ruby_function user_name: User name:

    ReplyDelete
  13. Oops, writing in German here accidentally ;-)

    Would be good if this also worked for function calls like:
    = some_ruby_function user_name: User name

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Check out https://github.com/shaiguitar/haml-i18n-extractor .

    It 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.

    ReplyDelete

 
DreamHost coupon code