全球化gem如何决定哪些内容转换为翻译表?

时间:2015-07-13 22:12:26

标签: ruby-on-rails rails-i18n globalize

在我的rails 4.2应用程序中,我通过在config / application.rb中设置它将其设置为默认语言环境:

config.i18n.default_locale = :it

我有一个简单的"产品"模特

class Product < ActiveRecord::Base
    translates :name, :description
end

我需要的是当I18n.locale =:它的内容应写在&#34;产品&#34;表格,而对于所有其他人来说,内容应该包含在&#34; product_translations&#34;表。 目前发生的情况如下:

如果

config.i18n.default_locale = :en

内容被写入&#34;产品&#34; table,对于所有不同的语言环境,内容转到&#34; product_translations&#34;表

我该如何更改?

修改

使用控制台测试全球化行为我发现也许我不明白全球化应该如何运作。 我期待&#34;产品&#34; table填充了default_locale(在我的例子中:它)和&#34; product_translations&#34; table填充了其他语言环境(:en,:fr,:de等)。

相反,我看到无论哪个语言环境都是,

指示的字段
translates :name, :description

始终写在&#34; product_translations&#34;和&#34;产品&#34; table仅包含那些未翻译的字段(在我的情况下是uom(度量单位))。 这是使用以下命令保存新产品后控制台的输出:en as locale。

[18] pry(main)> en_p=Product.create(:name=>"butter",
                                    :description => "82% min fat butter", 
                                    :uom => "kg")   (0.3ms)  
BEGIN 
SQL (0.7ms)  
  INSERT INTO "products" ("uom", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  
  [
    ["uom", "kg"], 
    ["created_at", "2015-07-14 05:49:09.097092"], 
    ["updated_at", "2015-07-14 05:49:09.097092"]
  ]
SQL (0.7ms)
  INSERT INTO "product_translations" ("locale", 
                                      "name", 
                                      "description", 
                                      "product_id", 
                                      "created_at", 
                                      "updated_at") 
        VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  
  [
    ["locale", "en"], 
    ["name", "butter"], 
    ["description", "82% min fat butter"], 
    ["product_id", 5], 
    ["created_at", "2015-07-14 05:49:09.116683"], 
    ["updated_at", "2015-07-14 05:49:09.116683"]
  ]
  (15.9ms)  
COMMIT
=> #<Product:0xb63d3568
 id: 5,
 name: "butter",
 description: "82% min fat butter",
 uom: "kg",
 created_at: Tue, 14 Jul 2015 05:49:09 UTC +00:00,
 updated_at: Tue, 14 Jul 2015 05:49:09 UTC +00:00>


[19] pry(main)> I18n.locale=:it
=> :it

[20] pry(main)> it_p=Product.create(:name=>"olio di oliva EVO",
                                    :description => "Olio di oliva extravergine spremuto a freddo", 
                                    :uom => "kg") (0.4ms)  
BEGIN
SQL (0.5ms)  
  INSERT INTO "products" ("uom", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  
  [
    ["uom", "kg"], 
    ["created_at", "2015-07-14 05:51:34.772755"], 
    ["updated_at", "2015-07-14 05:51:34.772755"]
  ]
SQL (0.8ms)
  INSERT INTO "product_translations" ("locale", 
                                      "name", 
                                      "description", 
                                      "product_id", 
                                      "created_at", 
                                      "updated_at") 
        VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  
  [
    ["locale", "it"], 
    ["name", "olio di oliva extravergine"], 
    ["description", "Olio di oliva extravergine ottenuto unicamente per spremitura"], 
    ["product_id", 6], 
    ["created_at", "2015-07-14 05:51:34.779220"], 
    ["updated_at", "2015-07-14 05:51:34.779220"]
  ]
  (16.1ms)  
COMMIT
=> #<Product:0xb6315ce8
 id: 6,
 name: "olio di oliva extravergine",
 description: "Olio di oliva extravergine ottenuto unicamente per spremitura",
 uom: "kg",
 created_at: Tue, 14 Jul 2015 05:51:34 UTC +00:00,
 updated_at: Tue, 14 Jul 2015 05:51:34 UTC +00:00>

这是默认行为吗? 我是否需要从原始表中删除可翻译字段?

0 个答案:

没有答案