Commit b44f870cb472eae5120ba958c030d7edfc81a4a3
1 parent
500d554f
Exists in
master
and in
22 other branches
ActionItem172: rewriting locale handling code, now we are going to trust a littl…
…e bit more in Ruby-GetText. git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1565 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
3 changed files
with
35 additions
and
31 deletions
Show diff stats
app/controllers/application.rb
| ... | ... | @@ -25,9 +25,9 @@ class ApplicationController < ActionController::Base |
| 25 | 25 | include AuthenticatedSystem |
| 26 | 26 | include PermissionCheck |
| 27 | 27 | |
| 28 | + before_init_gettext :maybe_save_locale | |
| 29 | + after_init_gettext :check_locale | |
| 28 | 30 | init_gettext 'noosfero' |
| 29 | - before_init_gettext :force_language | |
| 30 | - after_init_gettext :set_system_locale | |
| 31 | 31 | |
| 32 | 32 | include NeedsProfile |
| 33 | 33 | |
| ... | ... | @@ -40,7 +40,7 @@ class ApplicationController < ActionController::Base |
| 40 | 40 | verify :method => :post, :only => actions, :redirect_to => redirect |
| 41 | 41 | end |
| 42 | 42 | |
| 43 | - protected | |
| 43 | + protected | |
| 44 | 44 | |
| 45 | 45 | # TODO: move this logic somewhere else (Domain class?) |
| 46 | 46 | def detect_stuff_by_domain |
| ... | ... | @@ -63,43 +63,43 @@ class ApplicationController < ActionController::Base |
| 63 | 63 | current_user.person if logged_in? |
| 64 | 64 | end |
| 65 | 65 | |
| 66 | - def force_language | |
| 67 | - lang = params[:lang] | |
| 68 | - if lang.blank? | |
| 69 | - # no language forced, get language from cookie | |
| 70 | - lang = cookies[:lang] || Noosfero.default_locale | |
| 71 | - else | |
| 72 | - # save forced language in the cookie | |
| 73 | - cookies[:lang] = lang | |
| 74 | - end | |
| 75 | - | |
| 76 | - unless lang.blank? | |
| 77 | - set_locale lang | |
| 66 | + def maybe_save_locale | |
| 67 | + # save locale if forced | |
| 68 | + if params[:lang] | |
| 69 | + cookies[:lang] = params[:lang] | |
| 78 | 70 | end |
| 71 | + # force GetText to load a matching locale | |
| 72 | + GetText.locale = nil | |
| 79 | 73 | end |
| 80 | 74 | |
| 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 } | |
| 75 | + def check_locale | |
| 76 | + available_locales = Noosfero.available_locales | |
| 77 | + | |
| 78 | + # do not accept unsupported locales | |
| 79 | + if !available_locales.include?(locale.to_s) | |
| 80 | + old_locale = locale.to_s | |
| 81 | + # find a similar locale | |
| 82 | + similar = available_locales.find { |loc| locale.to_s.split('_').first == loc.split('_').first } | |
| 87 | 83 | if similar |
| 88 | 84 | set_locale similar |
| 85 | + cookies[:lang] = similar | |
| 89 | 86 | else |
| 90 | - set_locale(Noosfero.default_locale || 'en') | |
| 87 | + # no similar locale, fallback to default | |
| 88 | + set_locale(Noosfero.default_locale) | |
| 89 | + cookies[:lang] = Noosfero.default_locale | |
| 91 | 90 | end |
| 91 | + RAILS_DEFAULT_LOGGER.info('Locale reverted from %s to %s' % [old_locale, locale]) | |
| 92 | 92 | end |
| 93 | 93 | |
| 94 | - # actually set the system locale | |
| 95 | - lang = GetText.locale.to_s | |
| 96 | - system_locale = | |
| 97 | - if (lang == 'en') || lang.blank? | |
| 98 | - 'C' | |
| 99 | - else | |
| 100 | - ('%s.utf8' % lang) | |
| 101 | - end | |
| 102 | - Locale.setlocale(Locale::LC_ALL, system_locale) | |
| 94 | + # now set the system locale | |
| 95 | + system_locale = '%s.utf8' % locale | |
| 96 | + begin | |
| 97 | + Locale.setlocale(Locale::LC_ALL, system_locale) | |
| 98 | + rescue Exception => e | |
| 99 | + # fallback to C | |
| 100 | + RAILS_DEFAULT_LOGGER.info("Locale #{system_locale} not available, falling back to the portable \"C\" locale (consider installing the #{system_locale} locale in your system)") | |
| 101 | + Locale.setlocale(Locale::LC_ALL, 'C') | |
| 102 | + end | |
| 103 | 103 | end |
| 104 | 104 | |
| 105 | 105 | end | ... | ... |
config/environment.rb
test/integration/locale_setting_test.rb
| ... | ... | @@ -5,6 +5,8 @@ class LocaleSettingTest < ActionController::IntegrationTest |
| 5 | 5 | def setup |
| 6 | 6 | # reset GetText before every test |
| 7 | 7 | GetText.locale = nil |
| 8 | + Noosfero.stubs(:default_locale).returns('en') | |
| 9 | + Noosfero.stubs(:available_locales).returns(['pt_BR']) | |
| 8 | 10 | end |
| 9 | 11 | |
| 10 | 12 | should 'be able to set a default language' do | ... | ... |