سؤال

I am using Ruby on Rails v3.2.2 and the Globalize3 v0.2.0 gem. At this time I am using Globalize3 in order to translate country, region and city names in two languages but I plan to use it to internationalize my application in many more languages (ideally, all). So, I am populating the database with translation data, but I have some doubts on that: should I populate my country_translations, region_translations and city_translations database tables with all internationalized names (even if it may be that some names are the same as the default ones - in this case the internationalized name is nil or repeated)? that is, in translation tables should I create a translation record for each locale supported by my application (in my current case, two locales) and for each country, region and city?

Making so, in a "ideal" scenario supporting all languages, mentioned tables (mostly those related to regions and cities) will be very large and probabily performance less. On the other hand, it makes sure that Globalize3 will properly work since it seems that in some cases when the internationalized record doesn't exist (I avoid to explain my specific case since it is "hard" to do, and maybe it'd need a book to be explained) that gem does not properly fallback to the current locale.

How should I proceed?

هل كانت مفيدة؟

المحلول

I would advise that you do indeed pre-populate the translations. I have been doing a project where the languages of the content were preliminary known (5 of them), and the customers wanted 5 tabs for populating the translations (when they were editing the objects containing the translations) in the admin backend.

This however cannot happen if you don't have the translations pre-populated. The DB space or speed in this instance is a non-issue, in my opinion; you would need 100,000+ objects with several translations each to introduce even a minor lag on a default MySQL installation.

Assuming you use ActiveAdmin, you would do something like this:

# app/admin/your_models.rb
...
controller do
  def create
     ...
     YOUR_LANGUAGE_CODES.each do |lang|
       @your_model.translations.build(:locale => lang, :text => YOUR_DEFAULT_I18N_TEXT)
     end
  end
end

Thus, when you are creating new object with translations in your admin backed, you would also automatically create all the necessary translations for it. Of course, for a proper cleanup, don't forget to have this in your containing model:

has_many :translations, :dependent => :destroy

...so when you destroy the object, the translations go with it.

...Or alternatively, you could get only the loop code and include it in any other controller method you use to create the object that contains translations.

I didn't quite get your somewhat hierarhic-looking translation model, but I believe I have illustrated my point. You should be able to expand on that to cover your specific needs.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top