I’m trying to build a language selector into my rails app. The
selector is defined within the application layout and upon language
selection the current view should be updated to the selected language.
The selected language should then be passed to all subsequent
requests , the url should take the following form:
http://localhost:300/fr/producers
where the selected locale is systematically part of the url.
I have a problem with the selector that does not automatically
‘refresh’ the view to the selected language upon language selection.
Can somebody help me on this one ?
Here is the relevant parts of the code:
#application_controller.rb :
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale
def set_locale
I18n.locale = params[:locale]
end
def default_url_options(options={})
{ :locale => I18n.locale }
end
end
#The layout application.html.erb:
Test <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %> <%= form_tag '', :method => 'GET' do %> <%= select_tag :locale, options_for_select(LANGUAGES, I18n.locale), :onchange => 'this.form.submit();' %> <% end %><%= yield %>
#routes.rb:
scope “(/:locale)” do
resources :producers do
collection do
get ‘update_regions’
end
end
#config/application.rb:
config.i18n.default_locale = :de