gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:55:59 24 4
gpt4 key购买 nike

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

我的应用程序是 Multi-Tenancy 的,基于子域。

我想允许我的用户覆盖应用程序中的某些翻译(例如,将“员工”更改为“同事”,因为它匹配他们自己的术语)。

我试图根据每个请求更改 yml 文件的加载路径,但无济于事。

知道我如何为每个请求首先在我的用户特定的 yaml 文件中查找,如果翻译没有被覆盖,则回退到默认的 yaml 文件吗?

最佳答案

假设您将子域存储在来自 Controller 过滤器的实例变量中,您可以覆盖翻译助手以首先使用特定于子域​​的范围进行查找,然后回退到指定或默认范围。像这样的东西:

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

关于ruby-on-rails - rails : how can I override a locale based on subdomain?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15267071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com