Rails 3 I18n for database tables

I am looking for some tips and tricks for best practices for using internationalization. I searched around, but I'm not very happy with what I read. Most of the articles I read focus on using yml files for I18n that won't work in my situation.

Currently, I have several database tables with English text. Some of these tables contain text fields with a length of several sentences, and some contain 6 + paragraphs of text. I would also like to have these fields in Spanish.

The approach that I am currently considering is to use the I18n-active shortcut and have 1 translation table that the application will use for all translations in the application

class CreateTranslations < ActiveRecord::Migration def self.up create_table :translations do |t| t.string :locale t.string :key t.text :value t.text :interpolations t.boolean :is_proc, :default => false t.timestamps end end def self.down drop_table :translations end end 

Is this the best way to continue?

On the one hand, all translations will be well kept in one table. On the other hand, every time a user requests a database for I18n content. The application will query the source table for the key, and then query the translation table. Another problem is that the translation table will be massive and has a huge number of lines, since it will store all the translations for the application (everything from the section heading [a few words] to entire pages of text.

Any information is appreciated. Thanks

+5
source share
2 answers

Saving translations in db is not a bad solution. Do not be afraid of large tables - databases are created for this! Just make sure your indexes are configured correctly and cache everything you can.

Another, faster, and perhaps better solution is to use Redis as the backend for the I18n. See http://guides.rubyonrails.org/i18n.html#using-different-backends and http://railscasts.com/episodes/256-i18n-backends .

Where you save translations, you donโ€™t have to try to manage the interpolations yourself, as the I18n library handles this pretty nicely (unless you do something really ordinary, that is).

+6
source

The only advantage you will need to store in the database is to pretend editing on the fly. Therefore, if that was your intention, the offer of downtime is the way to go. But if you donโ€™t think about it, simple YML will do the job of translating the interface, table names, field names, etc.

If you want to store your data in more than one language, they need to spend some time on specific database modeling.

You can basically do this in two ways: - Duplicate the fields you need in more than one language - Duplicate the entered yen and flag using the language, you can also indicate the source version on all copies in order to better track them.

-1
source

Source: https://habr.com/ru/post/971749/


All Articles