开发者

Internationalization of thousands of product labels in Rails

开发者 https://www.devze.com 2023-03-12 12:03 出处:网络
W开发者_StackOverflow社区hat would be the best way to internationalize thousands of product labels in Rails?

W开发者_StackOverflow社区hat would be the best way to internationalize thousands of product labels in Rails? Should I enter all thousand labels in a YAML file? Or are there better solutions out there?

Any help will do :)


Rails supports non YAML storage for translations. If you want to store the translations in a table, use the i18n-active_record gem.

Watch the Railscast on customizable I18n backends.

If you use the i18n-active_record gem, make sure to memoize and flatten the keys for optimal performance as shown below(code sample taken from readme of i18n-active_record gem)

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)


You should create a table called translations.

In your product model:

has_many :labels

In your Label model:

 has_one :translation

Then your translation table can have as many languages as you need:

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

With this logic you can call:

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

精彩评论

暂无评论...
验证码 换一张
取 消