Tigraine
Tigraine

Reputation: 23648

Share translations between multiple locales in Rails3

I have locales that come in the form of en-GB, en-US, de-DE, de-AT, de-CH etc..

Now the issue is that de-DE, de-AT, de-CH all share the same translations. So having to define them multiple times is annoying and not very DRY.

Before anyone suggests just using :de as locale, I can't. That's how I did it before and due to some business logic I can't change the new en-GB format was forced onto me.

Help would be very much appreciated.

Upvotes: 0

Views: 108

Answers (2)

tigrish
tigrish

Reputation: 2518

Still DRY still but more importantly your translations aren't duplicated in memory :

Create an i18n initializer that includes fallbacks :

require "i18n/backend/fallbacks" 
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)

And move your translations into de: and en: locales.

A call like this

I18n.t :foo, :locale => 'de-AT'

will first look for de-AT.foo and providing there is no match, will look for de.foo.

More info on the i18n wiki.

Upvotes: 1

jdoe
jdoe

Reputation: 15771

If you're using YAML for you localization then you already have necessary tools. YAML allows you to write something like mixins:

en: &english
   hello: Hello, %{username}!

en-GB:
  <<: *english

en-US:
  <<: *english

I hope you got the idea of DRYing here.

Upvotes: 3

Related Questions