From e87330cc1151db9ca91df792c492e661bc2c43be Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Sat, 1 Mar 2008 20:17:51 +0000 Subject: [PATCH] ActionItem181: do not set unsupported locales --- app/controllers/application.rb | 13 +++++++++++++ test/integration/locale_setting_test.rb | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 0 deletions(-) diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 4d6be3b..127776c 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -79,6 +79,19 @@ class ApplicationController < ActionController::Base end def set_system_locale + # don't allow unsupported locales + available_locales = Noosfero.locales.keys + if !available_locales.include?(GetText.locale.to_s) + # guess similar locales by using same language + similar = available_locales.find { |loc| GetText.locale.to_s.split('_').first == loc.split('_').first } + if similar + set_locale similar + else + set_locale(Noosfero.default_locale || 'en') + end + end + + # actually set the system locale lang = GetText.locale.to_s system_locale = if (lang == 'en') || lang.blank? diff --git a/test/integration/locale_setting_test.rb b/test/integration/locale_setting_test.rb index 9f6b025..927f65c 100644 --- a/test/integration/locale_setting_test.rb +++ b/test/integration/locale_setting_test.rb @@ -23,7 +23,23 @@ class LocaleSettingTest < ActionController::IntegrationTest # user now wants en get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' } assert_locale 'en' + end + + should 'not use unsupported browser-informed locale and use C instead' do + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'xx-yy, pt-br, en' } + assert_locale 'en' + end + should 'fallback to similar languages' do + # FIXME this assumes pt_PT is unsupported. If a pt_PT translation is added + # this test will break. + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt-pt, en' } + assert_locale 'pt_BR' + end + + should 'accept language without country code and pick a suitable language' do + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt, en'} + assert_locale 'pt_BR' end should 'be able to force locale' do -- libgit2 0.21.2