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.

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/
For the french that I am, this is a really good news.
Thanks a lot.
Wonderful! Thanks!
Oh, that is great! Thanks!
I’m really happy about localization being built in !
I18n.t :thanks_this_is_good_news => “Danke, das sind gute Neuigkeiten!”
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.
Is there a way to upgrade rails 2.1 application structures to get these extra folders and config files?
For all of us with not English speaking customers this will be an easy and great step forward.
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.
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
config.i18n.default_locale = :es
I18n.t :thanks_this_is_good_news => “¡Gracias! Estas son buenas noticias”
config.i18n.default_locale = :fr
I18n.t :thanks_this_is_good_news => “Merci ! C’est une bonne nouvelle.”
config.i18n.default_locale = :ar
I18n.t :thanks_this_is_good_news => “شكراً ، هذه أخبار جيدة”
Thanks!
It would be really nice to be able to change I18n.default_backend in environment.rb
lolcatz, you can change all the I18n settings through environment.rb. config.i18n.whatever is a mapping for I18n.whatever.
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.
Holy cow, this is amazing! Thanks a ton!
config.i18n.default_locale = :zh_CN
I18n.t :thanks_this_is_good_news => “泄啦;这是一个好消息”
config.i18n.default_locale = :sv
I18n.t :thanks_this_is_good_news => “Tack! Detta är goda nyheter.”
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.
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 ?
config.i18n.default_locale = :kl_KL
I18n.t :thanks_this_is_good_news =>”buy’ ngop”
(f@!#§%g Klingons)
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.
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
config.i18n.default_locale = :tr
I18n.t :thanks_this_is_good_news => “Teşekkürler, çok güzel bir haber.”
Waited long for this stuff!!!
But:
...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!
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 :)
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.
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.
config.i18n.default_locale = :ru_RU
I18n.t :thanks_this_is_good_news => “Спасибо за хорошую новость”
I will see how good is this for RTL languages like, Arabic, Urdu, Persian and Hebrew etc.
:)
@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.
config.i18n.default_locale = :it_IT
I18n.t :thanks_this_is_good_news => “grazie! questa è un’ottima notizia”
Thanks so much! :)
Any rake tasks available like the ruby gettext plugin?
config.i18n.default_locale = :pt_BR
I18n.t :thanks_this_is_good_news => “Obrigado! É uma ótima notícia!”
config.i18n.default_locale = :ua
I18n.t :thanks_this_is_good_news => “Дякуємо. Новина просто СУПЕР!!!”
Has anyone looked into how to handle these .rb/.yml files with translation management tools like Pootle?