skip to Main Content

I have the following configured in my application.rb

config.i18n.available_locales = [:at, :de, :ch_de, :ch_fr, :fr, :int_en, :int_fr]
config.i18n.default_locale = :at

My default locale is set to :at (Austria). Which I require for Route Translation. Rails server won’t start without it and to be fair it makes sense.

I now created a fallback map, which works just fine:

config.i18n.fallbacks = {'de' => 'at', 'ch_de' => 'at', 'ch_fr' => 'fr', 'int_fr' => 'fr', 'fr' => 'fr', 'int_en' => 'int_en'}

So basically I want all German speaking countries to fallback on :at, whilst all French speaking countries fall back to :fr.

However, I do NOT under any circumstances want :fr to fallback on :at. This is for SEO purposes as some french pages do not have metadata configured. So now the french pages would display the Austrian :at metadata entry. Which is wrong.

If I turn of fallbacks completely:

config.i18n.fallbacks = false

the following works fine in my views:

t('.metatitle', :default => "")

In that case if there is no translation available then nothing is displayed. However, the rest of the site that already exists relies on fallbacks – so this is not an option, considering the effort to implement the change.

Is there a way turn off fallbacks for individual translations?

Or can I implement the fallback map and make sure that the map doesn’t fallback to it’s default locale if for example no french :fr translation exists?

PS: The route translating gem that requires the default locale is this one here.

Thank you for your help !

2

Answers


  1. Chosen as BEST ANSWER

    Figured it out - and thought of sharing it with you:

    If you wish to avoid fallback to the default locale on individual translation you simply have to send a empty fallback array like this:

    t('.metatitle', :default => "", :fallback => [])
    

    Et Voila !


  2. This is tricky in Rails up to 6.1 because you need to beat the logic in the Railtie initializer which desperately wants to fallback to the default_locale.

    To set the default fallback locale to nil you need to use the following code:

    config.i18n.default_locale = "de-AT"
    config.i18n.fallbacks.defaults = [[]] # If you just write [], then default_locale is used
    config.i18n.fallbacks.map = {
      :de => "de-AT",
      "de-CH" => "de-AT",
    }
    

    Let’s check:

    $ rails console
    2.7.2 :001 > I18n.fallbacks["de"]
     => [:de, :"de-AT"]
    2.7.2 :002 > I18n.fallbacks["fr"]
     => [:fr]
    2.7.2 :003 > I18n.fallbacks["de-CH"]
     => [:"de-CH", :de, :"de-AT"]
    2.7.2 :004 > I18n.fallbacks["de-AT"]
     => [:"de-AT", :de]
    2.7.2 :005 >
    

    Not 100% what you want, but there just seems to be no way to prevent the fallbacks to go from country specific locale to generic language locale, when fallbacks are enabled.

    Note #1: Your locales are a bit non-standard. AFAIK there is no ‘at’ locale, but only "de-AT".

    Note #2: Some more subtleties and notes in this answer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search