rails: 如何基于子域覆盖locale?

问题描述 投票:4回答:3

我已经使用标准的rails机制对我的应用程序进行了国际化和本地化,所有的东西都存储在en、fr、de.yml文件中。

我的应用程序是基于子域的多租户。

我想允许我的用户在应用程序中覆盖某些翻译(例如,将 "Employee "改为 "Associate",因为它符合他们自己的术语)。

我试着在每个请求的基础上改变我的yml文件的加载路径,但是没有用。

有什么办法可以让我在每次请求时,先在我的用户特定的yaml文件中查找,如果翻译没有被覆盖,再回到默认的yaml文件?

ruby-on-rails ruby-on-rails-3 internationalization rails-i18n
3个回答
6
投票

假设你把子域存储在控制器过滤器的实例变量中,你可以覆盖翻译助手,先用子域特定的作用域进行查找,然后回退到指定或默认的作用域。就像这样。

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

然后你可以添加你的子域特定的覆盖,比如这样。

en:
  customer: Customer
  subdomain_overrides:
    subdomain_1:
      customer: Buyer

2
投票

如果你想让租户使用特定的语言,但回退到默认的,我写了一个微观的 图书馆 会完成工作的。

https:/github.comElMassimoi18n_multitenant。

它负责配置 I18n 来回退到基本的locale,允许您使用特定于租户的翻译(如果有的话)。它的设计是为了与默认的后端静态的 .yml 文件,但它也应该可以与其他 I18n 后端。


0
投票

我最近创建了 I18n_global_scope gem,可以做到你所描述的那些,请查看源码 https:/github.commobilityhousei18n_global_scope。 并让我知道你的反馈。

© www.soinside.com 2019 - 2024. All rights reserved.