【问题标题】:Rails 4 and globalize not adding translationRails 4和全球化不添加翻译
【发布时间】:2013-12-28 11:56:18
【问题描述】:

我升级到 Rails 4 并进行了 globalize(而不是 globalize3),此后翻译无法正常工作。

步骤:

创建英文记录
将语言环境更改为 :es
然后在刚刚创建的对象上调用 update_attributes

这用于在 es 中创建一个新的翻译。
但是,现在,它正在修改英文记录!请帮忙?

宝石文件:

gem "rails", "4.0.1"
gem "globalize", "~> 4.0.0.alpha.1"

型号:

class GlossaryTerm < ActiveRecord::Base
 translates :term, :definition
 accepts_nested_attributes_for :translations

 has_many :glossary_term_translations
 belongs_to :section
 validates_presence_of :term
 validates_presence_of :definition

 **after_create :create_spanish_placeholder**

 def create_spanish_placeholder
   term = self.term
   definition = self.definition
   I18n.locale = :es
   self.update_attributes(:term => "SPANISH placeholder for #{term}", :definition => "SPANISH placeholder for #{definition}")
   I18n.locale = :en
 end

#...

end

控制器:

class Admin::GlossaryTermsController < ApplicationController
before_filter :authorize_sysadmin!

def create
  find_models
  @glossary_term = @section.glossary_terms.new(glossary_term_params)
  if @glossary_term.save
    redirect_to edit_admin_section_url(@section, program_id: @program.id)
  else
    render :action => "new"
  end
end

#...

 protected

 def glossary_term_params
   params.require(:glossary_term).permit(:term, :definition, :glossary_image,   :glossary_image_file_name,
                                    :translations_attributes => [:id, :term, :definition])
 end

 #...

 end

【问题讨论】:

  • 你试过gem 'globalize', '~&gt; 4.0.0.alpha.2'了吗?
  • 您找到解决方案了吗?如果可以,你能回答你的问题吗?

标签: ruby ruby-on-rails-4 rails-i18n globalize


【解决方案1】:

试试这个方法:

def create_spanish_placeholder
  term = self.term
  definition = self.definition
  Globalize.with_locale(:es) do
    self.update_attributes(:term => "SPANISH placeholder for #{term}", :definition => "SPANISH placeholder for #{definition}")
  end
end

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 2015-10-16
    相关资源
    最近更新 更多