-
Notifications
You must be signed in to change notification settings - Fork 82
Table Backend
The Table backend stores translation in a model-specific table. For a model Post
with a table posts
, the translation table would be post_translations
. The translation table has columns for every translated attribute, as well as a column for the locale and a foreign key pointing to the model table (post_id
).
A detailed description of this backend is provided as "Strategy #2" in this blog post.
Unlike the default KeyValue backend, for the Table backend you will need to generate and run a migration for each translated model. Mobility has a built-in generator for this. If your default backend is set to :table
, and you want to translate a model Post
to add translated columns title
(string) and content
(text), here is how you would do it:
rails generate mobility:translations post title:string content:text
This will generate a migration to create the following table:
create_table "post_translations", force: :cascade do |t|
t.string "title"
t.text "content"
t.string "locale", null: false
t.integer "post_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["locale"], name: "index_post_translations_on_locale"
t.index ["post_id", "locale"], name: "index_post_translations_on_post_id_and_locale", unique: true
t.index ["post_id"], name: "index_post_translations_on_post_id"
end
As can be seen, the translations table has two attribute columns, title
and content
, as well as a locale
column, timestamps, and a reference to the post (post_id
), as well as some indices to speed up translation retrieval. Note that the index on post_id
and locale
is unique, since normally you would never have more than one translation for a given locale on a given record.