diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 1d2d454..db163f2 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -26,7 +26,7 @@ class ApplicationController < ActionController::Base include PermissionCheck init_gettext 'noosfero' - before_init_gettext :set_locale + before_init_gettext :force_language include NeedsProfile @@ -62,15 +62,11 @@ class ApplicationController < ActionController::Base current_user.person if logged_in? end - def set_locale - locale = cookies[:locale] - unless params[:locale].blank? - locale = params[:locale] - end - - if locale - cookies[:locale] = locale - GetText.locale = locale + def force_language + lang = params[:lang] + unless lang.blank? + cookies[:lang] = lang + GetText.locale = lang end end diff --git a/app/views/shared/language_chooser.rhtml b/app/views/shared/language_chooser.rhtml index 28847d4..7deb8d1 100644 --- a/app/views/shared/language_chooser.rhtml +++ b/app/views/shared/language_chooser.rhtml @@ -2,6 +2,6 @@ <% if GetText.locale.to_s == locale_code %> <%= locale_name %> <% else %> - <%= link_to locale_name, { :locale => locale_code }%> + <%= link_to locale_name, { :lang => locale_code }%> <% end %> <% end %> diff --git a/test/integration/locale_setting_test.rb b/test/integration/locale_setting_test.rb index ddbc6c6..ecdd0b1 100644 --- a/test/integration/locale_setting_test.rb +++ b/test/integration/locale_setting_test.rb @@ -2,10 +2,27 @@ require "#{File.dirname(__FILE__)}/../test_helper" class LocaleSettingTest < ActionController::IntegrationTest - should 'set locale properly' do + def setup + # reset GetText before every test + GetText.locale = nil + end + + should 'detect locale from the browser' do + + # user has pt_BR + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt-br, en' } + assert_equal 'pt_BR', GetText.locale.to_s + + # user now wants en + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' } + assert_equal 'en', GetText.locale.to_s + + end + + should 'be able to force locale' do # set locale to pt_BR - get '/', :locale => 'pt_BR' + get '/', :lang => 'pt_BR' assert_equal 'pt_BR', GetText.locale.to_s # locale is kept @@ -13,7 +30,7 @@ class LocaleSettingTest < ActionController::IntegrationTest assert_equal 'pt_BR', GetText.locale.to_s # changing back - get '/', :locale => 'en' + get '/', :lang => 'en' assert_equal 'en', GetText.locale.to_s # locale is kept again @@ -23,4 +40,5 @@ class LocaleSettingTest < ActionController::IntegrationTest end + end -- libgit2 0.21.2