请尝试使用i18n-active_record。因为我们可以将所有locales 存储在database 中。
在这个 gem 中,他们使用没有关系的 translation 模型,在您的情况下,您可以在 relation 和 domain 之间创建关系,然后您可以使用 domain 存储所有区域设置,基于获取区域设置时您可以覆盖默认 gem 方法。
==> 您的密钥应包含用于查找域的使用域名,或者您可以使用其他方式获取域名,例如 thread 或 env,您可以保存您的值。
# 我的猜测键应该是 login-label-test.com
domain = Domain.where(:name => namespace.split("-").last).first
在 gem 中找到translation 时,只需覆盖该方法,请尝试一下。
我不确定,请尝试一次
首先将您的locale 文件迁移到db
require 'yaml'
namespace :locale do
desc "Copy translations from yml yo db. Non-overwriting."
task :yml_to_db, [:environment, :locale,:domain] => :environment do |t, args|
info = YAML::load(IO.read("#{Rails.root}/config/locales/#{args[:locale]}-#{args[:domain]}.yml"))
domain = Domain.where(:name => args[:domain]).first
info["#{args[:locale]}"].each do |key, value|
translation = domain.translations.where(:key => key)
domain.translations.create!(:key => key, :value => value, :locale => args[:locale]) if translation.blank?
end
end
end
我试图覆盖 gem 中的一种方法:
require 'active_record'
module I18n
module Backend
class ActiveRecord
class Translation < ::ActiveRecord::Base
belongs_to :domain
TRUTHY_CHAR = "\001"
FALSY_CHAR = "\002"
self.table_name = 'translations'
serialize :value
serialize :interpolations, Array
def lookup(keys, *separator)
# in your keys should contain use domain name for find the domain or you can use some other way to get the domain name like in thread or env you can save your value.
column_name = connection.quote_column_name('key')
keys = Array(keys).map! { |key| key.to_s }
unless separator.empty?
warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
"You can change the internal separator by overwriting FLATTEN_SEPARATOR."
end
namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
# my guess key should be login-label-test.com
domain = Domain.where(:name => namespace.split("-").last).first
where("#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace).where(:id => domain.id)
end
end
end
end
请像这样尝试。