Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make rails i18n locales fallback to each other?

I'm using Rails 3 with Globalize3 0.2.0.beta4

Ideally I need :fr to fallback to :en and vice versa.

There are cases when only a French translation is available and I need to show it even if the locale is :en.

I tried

config.i18n.fallbacks = { :fr => :en, :en => :fr }

but somewhat unsurprisingly it causes a stack level too deep error.

like image 375
marc Avatar asked Oct 29 '11 23:10

marc


3 Answers

I'm changing my answer.

To enable fallbacks, add the following to your environment.rb file:

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

Then, you can enable circular fallbacks like you were trying to, eg:

   config.i18n.fallbacks = {'en' => 'fr', 'fr' => 'en'}

In this case, if something is missing in the en locale, it'll check the fr locale, and then the other way around. I don't get any errors running this.

Source: http://batsov.com/articles/2012/09/12/setting-up-fallback-locale-s-in-rails-3/

like image 152
jay Avatar answered Nov 15 '22 16:11

jay


If you pass an array of locales they will be set as default fallbacks for all locales.

config.i18n.fallbacks = [:en, :fr]

Unfortunately, I haven't found a way to set up just two locales to fall back to each other.

like image 35
Simon Perepelitsa Avatar answered Nov 15 '22 16:11

Simon Perepelitsa


In the end I monkey patched Globalize3. Not great as I have to update the patch whenever the site needs a new locale, but hey, it worked.

module Globalize

  class << self

    def fallbacks(locale = self.locale)
      case locale
      when :en then [:en, :fr]
      when :fr then [:fr, :en]
      end
    end

  end
end
like image 23
marc Avatar answered Nov 15 '22 16:11

marc