Posted about 9 years ago. Visible to the public. Repeats.

I18n fallback locales

When you need to create a locale for a language variant (like Austrian for German), you probably don't want to duplicate your entire de.yml file only to change a few minor exceptions for our Austrian friends.

Luckily, the I18n gem used by Rails has a fallback feature where you can make one locale file fall back to another if no translation is available.

In the example above you would have a config/locales/de_DE.yml:

Copy
de_DE: # hundreds of translations here

... and another locale config/locales/de_AT.yml:

Copy
de_AT: # only a handful exceptions here

Now write the following lines to config/initializers/i18n.rb to make de_AT fall back to de_DE:

Copy
require "i18n/backend/fallbacks" I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) I18n.fallbacks.map('de_AT' => 'de_DE')

Rails 2

For Rails 2.3.11 you need to upgrade your project to use the I18n gem by adding this to the top of your Gemfile:

Copy
gem 'i18n'

Then run

Copy
bundle install
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 4 years ago
by Emanuel De
Keywords:
swiss, switzerland, at, ch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more