Commit 3c321efd54feee1b0c46845a2fa1c14e6bffe59f
1 parent
5f61ee30
Exists in
master
and in
29 other branches
ActionItem830: always set a locale explicitly (or so)
The only exception is when the locale set by Ruby-GetText is included in the list of supported locales.
Showing
2 changed files
with
18 additions
and
22 deletions
Show diff stats
app/controllers/application.rb
| ... | ... | @@ -72,9 +72,6 @@ class ApplicationController < ActionController::Base |
| 72 | 72 | |
| 73 | 73 | before_init_gettext :maybe_save_locale |
| 74 | 74 | after_init_gettext :check_locale |
| 75 | - #if Noosfero.available_locales.size == 1 | |
| 76 | - #GetText.locale = Noosfero.available_locales.first | |
| 77 | - #end | |
| 78 | 75 | include GetText |
| 79 | 76 | init_gettext 'noosfero' |
| 80 | 77 | |
| ... | ... | @@ -133,29 +130,28 @@ class ApplicationController < ActionController::Base |
| 133 | 130 | if params[:lang] |
| 134 | 131 | cookies[:lang] = params[:lang] |
| 135 | 132 | end |
| 136 | - # force GetText to load a matching locale | |
| 137 | - GetText.locale = nil | |
| 138 | 133 | end |
| 139 | 134 | end |
| 140 | 135 | |
| 141 | 136 | def check_locale |
| 142 | - available_locales = Noosfero.available_locales | |
| 143 | - return if available_locales.size == 1 | |
| 144 | - | |
| 145 | - # do not accept unsupported locales | |
| 146 | - if !available_locales.include?(locale.to_s) | |
| 147 | - old_locale = locale.to_s | |
| 148 | - # find a similar locale | |
| 149 | - similar = available_locales.find { |loc| locale.to_s.split('_').first == loc.split('_').first } | |
| 150 | - if similar | |
| 151 | - set_locale similar | |
| 152 | - cookies[:lang] = similar | |
| 153 | - else | |
| 154 | - # no similar locale, fallback to default | |
| 155 | - set_locale(Noosfero.default_locale) | |
| 156 | - cookies[:lang] = Noosfero.default_locale | |
| 137 | + if Noosfero.available_locales.size == 1 | |
| 138 | + set_locale Noosfero.available_locales.first | |
| 139 | + else | |
| 140 | + # do not accept unsupported locales | |
| 141 | + if !Noosfero.available_locales.include?(locale.to_s) | |
| 142 | + old_locale = locale.to_s | |
| 143 | + # find a similar locale | |
| 144 | + similar = Noosfero.available_locales.find { |loc| locale.to_s.split('_').first == loc.split('_').first } | |
| 145 | + if similar | |
| 146 | + set_locale similar | |
| 147 | + cookies[:lang] = similar | |
| 148 | + else | |
| 149 | + # no similar locale, fallback to default | |
| 150 | + set_locale(Noosfero.default_locale) | |
| 151 | + cookies[:lang] = Noosfero.default_locale | |
| 152 | + end | |
| 153 | + RAILS_DEFAULT_LOGGER.info('Locale reverted from %s to %s' % [old_locale, locale]) | |
| 157 | 154 | end |
| 158 | - RAILS_DEFAULT_LOGGER.info('Locale reverted from %s to %s' % [old_locale, locale]) | |
| 159 | 155 | end |
| 160 | 156 | end |
| 161 | 157 | ... | ... |
lib/noosfero.rb
| ... | ... | @@ -13,7 +13,7 @@ module Noosfero |
| 13 | 13 | attr_accessor :locales |
| 14 | 14 | attr_accessor :default_locale |
| 15 | 15 | def available_locales |
| 16 | - @available_locales ||= (Dir.glob(File.join(RAILS_ROOT, 'locale', '*')).map { |f| File.basename(f) }.select {|item| locales.include?(item) }) | |
| 16 | + @available_locales ||= locales.keys | |
| 17 | 17 | end |
| 18 | 18 | def each_locale |
| 19 | 19 | locales.keys.sort.each do |key| | ... | ... |