mobility

Any chance of expanding data types for mobility


I love the gem and how it works, I was just wondering if there was any existing or planned functionality to specify data types other than text and string for the translations (stored in mobility_[type]_translations)?


Solution

  • This is not documented, but it's not hard to support other types like Integer, Float, etc.

    e.g. for Integer, you'd have to create a table like this:

      create_table "mobility_integer_translations", force: :cascade do |t|
        t.string "locale", null: false
        t.string "key", null: false
        t.integer "value"
        t.string "translatable_type"
        t.bigint "translatable_id"
        t.datetime "created_at", precision: 6, null: false
        t.datetime "updated_at", precision: 6, null: false
        t.index ["translatable_id", "translatable_type", "key"], name: "index_mobility_string_translations_on_translatable_attribute"
        t.index ["translatable_id", "translatable_type", "locale", "key"], name: "index_mobility_string_translations_on_keys", unique: true
        t.index ["translatable_type", "key", "value", "locale"], name: "index_mobility_string_translations_on_query_keys"
      end
    

    then you'd need to create a class for this table:

    module Mobility
      module Backends
        class ActiveRecord::KeyValue
          class IntegerTranslation < Translation
            self.table_name = "mobility_integer_translations"
          end
        end
      end
    end
    

    I believe this should be enough, and I think you should be able to just do this (assuming your config has key_value as the backend):

    translates :foo, type: :integer
    

    There's nothing in Mobility itself that actually says you can't use another translation class, it's just that these are not offered out-of-the-box. Probably this should be added to the Wiki somewhere.