Is it possible to make i18n rails a locale backup?

I am using Rails 3 with Globalize3 0.2.0.beta4

Ideally, I need: fr to return to: en and vice versa.

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

I tried

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

but it’s somewhat unsurprising that this causes a too deep stack level error.

+8
ruby-on-rails ruby-on-rails-3 internationalization globalization
source share
5 answers

I am changing my answer.

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

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

Then you can enable circular backups as you tried, for example:

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

In this case, if something is missing from en locale, it will check the locale fr, and then vice versa. I do not get any errors when working with this.

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

+5
source share

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

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

Unfortunately, I did not find a way to configure only two locales to return to each other.

+1
source share

In the end, I monkey paid for globalization 3. Not bad, since I have to update the patch whenever the site needs a new language, 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 
+1
source share

This seems to have changed:

 Globalize.fallbacks = {:en => [:en, :fr], :fr => [:fr, :en]} 

Retrieved from official docs: https://github.com/globalize/globalize#fallback-locales-to-each-other

0
source share

In the latest i18n gem (0.7.0), I found it necessary to define backup locales such as this (in config/application.rb ):

 # Custom I18n fallbacks config.after_initialize do I18n.fallbacks = I18n::Locale::Fallbacks.new(at: :"de-DE", ch: :"de-DE", gb: :"en-US") end 

You also need to set config.i18n.fallbacks = true in all config/environments/*.rb files.

0
source share

All Articles