简体   繁体   English

Globalize2的多语言管理员

[英]Multilingual admin with Globalize2

I have a multilingual admin (for English and Portuguese) that needs to save data for both languages at the same time, like Description EN field and Description PT field in the same form. 我有一个多语言的管理员(适用于英语和葡萄牙语),需要同时保存两种语言的数据,例如以相同的形式描述EN字段和PT字段。

Globalize2 makes some magic and I don't know exactly how to save this. Globalize2带来了一些魔力,我不知道该如何保存。 I'll post my controller action here, that obviously needs some refactoring. 我将在此处发布控制器操作,显然需要进行一些重构。 Thanks, people! 谢谢大家!

def create
  @brand = Brand.create()
  @brand.title = params[:title]
  @brand.upload_logo(params[:logo]) unless params[:logo].blank?
  @brand.order = params[:order]
  @brand.priority = params[:priority]

  plataforms = Plataform.find(:all, :conditions => ["id IN (?)", params[:plataforms]])
  @brand.plataforms = plataforms

  params[:pt].each do |k, v|
    I18n.locale = :pt
    eval "@brand.#{k} = v"
  end

  params[:en].each do |k, v|
    I18n.locale = :en
    eval "@brand.#{k} = v"  
  end

  respond_to do |format|
    if @brand.save
    # if 1 == 1
      flash[:notice] = 'Brand was successfully created.'
      format.html { redirect_to(@brand) }
      format.xml  { render :xml => @brand, :status => :created, :location => @brand }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @brand.errors, :status => :unprocessable_entity }
    end
  end
end

Sorry.. I've forgotten to say.. My doubt is about saving the translations. 对不起..我忘了说..我的疑问是关于保存翻译。 This code: 这段代码:

params[:pt].each do |k, v|
  I18n.locale = :pt
  eval "@brand.#{k} = v"
end

params[:en].each do |k, v|
  I18n.locale = :en
  eval "@brand.#{k} = v"  
end

Doing globalization using i18n is great and simple. 使用i18n进行全球化非常简单。

For instance : 例如 :

flash[:notice] = t 'Brand was successfully created'

Then create a en.yml in config/locales : 然后在config / locales中创建一个en.yml:

en:
 Brand was successfully created: "Brand was successfully created"

And the portugese version as well following the same convention : 葡萄牙语版本也遵循相同的约定:

pt: (not sure about the country code)
Brand was successfully created: "bla bla bla I don't speak portugese"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM