user1748009
user1748009

Reputation: 163

Rails not setting locale correctly

I have Rails app (3.2.12) and currently run internationalisation on the app ('en', 'es', 'it', 'de','fr').

The internationalisation is working within the app except when I signout of the app and reach the landing page. So for example, the landing pages:

http://localhost:3000/?locale=en
http://localhost:3000/?locale=es
http://localhost:3000/?locale=it
etc...

all default to Spanish translation 'es'. When I check for the locale set for each of the above pages I find that the locale is set correctly. So basically for say the English 'en' locale it shows Spanish translation.

I have also checked the YAML files and those are fine.

The closest match on SO for this problem is this question: Mixed locales in Rails i18n

I have tried the solutions in this SO answer but they don't seem to work for me.

Here is some of the relevant code related to Internationalisation.

Application Controller

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :set_locale

private

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
    #default_url_options[:locale] = params[:locale]
    #I18n.locale = params[:locale] || 'en'
  end

  def default_url_options
    { :locale => I18n.locale }
  end
end

Application.rb

 config.i18n.default_locale = :en
 config.i18n.available_locales = ["en", "it", "de", "es", "fr"]
 I18n.config.enforce_available_locales = true

Additionally, my app uses Devise and Active_admin.

Any idea as to what I might be doing wrong here?

Upvotes: 1

Views: 563

Answers (1)

alex
alex

Reputation: 3742

Try to debug set_locale method. Seems the locale is set by default, like params[:locale] is nil.

def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
    Rails.logger.debug "locale_params=#{params[:locale]}"
  end

Upvotes: 1

Related Questions