2

In my rails 4.2 app I have set it as the default locale by setting it in config/application.rb:

config.i18n.default_locale = :it

I have a simple "product" model

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

My need is when I18n.locale = :it the content should be written in "products" table while for all others locales the content shoud go in the "product_translations" table. Currently what happens is the following:

if

config.i18n.default_locale = :en

content is written to the "products" table, for all different locales the content goes to the "product_translations" table.

How can I change this?

EDIT

Using the console to test globalize behaviour I found that maybe I did not understand how globalize should work. I was expecting that the "products" table is filled with default_locale (in my case :it) and the "product_translations" table is filled with other locales (:en, :fr, :de and so on).

Instead I see that whichever the locale is, fields that are indicated as

translates :name, :description

are always written in "product_translations" and "product" table only contains those fields that are not translated (in my case uom (unit of measure). This is the output of the console after saving a new product with :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>

Is this the default behaviour? Do I need to remove translatable fields from the original table?

kranz
  • 599
  • 1
  • 6
  • 23

0 Answers0