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.
Globalize2 makes some magic and I don't know exactly how to save this. 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 开发者_JAVA百科= :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.
For instance :
flash[:notice] = t 'Brand was successfully created'
Then create a en.yml in config/locales :
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"
精彩评论