0

我喜欢 gem 以及它是如何工作的,我只是想知道是否有任何现有或计划的功能来指定翻译以外的数据类型textstring存储在 中mobility_[type]_translations)?

4

1 回答 1

1

这没有记录,但不难支持其他类型,如 Integer、Float 等。

例如对于整数,您必须创建一个像这样的表:

  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

那么你需要为这个表创建一个类:

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

我相信这应该足够了,我认为你应该能够做到这一点(假设你的配置key_value作为后端):

translates :foo, type: :integer

Mobility 本身并没有您不能使用另一个翻译类,只是这些不是开箱即用的。可能这应该添加到 Wiki 的某个地方。

于 2021-03-11T12:24:12.623 回答