ruby-on-rails - I18n 不使用公寓 gem rails 加载翻译

标签 ruby-on-rails multi-tenant ruby-on-rails-5 apartment-gem

我正在使用 apartment gem 制作一个 Multi-Tenancy 应用程序.我已经全部设置好,一切都按预期工作。我还使用带有事件记录后端的 Rails Internationalization (I18n) 来存储翻译。我目前的设置

翻译表

class CreateTranslations < ActiveRecord::Migration[5.0]
  def change
    create_table :translations do |t|
      t.string :locale
      t.string :key
      t.text :value
      t.text :interpolations
      t.boolean :is_proc, default: false

      t.timestamps
    end
  end
end

Apartment.rb配置

我已将翻译模型添加到排除模型列表中,因此它在所有租户中都是全局的

Apartment.configure do |config|

  # Add any models that you do not want to be multi-tenanted, but remain in the global (public) namespace.
  # A typical example would be a Customer or Tenant model that stores each Tenant's information.
  #
  config.excluded_models = %w{ User Workspace Translation }
end

在我的翻译表中,我有英语(默认)和挪威语的翻译。在主域上,一切都按预期在英语和挪威语之间切换,但一旦我加载租户,所有翻译都会丢失。控制台中的演示:

> Apartment::Tenant.switch! # switch to main tenant
> I18n.locale = :en # use English translations
> I18n.t('home.members_label')
  => "Show Members"

> Apartment::Tenant.switch! "elabs" # switch to a different tenant
> I18n.t('home.members_label')
  => "translation missing: en.home.members_label"

我不确定为什么在租用环境中会缺少翻译。我认为在 excluded_models 列表中有 Translation 模型应该可以解决问题,但似乎某处出了问题。有什么线索吗?谢谢

最佳答案

Translation 模型实际上是在 I18n::Backend::ActiveRecord::Translation 中定义的,因此您可能必须添加一个扩展该模型的模型在模型文件夹中或尝试执行以下操作,看看是否可行:

config.excluded_models = %w{ User Workspace I18n::Backend::ActiveRecord::Translation }

或许

Translation = I18n::Backend::ActiveRecord::Translation
config.excluded_models = %w{ User Workspace Translation }

关于ruby-on-rails - I18n 不使用公寓 gem rails 加载翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37183487/

相关文章:

ruby-on-rails - 如何在不使用子域的情况下设置 Multi-Tenancy Rails 应用程序?

Node.js 多个子域

ruby-on-rails - rails : Render a status code without rendering a template

rake db :create 后 MySql 错误 1045

c# - 如何在 .net 核心 API 中通过 API key 或用户凭据处理身份验证?

ruby-on-rails - rails 3 中的脚本/运行器

ruby-on-rails - 我可以在 Rails 4 及更高版本中使用 change_column 方法回滚吗?

ruby-on-rails - rails 5 : can't order after group and count

mysql - 为什么我的表的编码错误?

ruby-on-rails - Apache 对 POST 请求大小的限制