Search code examples
ruby-on-railsruby-on-rails-4internationalizationrails-i18n

Avoid I18n Fallback to default locale


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 !


Solution

  • 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 !