【问题标题】:How to add translation for document如何为文档添加翻译
【发布时间】:2016-06-07 11:49:22
【问题描述】:

我有一个名为“documents”的表,其中包含附件(通过回形针 gem),我想为这个附件(通过 globalize gem)添加翻译以在 Activeadmin 中使用。 因此,一旦我在活动管理员中打开文档页面,我想添加文档的两个或多个翻译,但使用相同的模型(相同的模型 ID,但只有区域设置更改)。

Document模型的schema create table DB表为:

create_table "documents", force: :cascade do |t|
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.string   "doc_file_name"
    t.string   "doc_content_type"
    t.integer  "doc_file_size"
    t.datetime "doc_updated_at"
    t.integer  "model_id"
  end

数据库是postgres。

【问题讨论】:

  • 您要翻译表格、文档还是在视图中显示表格时?
  • 我只想翻译文档以通过model_id & locale得到它
  • 哦,我明白了。然后是时候检查 I18n 国际化了。
  • gem 'globalize' 帮助您在数据库中拥有 model_translations 表,复制整个模型实例。我认为你的回形针可以在那个单独的模型中,因为它所做的只是完全克隆你的初始模型。我已经在几个项目中做到了这一点。如果您的图片将保持不变,但您想要更改图片属性(即图片描述等),那么这个 gem 就是您的最佳选择。

标签: ruby-on-rails postgresql paperclip activeadmin globalize


【解决方案1】:

最后,我通过从文档表中删除附件“doc”然后使用 globalize gem Document.create_translation_table! 为文档创建翻译表并添加:

has_many :docs
 class Translation
    belongs_to :document
    has_attached_file :doc, MODEL_DOCUMENTS_STORAGE_OPTIONS
    validates_attachment_content_type :doc, content_type: ['application/pdf']
  end

到文档模型,然后最终通过活动管理表单访问它(创建/更新):

form :html => { :enctype => 'multipart/form-data' } do |f|
    f.inputs 'Details' do
      f.translated_inputs 'ignored title', switch_locale: false do |t|
        t.input :doc, :as => :file
      end
    end
    actions
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2012-01-09
    • 1970-01-01
    相关资源
    最近更新 更多