Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rails I18n default_locale and fallbacks by user preference

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading