I18n :: Backend ::带范围的ActiveRecord

时间:2013-05-30 16:39:54

标签: activerecord internationalization ruby-on-rails-3.2 scope i18n-gem

我想让用户覆盖locales / YAML文件中的自定义翻译。 我使用了Sven Fuchs的i18n-active_record gem,它非常适合使用存储在数据库中的翻译。

问题:用户应该只获得自己的翻译,而不是其他翻译。

所以我在user_id表中添加了translations列。现在我不知道如何设置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)

感谢您的任何想法!

1 个答案:

答案 0 :(得分:2)

尝试将其添加到初始化文件

ie:添加到初始化i18n的activerecord后端的位置

  

配置/初始化/ 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

中的默认语言环境方法
相关问题