0

I am currently having a bit of a problem with Globalize gem.

I explain the current situation: I have a Model called Question. After creating it, without any data stored, I added the following lines to the model:

class Question < ActiveRecord::Base
  translates :wording, :answer1, :answer2, :answer3, :answer4
end

Then, I created a migration to create the translations table

class CreateTranslationsTable < ActiveRecord::Migration
    def up
        Question.create_translation_table! :wording => :string, :answer1 => :string, :answer2 => :string, :answer3 => :string, :answer4 => :string
    def end

    def down
        Question.drop_translation_table!
    def end

My default locale is :en. After that I added some data.

If I go execute rails c and put the command Question.first.wording everything works fine. Although when I execute in 'rails c' I18n.locale = :es and then I do Question.first.wording still displays the english text I put at the beginning.

I tried one thing which it seemed to help me is that I dropped all the translated columns (like specified in Globalize documentation after you migrated data. In my case I didn't have any data to migrate at the beginning though). After that I made a rollback (which got back the columns I deleted form the Question model), then executing Question.first.wording with I18n.locale = :es got it working. Which means that Question.first.wording returns nil.

After that, I implemented the 'Locale from Url Params' as specified in the Ruby on Rails guide Which means the first URL param si the ':locale' param. Now the current problem: The view still displays the information in English when it should display it in Spanish, since the URL I entered was http://localhost.com/es/questions/.

How can I make it to display in the view the Spanish information?

Hashirama Senju
  • 195
  • 1
  • 11

1 Answers1

0

My mistake. I interpreted from the documentation that the the chunck of code (in application_controller.rb) that works for setting the url:

def default_url_options(options={})
    { locale: params[:locale] }
end

would actually set the 'I18n.locale' variable. What I did is the next to get around this (in application_controller.rb):

before_action :change_to_current_locale 
def change_to_current_locale
    I18n.locale = params[:locale]
end

That made it work.

Hashirama Senju
  • 195
  • 1
  • 11