7

My Rails App is on rails 4.0.2 and I have a problem switching translations with the locale variable and the params[:locale] from the url scheme following the official rails guide. I have a single page website at my site.

My routes for Internationalization:

scope "(:locale)", locale: /en|de/ do
  #my routes here
end

My application controller

before_filter :set_locale
  def set_locale
     I18n.locale = params[:locale] || I18n.default_locale
     #Rails.application.routes.default_url_options[:locale]= I18n.locale
  end

  # app/controllers/application_controller.rb
  def default_url_options(options = {})
     { locale: I18n.locale }.merge options
  end

The links to change the locale variables in the view:

<%= link_to_unless I18n.locale == :en, "English", locale: :en %>
|
<%= link_to_unless I18n.locale == :de, "Deutsch", locale: :de %>

What happens: the locale variable is set correctly but the translations are not switching. If I remove one of the translation files (currently for english and german) the languages switches to the remaining translation file. When I put back the other translation file and try to switch to it by changing the locale variable it never switches to the other language.

Why is my code not changing the translations?

phs
  • 10,072
  • 3
  • 54
  • 80
nuxxxx
  • 223
  • 2
  • 13
  • I hope you can patch that because Rails 4.0.2 has some serious security problems, and using 4.0.13 is highly recommended. A tool like [Gem Canary](https://gemcanary.com/) can give you a heads up on problems like this before they become trouble. – tadman May 08 '15 at 18:18
  • 1
    I patched the gemset rails 4.0.13, thanks for the advice. – nuxxxx May 09 '15 at 17:41

2 Answers2

2

I had the same issues and maybe it would be a solution for you:

in routes.rb change

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
  #your routes here
end
get '*path', to: redirect("/#{I18n.default_locale}/%{path}")
get '', to: redirect("/#{I18n.default_locale}")

in application_controller.rb

def set_locale
  I18n.locale = params[:locale] if params[:locale].present?
end

def default_url_options(options = {})
  {locale: I18n.locale}
end

p.s.

in config/locales/en.yml something like this:

en:
  languages:
    en: "English"
    de: "Deutsch"

and in config/locales/de.yml in German

in view

<%= link_to_unless_current t('languages.en'), locale: :en %>
|
<%= link_to_unless_current t('languages.de'), locale: :de %>
Igor Ivancha
  • 3,231
  • 4
  • 28
  • 39
  • Your code gives me a output buffer error: `syntax error, unexpected ',', expecting :: or '[' or '.' ...k_to_unless_current, "English", locale: :en );@output_buffer...` – nuxxxx May 09 '15 at 16:31
  • 1
    ok! you need to edit `config/locales/en.yml` `en: languages: en: "English" de: "Deutsch" ` and in `config/locales/de.yml` in German than in view: | – Igor Ivancha May 09 '15 at 17:22
0

I think you need to define the constraint on the locale more explicit:

scope path: '(:locale)', constraints: { locale: /en|de/ } do
  # routes you want to localize
end
spickermann
  • 81,597
  • 8
  • 83
  • 112