2

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

config.i18n.default_locale = :it

我有一个简单的“产品”模型

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

我需要的是当 I18n.locale = :it 内容应该写在“products”表中,而对于所有其他语言环境,内容应该写在“product_translations”表中。目前发生的情况如下:

如果

config.i18n.default_locale = :en

内容被写入“products”表,对于所有不同的语言环境,内容都被写入“product_translations”表。

我怎样才能改变这个?

编辑

使用控制台测试 globalize 行为我发现也许我不明白 globalize 应该如何工作。我期待“products”表中填充了 default_locale(在我的情况下为 :it),而“product_translations”表中填充了其他语言环境(:en、:fr、:de 等)。

相反,我看到无论语言环境是什么,字段都表示为

translates :name, :description

总是写“product_translations”中,而“product”表只包含那些未翻译的字段(在我的例子中是 uom(计量单位)。这是在保存新产品后控制台的输出:en 作为语言环境。

[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>

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

4

0 回答 0