I18n::后端::具有作用域的ActiveRecord
我希望使用户能够覆盖locales/YAML文件中的自定义翻译。 我使用了Sven Fuchs的I18n::后端::具有作用域的ActiveRecord,activerecord,internationalization,ruby-on-rails-3.2,scope,i18n-gem,Activerecord,Internationalization,Ruby On Rails 3.2,Scope,I18n Gem,我希望使用户能够覆盖locales/YAML文件中的自定义翻译。 我使用了Sven Fuchs的i18n-active_记录gem,它非常适合使用数据库中存储的翻译 问题是:用户应该只得到自己的翻译,而不是别人的翻译 因此,我在translations表中添加了一个user\u id列。现在我不知道如何为I18n::Backend::ActiveRecord设置作用域 Mylocale.rb(在配置/初始值设定项中): 谢谢你的建议 尝试将其添加到初始值设定项文件中 ie:添加到初始化i18n的
i18n-active_记录
gem,它非常适合使用数据库中存储的翻译
问题是:用户应该只得到自己的翻译,而不是别人的翻译
因此,我在translations
表中添加了一个user\u id
列。现在我不知道如何为I18n::Backend::ActiveRecord
设置作用域
Mylocale.rb
(在配置/初始值设定项中):
谢谢你的建议 尝试将其添加到初始值设定项文件中 ie:添加到初始化i18n的activerecord后端的位置 config/initializers/i18n_backend.rb
需要“i18n/后端/活动\u记录”
是否存在ActiveRecord::Base.connection.table_?“翻译
需要“i18n/后端/活动\u记录”
I18n.backend=I18n::backend::Chain。
新建(I18n::Backend::ActiveRecord.new,I18n.Backend)
结束
#覆盖默认查询
模块I18n
模块后端
类活动记录
类转换<::ActiveRecord::Base
类locale.to_s).where(:field=>condition)
结束
结束
结束
结束
结束
结束
这将覆盖i18n-active_记录gem中的默认区域设置方法
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)
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