Model katmanına I18n desteği eklemek için globalize3 gemini kullanıyoruz.
Gemfile’ a gem 'globalize3' ekleyip bundle install ile gemi kuralım.
Modelde I18n desteği eklenilecek fieldları belirtiyoruz.
1
2
3
4
class Country < ActiveRecord : :Base
attr_accessible :name
translates :name
end
Daha sonra migrationda translates tablosunu oluşturacak kodu ekliyoruz.
1
2
3
4
5
6
7
8
9
10
class CreateCountries < ActiveRecord : :Migration
def change
create_table :countries do | t |
t . timestamps
end
Country . create_translation_table! :name => :string
Country . create ( name : "Turkey" )
end
end
rake db:migrate komutunu çalıştıralım.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>> I18n . locale
:en
>> Country . first . name
"TURKEY"
>> I18n . locale = :tr
:tr
>> Country . first . name
nil
>> c = Country . first
#<Country id: 1, name: nil, created_at: "2013-01-24 08:20:59", updated_at: "2013-01-24 11:09:31">
>> c . name = "Türkiye"
"Türkiye"
>> c . save
true
>> I18n . locale
:tr
>> Country . first . name
"Türkiye"
Örnekte locale :tr iken bir veri kaydedilmediğinden nil sonucu döndü. Eğer nil yerine default locale değerinin dönmesini istersek application.rb dosyasına aşağıdaki satırı ekleyelim.
1
config . i18n . fallbacks = true
İyi çalışmalar dilerim.