New Rails 2.2 i18n defaults

Posted by David November 18, 2008 @ 01:44 PM

I just reduced the housework needed to setup a new Rails application with i18n. All new applications will ship with a config/locales directory that’s automatically wired up in the load path for i18n. So you can just drop .yml or .rb locale files in there and they’ll be instantly available for translation.

There’s also a sample config/locales/en.yml file in there to give you a starting point. In addition, the initializer is now wired up through the Rails config. The new default environment.rb provides these pointers:

# The internationalization framework can be changed 
# to have another default locale (standard is :en) or more load paths.
# All files from config/locales/*.rb,yml are added automatically.
# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de

So on a fresh Rails 2.2 application, you’ll be able to do see it all wired up out of the box (the :hello key is from the config/locales/en.yml demo file):

$ ./script/console
>> I18n.t :hello
=> "Hello world"

Rails 2.2 final is just around the corner. We’ve been ironing out the last bugs and added the last amount of polish to make this a kick ass release. Also, work on 2.3 / 3.0 has already begun in master as well since we’ve branched for 2.2 a while back.

Posted in Documentation | 38 comments

Comments

  1. Attila Györffy on 18 Nov 13:50:

    I will definitely digg deeper into the i18n features of Rails. This comes really handy since I’m working on a multilingual project atm.

    Also, I’m looking forward to see major feature plans for v3.0…

    Thanks for your time and effort. o/

  2. oelmekki on 18 Nov 13:53:

    For the french that I am, this is a really good news.

    Thanks a lot.

  3. karmi on 18 Nov 13:58:

    Wonderful! Thanks!

  4. Andrea on 18 Nov 14:00:

    Oh, that is great! Thanks!

  5. Neil on 18 Nov 14:27:

    I’m really happy about localization being built in !

  6. Thomas on 18 Nov 14:28:

    I18n.t :thanks_this_is_good_news => “Danke, das sind gute Neuigkeiten!”

  7. Alex on 18 Nov 14:32:

    Is there any further hierarchization planned than one file per language, like in, e.g. SimpleLocalization? In my app, I currently have dozens of files per language including a bunch of yaml hierarchy levels inside to have things a lil structured.

  8. Donald Piret on 18 Nov 14:44:

    Is there a way to upgrade rails 2.1 application structures to get these extra folders and config files?

  9. dejan.dimic@gmail.com on 18 Nov 14:55:

    For all of us with not English speaking customers this will be an easy and great step forward.

  10. DHH on 18 Nov 15:46:

    Alex, you can always just add more load paths. Now it’s even easier with config.i18n.load_path exposed in environment.rb.

    Donald, you just add config/locales and upgrade to Rails 2.2 and you’ll get this stuff for free.

  11. Aldrin Lenny on 18 Nov 17:39:

    When we upgrade our application www.realtyitch.com to rails 2.2, it picks up plugin models Rateable ( acts_as_rateable ) and Comments ( acts_as_comments ) just fine in ‘Production’ mode, but gives error ‘uninitialized constant’ in ‘Development’ mode. Why is this difference in behavior? FYI: Our LIVE site is configured to use 2.0.x release and we don’t have any problem in our Development mode with these plugin models. Any help is much appreciated. thx in advance Aldrin

  12. oboxodo on 18 Nov 18:14:

    config.i18n.default_locale = :es

    I18n.t :thanks_this_is_good_news => “¡Gracias! Estas son buenas noticias”

  13. jblanche on 18 Nov 21:37:

    config.i18n.default_locale = :fr

    I18n.t :thanks_this_is_good_news => “Merci ! C’est une bonne nouvelle.”

  14. oldmoe on 18 Nov 22:25:

    config.i18n.default_locale = :ar

    I18n.t :thanks_this_is_good_news => “شكراً ، هذه أخبار جيدة”

  15. lolcatz on 19 Nov 05:36:

    Thanks!

    It would be really nice to be able to change I18n.default_backend in environment.rb

  16. DHH on 19 Nov 08:19:

    lolcatz, you can change all the I18n settings through environment.rb. config.i18n.whatever is a mapping for I18n.whatever.

  17. Sven Fuchs on 19 Nov 23:38:

    Very cool, David!

    Also, lemme add that there’s a ton of useful stuff on http://rails-i18n.org/wiki already. Globalize2 (http://github.com/joshmh/globalize2) comes with some pretty useful features, too, like model translations, locale fallbacks, translation metadata etc.

  18. lolcatz on 20 Nov 07:05:

    Holy cow, this is amazing! Thanks a ton!

  19. good on 20 Nov 09:25:

    config.i18n.default_locale = :zh_CN

    I18n.t :thanks_this_is_good_news => “泄啦;这是一个好消息”

  20. Mange on 20 Nov 11:40:

    config.i18n.default_locale = :sv

    I18n.t :thanks_this_is_good_news => “Tack! Detta är goda nyheter.”

  21. Harry Seldon on 20 Nov 13:03:

    Great i18n !

    By the way, is there a way to view on this blog a list of the most recent articles ? I cannot find it. Actually the only way I found is to click on Syndicate/articles. But it is kind of a hack.

  22. Harry Seldon on 20 Nov 13:05:

    OK to precise my prvious comment. You can actually simply click on Weblog to get the list. However how do you navigate by months, like on most blogs ?

  23. Jerome on 20 Nov 17:25:

    config.i18n.default_locale = :kl_KL

    I18n.t :thanks_this_is_good_news =>”buy’ ngop”

    (f@!#§%g Klingons)

  24. Lukas Rieder on 22 Nov 17:58:

    Dankeschön!

    Merci beaucoup!

    Grazie!

    Das sind großartige Neuigkeiten für die entwicklung exzellenter Web Applikationene im deutschsprachigen Raum.

    Thanks!

    This are great news for developing excellent web applications in the german speaking area.

  25. jfromm on 24 Nov 11:36:

    There seems to be a problem with using ”<<” here for Rails 2.2.2, it causes the TypeError “can’t convert Array into String”, see http://is.gd/8M9i

  26. Huseyin Gomleksizoglu on 24 Nov 12:00:

    config.i18n.default_locale = :tr

    I18n.t :thanks_this_is_good_news => “Teşekkürler, çok güzel bir haber.”

  27. Anton Trapp on 24 Nov 12:57:

    Waited long for this stuff!!!

    But:

    I18n.t :hello NoMethodError: undefined method `each’ for false:FalseClass

    ...i18n…/simple.rb:179:in `load_file’

    No matter if I set the path in environment.rb or try the default…

    Anything to do manually for “old” 2.1 rails apps?

    Thank you very much in advance!

  28. Anton Trapp on 24 Nov 13:25:

    Solved the problems. For all that have the same:

    1) Nothing is added automatically (checked the load_file method in simple.rb).

    2) config.i18n.load_path << Dir[File.join(RAILS_ROOT, ‘my’, ‘locales’, ’*.{rb,yml}’)]

    adds an Array, so you can’t use this to (TypeError: can’t convert Array into String in sample.rb, line 176).

    So add every single translation file to the load path one by one, not as shown here.

    After that the translation worked :)

  29. Sven Fuchs on 25 Nov 10:15:

    Anton,

    config.i18n.load_path += Dir[File.join(RAILS_ROOT, ‘my’, ‘locales’, ’*.{rb,yml}’)]

    ... should work.

    And yes, actually loading translations from the files in load_path is deferred to the latest possible point (i.e. when the first translation is looked up). That’s so that you can still swap the backend in plugin/init or config/initializer stage.

  30. MikeG on 25 Nov 18:30:

    Any suggesstions for moving from the old Globalize (database driven) plugin to this method? I have LOTS of translated fragments in my database that I can’t afford to throw out.

  31. Roman Smirnov on 25 Nov 20:52:

    config.i18n.default_locale = :ru_RU

    I18n.t :thanks_this_is_good_news => “Спасибо за хорошую новость”

  32. Sawood Alam on 27 Nov 12:52:

    I will see how good is this for RTL languages like, Arabic, Urdu, Persian and Hebrew etc.

    :)

  33. Kazim Zaidi on 27 Nov 17:41:

    @MikeG, you can certainly write a simple script to work it out (and run it with script/runner so that it writes Config files)

    Better still if you can make a plugin outta it.

    And thanks to David and Sven Fuchs, the mastermind behind i18n taskforce. :-D

    Rails continues to be the best of web development.

  34. Filippo Camillo on 28 Nov 12:56:

    config.i18n.default_locale = :it_IT

    I18n.t :thanks_this_is_good_news => “grazie! questa è un’ottima notizia”

    Thanks so much! :)

  35. Patate on 28 Nov 19:42:

    Any rake tasks available like the ruby gettext plugin?

  36. Thiago on 01 Dec 23:20:

    config.i18n.default_locale = :pt_BR

    I18n.t :thanks_this_is_good_news => “Obrigado! É uma ótima notícia!”

  37. Alex Yakimchuk on 02 Dec 21:19:

    config.i18n.default_locale = :ua

    I18n.t :thanks_this_is_good_news => “Дякуємо. Новина просто СУПЕР!!!”

  38. Chuck LeDuc Díaz on 10 Dec 02:07:

    Has anyone looked into how to handle these .rb/.yml files with translation management tools like Pootle?