Rails 4 and globalize not adding translation

2.4k views Asked by At

I upgraded to Rails 4 and globalize (instead of globalize3) and translations are not working correctly since.

Steps:

create english record
change the locale to :es
then call update_attributes on just created object

This used to create a new translation in es.
But, now, it is modifying the English record instead!!! Please help?

Gemfile:

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

Model:

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

Controller:

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
1

There are 1 answers

0
retro On

Try this method:

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