2011-06-14 20 views

回答

4

Rails支持用於翻譯的非YAML存儲。如果您想將翻譯存儲在表格中,請使用i18n-active_record寶石。

在可定製的I18n後端上觀看Railscast

如果使用i18n-active_record寶石,確保memoizeflatten以獲得最佳性能的關鍵如下圖所示(摘自readmei18n-active_record寶石代碼示例)

I18n.backend = I18n::Backend::ActiveRecord.new 
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize) 
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten) 
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize) 
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization) 
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend) 
0

您應該創建一個名爲translations的表。

在您的產品型號:

has_many :labels 

在你的標籤型號:

has_one :translation 

然後你需要你的翻譯表可以有儘可能多的語言:

Product.first.labels.first.translation.en 
Product.first.labels.first.translation.cn 
Product.first.labels.first.translation.fn 

有了這個您可以撥打的邏輯:

Product.all.each do |p| 
    p.lables.each do |l| 
     l.translation.en 
     l.translation.cn 
     #etc.... 
    end 
end