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:
-
Dir.glob("#{RAILS_ROOT}/lib/locale/*.{yml|rb}").each do |f|
-
I18n.backend.load_translations f
-
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…