加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Rails 4并全球化不添加翻译

发布时间:2020-12-17 02:05:40 所属栏目:百科 来源:网络整理
导读:我升级到Rails 4并全球化(而不是globalize3),因此翻译无法正常工作. 脚步: 创造英文记录 将语言环境更改为:es 然后在刚刚创建的对象上调用update_attributes 这用于在es中创建新的翻译. 但是,现在,它正在修改英文记录!请帮忙? 的Gemfile: gem "rails","
我升级到Rails 4并全球化(而不是globalize3),因此翻译无法正常工作.

脚步:

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

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

的Gemfile:

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

解决方法

试试这个方法:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读