Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails: how can I override a locale based on subdomain?

I have internationalized and localized my application using the standard rails mechanisms. Everything is stored in en, fr, de.yml files.

My application is multi-tenant, based on the subdomain.

I would like to allow my users to override certain translations in the application (e.g. to change "Employee" to "Associate" because it matches their own terminology).

I've tried to change the load path of my yml files on a per-request basis, but to no avail.

Any idea how I could, for each request, look up first in my user-specific yaml file, and fall back to the default yaml file if the translation was not overriden?

like image 597
Pierre Avatar asked Feb 17 '23 06:02

Pierre


1 Answers

Assuming you store the subdomain in an instance variable from a controller filter, you could override the translation helper to do a lookup with a subdomain-specific scope first, then fallback to the specified or default scope. Something like this:

module ApplicationHelper

  def t(key, original_options = {})
    options = original_options.dup
    site_translation_scope = ['subdomain_overrides', @subdomain.name]
    scope =
      case options[:scope]
      when nil
        site_translation_scope
      when Array
        options[:scope] = site_translation_scope + options[:scope]
      when String
        [site_translation_scope, options[:scope]].join(".")
      end
    translate(key, options.merge(:scope => scope, :raise => true))
  rescue I18n::MissingTranslationData
    translate(key, original_options)
  end

end

Then you add your subdomain-specific overrides likes this:

en:
  customer: Customer
  subdomain_overrides:
    subdomain_1:
      customer: Buyer
like image 114
Mihai Târnovan Avatar answered Mar 04 '23 05:03

Mihai Târnovan