Home > Net >  Rails I18n default_locale and fallbacks by user preference
Rails I18n default_locale and fallbacks by user preference

Time:11-16

Rails internationalization has a nice fallback feature where empty linguistic versions don't show empty content, but a default locale's content via application configuration in application.rb

    config.i18n.default_locale = :en
    config.i18n.fallbacks = true

By inference, fallbacks may be set to true, but if the application knows not what locale to fallback upon, then there is breakage.

For multi-user application with fallbacks, what approach must be taken to have the fallback local not be the config.i18n.default_locale but a user defined one ?

CodePudding user response:

You can overwrite the fallback configuration from the application.rb by setting the I18n.fallbacks on the app level. For example:

#application_controller

before_action :set_locale

def set_locale
  ......
  I18n.fallbacks = current_user.fallback_locales
end

That being said, on the user account page might be a field/list of fallback languages that user can choose, so every time I18n goes to fallback it will display it in the preselected language.

Hope this helps.

Cheers

  • Related