Talk Unafraid
The (occasionally coherent) ramblings of a geek
  • Home
  • Contact
  • Hire me!
You are here: Home / 2008 / October / Rails I18n Tips

Rails I18n Tips

By James Harrison on October 27, 2008

I’ve just finished implementing and localising Nexus in it’s entirety. It’s a weighty YAML file of translations, weighing in at just over 350 phrases and words.

I thought I’d share my tips on implementation as there seem to be a few gotchas. For me, load_path didn’t quite work as intended, so I came up with this:

I18n.default_locale = 'en-GB'
  1. Dir.glob("#{RAILS_ROOT}/lib/locale/*.{yml|rb}").each do |f|
  2.   I18n.backend.load_translations f
  3. end

This goes in environment.rb after the Rails Initializer, and loads in all the translation files I have in /lib/locale. Once loaded, you can set the locale you want in a set_locale method as before_filter in ApplicationController and you’re set. You can now use I18n.t(‘key’) in views and so on

Some tips and warnings, however.

  • Don’t reuse phrases. What’s reusable in one language may not be in another.
  • Namespace. I made the fatal mistake of not namespacing enough- I would recommend models, views and controllers as your bases.
  • Where possible, keep HTML out of your YAML. It makes it easier to read and keeps formatting and style to views instead of localisation material.

And don’t lose hope. It took me a solid day sat down working through the app to get everything sorted out for Nexus, but it’s very much worth it! Now the next challenge-finding translators…

Posted in Code Snippets and Examples, Nexus | Tagged i18n, rails
Previous Entry: Nexus, Rails 2.2, I18n
Next Entry: EnvyCasts

Copyright © 2012 Talk Unafraid.

Powered by WordPress and Prototype.