diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 673d5ba..4d6be3b 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -27,6 +27,7 @@ class ApplicationController < ActionController::Base init_gettext 'noosfero' before_init_gettext :force_language + after_init_gettext :set_system_locale include NeedsProfile @@ -72,7 +73,20 @@ class ApplicationController < ActionController::Base cookies[:lang] = lang end - set_locale lang unless lang.blank? + unless lang.blank? + set_locale lang + end + end + + def set_system_locale + lang = GetText.locale.to_s + system_locale = + if (lang == 'en') || lang.blank? + 'C' + else + ('%s.utf8' % lang) + end + Locale.setlocale(Locale::LC_ALL, system_locale) end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4d24b59..99f2707 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -394,10 +394,12 @@ module ApplicationHelper end + # formats a date for displaying. def show_date(date) date.strftime(_('%d %B %Y')) end + # formats a datetime for displaying. def show_time(time) time.strftime(_('%d %B %Y, %H:%m')) end diff --git a/config/environment.rb b/config/environment.rb index d4c6d96..d72fee9 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -82,6 +82,7 @@ Noosfero.locales = { 'en' => 'English', 'pt_BR' => 'Português Brasileiro', } +require 'locale' Tag.hierarchical = true diff --git a/test/integration/locale_setting_test.rb b/test/integration/locale_setting_test.rb index 2f6e885..9f6b025 100644 --- a/test/integration/locale_setting_test.rb +++ b/test/integration/locale_setting_test.rb @@ -11,18 +11,18 @@ class LocaleSettingTest < ActionController::IntegrationTest Noosfero.expects(:default_locale).returns('pt_BR').at_least_once get '/' - assert_equal 'pt_BR', GetText.locale.to_s + assert_locale 'pt_BR' 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 + assert_locale 'pt_BR' # user now wants en get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' } - assert_equal 'en', GetText.locale.to_s + assert_locale 'en' end @@ -30,22 +30,38 @@ class LocaleSettingTest < ActionController::IntegrationTest # set locale to pt_BR get '/', :lang => 'pt_BR' - assert_equal 'pt_BR', GetText.locale.to_s + assert_locale 'pt_BR' # locale is kept get '/' - assert_equal 'pt_BR', GetText.locale.to_s + assert_locale 'pt_BR' # changing back get '/', :lang => 'en' - assert_equal 'en', GetText.locale.to_s + assert_locale 'en' # locale is kept again get '/' - assert_equal 'en', GetText.locale.to_s + assert_locale 'en' end + protected + def assert_locale(locale) + gettext_locale = GetText.locale.to_s + ok("Ruby-GetText locale should be #{locale}, but was #{gettext_locale}") { locale == gettext_locale } + # TODO this test depends on a unpublished patch to liblocale-ruby + #system_locale = Locale.getlocale + #wanted_system_locale = + # if locale == 'en' + # 'C' + # else + # '%s.utf8' % locale + # end + + #ok("System locale should be #{wanted_system_locale}, but was #{system_locale}") { wanted_system_locale == system_locale } + end + end -- libgit2 0.21.2