我希望用户能够覆盖locales/YAML文件中的自定义翻译。我使用Sven Fuchs的i18n-active_record gem,它非常适合使用存储在数据库中的翻译。
问题是:用户应该只得到他们自己的翻译,而不是别人的翻译。
因此,我在translations表中添加了一个user_id列。现在我不知道如何为I18n::Backend::ActiveRecord设置作用域。
我的locale.rb (在配置/初始化器中):
require 'i18n/backend/active_record'
I18n.backend = I18n::Backend::ActiveRecord.new
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
I18n.backend = I18n::Backend::Chain.new(I18n.backend, I18n::Backend::Simple.new)谢谢你的任何想法!
发布于 2014-10-16 16:52:14
尝试将其添加到初始化器文件中
ie:添加到初始化i18n的活动记录后端的位置
配置/初始化器/i18n_backend.rb
require 'i18n/backend/active_record'
if ActiveRecord::Base.connection.table_exists? 'translations'
require 'i18n/backend/active_record'
I18n.backend = I18n::Backend::Chain.
new(I18n::Backend::ActiveRecord.new, I18n.backend)
end
# OVERRIDING DEFAULT QUERY
module I18n
module Backend
class ActiveRecord
class Translation < ::ActiveRecord::Base
class << self
def locale(locale)
where(:locale => locale.to_s).where(:field => condition)
end
end
end
end
end
end这应该会覆盖i18n-active_record gem中的缺省语言环境方法
https://stackoverflow.com/questions/16841599
复制相似问题