Commit e87330cc1151db9ca91df792c492e661bc2c43be
1 parent
669c063d
Exists in
staging
and in
42 other branches
ActionItem181: do not set unsupported locales
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1545 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
29 additions
and
0 deletions
Show diff stats
app/controllers/application.rb
| ... | ... | @@ -79,6 +79,19 @@ class ApplicationController < ActionController::Base |
| 79 | 79 | end |
| 80 | 80 | |
| 81 | 81 | def set_system_locale |
| 82 | + # don't allow unsupported locales | |
| 83 | + available_locales = Noosfero.locales.keys | |
| 84 | + if !available_locales.include?(GetText.locale.to_s) | |
| 85 | + # guess similar locales by using same language | |
| 86 | + similar = available_locales.find { |loc| GetText.locale.to_s.split('_').first == loc.split('_').first } | |
| 87 | + if similar | |
| 88 | + set_locale similar | |
| 89 | + else | |
| 90 | + set_locale(Noosfero.default_locale || 'en') | |
| 91 | + end | |
| 92 | + end | |
| 93 | + | |
| 94 | + # actually set the system locale | |
| 82 | 95 | lang = GetText.locale.to_s |
| 83 | 96 | system_locale = |
| 84 | 97 | if (lang == 'en') || lang.blank? | ... | ... |
test/integration/locale_setting_test.rb
| ... | ... | @@ -23,7 +23,23 @@ class LocaleSettingTest < ActionController::IntegrationTest |
| 23 | 23 | # user now wants en |
| 24 | 24 | get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'en' } |
| 25 | 25 | assert_locale 'en' |
| 26 | + end | |
| 27 | + | |
| 28 | + should 'not use unsupported browser-informed locale and use C instead' do | |
| 29 | + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'xx-yy, pt-br, en' } | |
| 30 | + assert_locale 'en' | |
| 31 | + end | |
| 26 | 32 | |
| 33 | + should 'fallback to similar languages' do | |
| 34 | + # FIXME this assumes pt_PT is unsupported. If a pt_PT translation is added | |
| 35 | + # this test will break. | |
| 36 | + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt-pt, en' } | |
| 37 | + assert_locale 'pt_BR' | |
| 38 | + end | |
| 39 | + | |
| 40 | + should 'accept language without country code and pick a suitable language' do | |
| 41 | + get '/', { }, { 'HTTP_ACCEPT_LANGUAGE' => 'pt, en'} | |
| 42 | + assert_locale 'pt_BR' | |
| 27 | 43 | end |
| 28 | 44 | |
| 29 | 45 | should 'be able to force locale' do | ... | ... |