Search code examples
internationalizationruby-on-rails-5rails-i18n

How can I define a business specific spoken language with i18n?


I develop a Ruby on Rails 5 application in which I use I18n gem for views translations. Terms used in views are quite generic, and are supposed to meet all businesses.

But for one specific customer, the business requires very specific terms. I can't make these terms the general translation. I can imagine 2 ways to support this:

  • create a specific en-customer language to use with i18n
  • create a specific .yml as a subsection of en.yml and switch to it by configuration

How can I do any of these?

Thanks for your help!


Solution

  • This should be quite doable with your first example of creating a customer-specific locale code.

    If you configure I18n to register this custom locale, and configure a fallback for that locale to the base language:

    Rails.application.configure do
      config.i18n.available_locales = %i[en es fr en-customer]
      config.i18n.default_locale = :en
      config.i18n.backend.class.send(:include, I18n::Backend::Fallbacks)
      config.i18n.fallbacks.map('en-customer' => 'en')
    end
    

    This should let you set a preference for users of that company to use the custom locale, and you can create a yaml file to override just the keys you need to.

    You could do this without a fallback, too, but you'd have to copy an entire language, then apply your changes, which can be harder to manage as text in your app changes, and more prone to breakage if you miss a key.