Internationalizing Thousands of Product Shortcuts in Rails

What would be the best way to internationalize thousands of product labels in Rails? Do I have to enter all thousands of tags in a YAML file? Or are there better solutions out there?

Any help would do :)

+4
source share
2 answers

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

Check out Railscast for customizable I18n servers.

If you use i18n-active_record , make sure that memoize and flatten keys are for optimal performance, as shown below (code image taken from readme 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) 
+4
source

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 

Using 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
source

All Articles