Ruby on ZiGzAg About GitHub LinkedIn Twitter RSS

Get your local Rails application ready for I18N

17 Dec 2009 – Beijing

Think of the following scenario:
You have created your lovely Rails application and managed to get it real. After a period of time it become more and more popular. People around the world love your application. Then somebody start to ask wether you can provide an version in their language. Some active users even made a commitment that they will contribute the translation to their language.

Pretty good news yet, but when you look back to you application code, you already have over hundreds ERB pages with many hard-code text being spread in HTML and ERB tags…
Although Rails > 2.2 provide a handy way of doing I18N by the I18n.translate( t method in helper), Found out all the text need to transfer is a tedious and error-prone task.

An I18N Textmate Bundle written by Sven Fuchs may do you a favor when extracting those text. But you still need to find every explicit string and name the keys of them manually.

Could we make an automatic tool for this boring task? Since if the hard-coded text is written in English, we can conventionally name the key with the same name. (just lowercase it and connected with underline other than space). And the texts need to be translated in ERB seem to have some common attributes: they either exist in the HTML text node or in some helper methods like link_to, label_tag, etc…

Based on the above assumption, My colleague Rainy pair-programming with me the following tool called ready_for_i18n . and release it at Gemcutter. After install it by
gem install ready_for_i18n
just run:
ready_for_i18n <you view path> <target path>

Then the tool will transform your view like the screenshot #1(Diff result) and also generate a dictionary en.yml file looked like screenshot #2.
Have the similar I18N problem? Check it out and any of your comments are welcome here and also on the project home at github .

Screenshot #1: Diff result of the transform result with the original view file

Screenshot #1

Screenshot #2 Auto generated en.yml

Screenshot #2


Fork me on GitHub