如何将翻译添加到自定义扩展程序?

时间:2016-06-04 17:33:52

标签: refinerycms

我正在尝试将翻译添加到名为Post的自定义扩展程序中,但是当我尝试保存帖子时出现错误。

我启用了三个区域设置(:es,:en,:pt)。我注意到当我打开表单时,没有选择默认语言环境。

我错过了什么?

enter image description here

迁移:

class PostTranslations < ActiveRecord::Migration

  def up
     ::Refinery::Posts::Post.create_translation_table!({
     :title => :string,
     :description => :text },
     :migrate_data => true)
     remove_column :refinery_posts, :title
     remove_column :refinery_posts, :description
  end
  def self.down
     add_column :refinery_posts, :title
     add_column :refinery_posts, :description
     ::Refinery::Posts::Post.drop_translation_table! :migrate_data => true
  end
end

_locale_picker.html.erb

<input type="hidden" name="switch_locale" id="switch_locale" value="<%= local_assigns[:current_locale] %>" />
<% if Refinery::I18n.frontend_locales.many? %>
  <label><%= t('.language') %></label>
  <ul id="switch_locale_picker" class="clearfix">
    <% locales = Refinery::I18n.locales.clone %>
    <% Refinery::I18n.frontend_locales.each do |locale| %>
      <% locale_name = locales.delete(locale) %>
      <li<%= %Q{ class=selected} if locale.to_s == local_assigns[:current_locale].to_s %>>
        <%= link_to refinery.url_for(:switch_locale => locale, :parent_id => params[:parent_id]), id: locale do %>
          <div class="<%=locale %> locale_marker">
            <%= locale_text_icon(locale.upcase) %>
          </div>
          <%= locale_name %>
        <% end %>
      </li>
    <% end %>
  </ul>
<% end %>
邮政形式

...
  <%= render :partial => "locale_picker",
    :locals => {
    :current_locale => Thread.current[:globalize_locale]
    } if defined?(::Refinery::I18n) %>
...

架构:

...
  create_table "refinery_post_translations", force: :cascade do |t|
    t.integer  "refinery_post_id", null: false
    t.string   "locale",           null: false
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.string   "title"
    t.text     "description"
  end

  add_index "refinery_post_translations", ["locale"], name: "index_refinery_post_translations_on_locale", using: :btree
  add_index "refinery_post_translations", ["refinery_post_id"], name: "index_refinery_post_translations_on_refinery_post_id", using: :btree

  create_table "refinery_posts", force: :cascade do |t|
    t.integer  "post_image_id"
    t.date     "post_date"
    t.string   "author"
    t.integer  "position"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
...

错误:

Started POST "/refinery/posts" for ::1 at 2016-06-04 13:22:08 -0400
Processing by Refinery::Posts::Admin::PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MIHqOv9yCQWkIdS2dKInFbJQtX62+21iqqTDzBtSCOHfBr7TejNIOgQX6iJgI4TWVqa+DVjsemz1DgkhtIZFOw==", "switch_locale"=>"", "post"=>{"post_image_id"=>"", "title"=>"Juan", "post_date(3i)"=>"4", "post_date(2i)"=>"6", "post_date(1i)"=>"2016", "author"=>"", "description"=>""}, "locale"=>:pt}
   (0.4ms)  SELECT MAX("refinery_posts"."position") FROM "refinery_posts"
  Refinery::Authentication::Devise::User Load (0.2ms)  SELECT  "refinery_authentication_devise_users".* FROM "refinery_authentication_devise_users" WHERE "refinery_authentication_devise_users"."id" = $1  ORDER BY "refinery_authentication_devise_users"."id" ASC LIMIT 1  [["id", 1]]
  Refinery::Authentication::Devise::Role Load (0.2ms)  SELECT "refinery_authentication_devise_roles".* FROM "refinery_authentication_devise_roles" INNER JOIN "refinery_authentication_devise_roles_users" ON "refinery_authentication_devise_roles"."id" = "refinery_authentication_devise_roles_users"."role_id" WHERE "refinery_authentication_devise_roles_users"."user_id" = $1  [["user_id", 1]]
   (0.2ms)  BEGIN
   (0.3ms)  ROLLBACK
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_error_messages.html.erb (0.6ms)
  Rendered vendor/extensions/posts/app/views/refinery/posts/admin/posts/_locale_picker.html.erb (2.5ms)
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_image_picker.html.erb (1.1ms)
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_wysiwyg.html.erb (0.4ms)
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_form_actions.html.erb (1.2ms)
  Rendered vendor/extensions/posts/app/views/refinery/posts/admin/posts/_form.html.erb (142.5ms)
  Rendered vendor/extensions/posts/app/views/refinery/posts/admin/posts/new.html.erb within layouts/refinery/admin (151.0ms)
  Rendered refinery/_html_tag.html.erb (1.0ms)
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_javascripts.html.erb (139.9ms)
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_head.html.erb (207.1ms)
  Rendered refinery/_site_bar.html.erb (0.7ms)
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_menu_item.html.erb (59.5ms)
  Rendered /Users/juan/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/refinerycms-9e79b276891f/core/app/views/refinery/admin/_menu.html.erb (89.9ms)
  Rendered refinery/_no_script.html.erb (0.3ms)
  Rendered refinery/_message.html.erb (0.1ms)

2 个答案:

答案 0 :(得分:1)

我不是翻译专家,但我认为有更快的方法。

在您的模型中,您可以拥有

translates :title, :description

这就足够了。我认为,整个语言环境选择器会在翻译模型字段时自动生成。

也可能有发电机,但我不确定。检查gitter响应。

答案 1 :(得分:0)

我假设您在某个默认选项卡上时没有收到此错误。

检查您的vendor / extensions / posts / config / locales文件夹中是否包含所需的所有语言。也许是因为它试图获取未设置的语言的翻译(或者只为一种语言添加的翻译专用关键字?)。

查看您的en.yml并检查是否在您要使用的所有其他语言文件中设置了该内容(es.yml和pt.yml)。

编辑:哦等等......那么图像区域内的错误呢?如果它试图获取图像标题或其他内容的翻译但是由于未设置图像而失败会怎么样?