2

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

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

所以我在表格中添加了一user_idtranslations。现在我不知道如何为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)

感谢您的任何想法!

4

1 回答 1

2

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

即:添加到为 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 中的默认语言环境方法

于 2014-10-16T08:52:13.993 回答