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,9 +72,6 @@ class ApplicationController < ActionController::Base | ||
72 | 72 | ||
73 | before_init_gettext :maybe_save_locale | 73 | before_init_gettext :maybe_save_locale |
74 | after_init_gettext :check_locale | 74 | after_init_gettext :check_locale |
75 | - #if Noosfero.available_locales.size == 1 | ||
76 | - #GetText.locale = Noosfero.available_locales.first | ||
77 | - #end | ||
78 | include GetText | 75 | include GetText |
79 | init_gettext 'noosfero' | 76 | init_gettext 'noosfero' |
80 | 77 | ||
@@ -133,29 +130,28 @@ class ApplicationController < ActionController::Base | @@ -133,29 +130,28 @@ class ApplicationController < ActionController::Base | ||
133 | if params[:lang] | 130 | if params[:lang] |
134 | cookies[:lang] = params[:lang] | 131 | cookies[:lang] = params[:lang] |
135 | end | 132 | end |
136 | - # force GetText to load a matching locale | ||
137 | - GetText.locale = nil | ||
138 | end | 133 | end |
139 | end | 134 | end |
140 | 135 | ||
141 | def check_locale | 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 | end | 154 | end |
158 | - RAILS_DEFAULT_LOGGER.info('Locale reverted from %s to %s' % [old_locale, locale]) | ||
159 | end | 155 | end |
160 | end | 156 | end |
161 | 157 |
lib/noosfero.rb
@@ -13,7 +13,7 @@ module Noosfero | @@ -13,7 +13,7 @@ module Noosfero | ||
13 | attr_accessor :locales | 13 | attr_accessor :locales |
14 | attr_accessor :default_locale | 14 | attr_accessor :default_locale |
15 | def available_locales | 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 | end | 17 | end |
18 | def each_locale | 18 | def each_locale |
19 | locales.keys.sort.each do |key| | 19 | locales.keys.sort.each do |key| |