2

For example i have project model and 5 available locales. User should be able to create project localized to those 5 locales? What is the best way to add this kind of feature, is there some rails way for this? I understand that i should store all those translations in database, but something like text_en, text_pt, text_es, text_fr, text_it for each field is scares me. Can you give me an advice? Thanks!

UPD. Seems like i find nice tool for the job https://github.com/svenfuchs/globalize3 :)

Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70

3 Answers3

1

Globalize3 is pretty coll, yes, but it can lead in some trouble for bigger projects.

Mostly because it creates one table per type of data you want to translate. I've saw a project with more than 35 (!) translation tables, which complexify hugely the maintenance. Imagine just the translation admin system for this project. It's some complexity you can avoid also.

The idea of using serialization is pretty good, I would recommend it with postgres and jsonb (or hstore). It will still allow you to do some full text search:

MyModel.where("name @> ?", {fr: "NomFrancais"}.to_json)

The operator @> means "it contains". The search is using full text optimizations so the cost is not so high. You can get here more information:

http://www.postgresql.org/docs/9.4/static/datatype-json.html

Otherwise I've made a gem, which is still not mature, but it allows to have all the i18n of database in the same table: https://github.com/anykeyh/rails_db_localize

The advantages here is you can build easily a translation interface, and it completely overlaying your project (no schema update when you want to translate a field).

Translation is always painful in my opinion, and should never underestimated :)

Hope it helps!

Yacine
  • 221
  • 1
  • 9
0

You may want to use a serialized attribute for locale-dependent data, and define a title (or anything) as a hash:

class Project
  serialize :title, Hash
...

p.title[:pt] = "title in pt"

The problem will be that you will not be able to do SQL search for the textual values. If you want to search projects by names, just extract the locale-dependent attributes to another table:

class Project
  has_many :titles
end

class Title
  belongs_to :project
  belongs_to :locale # or just put there a string attribute
end
Arsen7
  • 12,522
  • 2
  • 43
  • 60
0

I would create another table. Main table would keep only key of text. Second table would keep localized text with field key and localization key. Of course text key + localization key should be unique.

You would select one language as a primary language. So, for example english text would be key for another.

Hope it helps..

Jan
  • 400
  • 5
  • 14
  • The problem with choosing a phrase in some language as the key for other translations has two problems: 1) you cannot easily improve your key-language wording, because it will force you to update all other translations, and 2) the same sentence in the key-language, used in two contexts, may need different translations in some other languages - and that would be impossible to implement. A much better idea is IMHO to use some language-agnostic symbols, and translating it to other languages, without favoring any single one. – Arsen7 Jun 15 '12 at 10:40
  • If you use relationships you can use cascade update feature. So, if you improve key language you also change the key in the translation table. But it was just a second idea. A developer can decide to use or not if it suits with his/her case. – Jan Jun 15 '12 at 23:00