Are duplicate translations necessary for same strings in Globalize3?

668 Views Asked by At

I was hoping to utilize Globalize3 to translate models in a project I'm building. However, trying it out it looks like each model, say Post like:

class Post < ActiveRecord::Base
    translates :title, :body
end

will be completely independent from other posts in the sense that a title must be translated multiple times even if the title is identical to an already translated one. I.e. the system already knows that it should be.

I understand that in many cases the strings are unique, so this is not a problem, however, imagine a model of a car with color attribute. If the system has hundreds of cars with the same color, am I supposed to translate the color that many times? I'd like to just translate once and all can use the same translation if the string is same.

Have I misunderstood something or is this really how Globalize3 works? If so, it really sounds like it mostly fits when the strings are unique, like in a blog.

Is there another gem available that does what I'd need?

Thanks for your help!

2

There are 2 best solutions below

0
On

Essentially that's how Globalize works, that each model record's attribute is translated and saved in a translation record.

Your problem can be solved by normalizing the table, so color becomes a model itself, and you translate that.

I would not do that though, and I do not know any gem for your scenario. I would instead do the following:

Store RGB values instead, and in the controller convert it into color names. Then you can then translate that using Rails i18n module.

Or a variation of the above, you use the color names as i18n keys, like t("color.#{car.color.parameterize}"), and in the en.yml have

en:
  color:
    dark-blue: 'Dark Blue'
0
On

It's an issue you can't easily avoid, if the strings to translate are "standard" you can go with the solution proposed by lulalala (model that information).

I had a similar problem (not in Rails but it's the same) and we "fixed" that with a task that periodically and automatically copy translations for duplicated content.