Skip to content
Chris Salzberg edited this page Mar 29, 2017 · 16 revisions

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.

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