【问题标题】:I18n::Backend::ActiveRecord with scopeI18n::Backend::ActiveRecord 范围
【发布时间】:2013-05-30 16:39:54
【问题描述】:

我想让用户覆盖语言环境/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)

感谢您的任何想法!

【问题讨论】:

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


    【解决方案1】:

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

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

    config/initializers/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 中的默认语言环境方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      相关资源
      最近更新 更多