Commit 6ae357043b91f48a1eca88e1a4edac63b88b4daf
1 parent
28b264c7
Exists in
master
and in
29 other branches
ActionItem830: forcing single-supported locale
Showing
3 changed files
with
13 additions
and
7 deletions
Show diff stats
app/controllers/application.rb
... | ... | @@ -62,6 +62,9 @@ class ApplicationController < ActionController::Base |
62 | 62 | |
63 | 63 | before_init_gettext :maybe_save_locale |
64 | 64 | after_init_gettext :check_locale |
65 | + if Noosfero.available_locales.size == 1 | |
66 | + GetText.locale = Noosfero.available_locales.first | |
67 | + end | |
65 | 68 | init_gettext 'noosfero' |
66 | 69 | |
67 | 70 | include NeedsProfile |
... | ... | @@ -117,16 +120,19 @@ class ApplicationController < ActionController::Base |
117 | 120 | end |
118 | 121 | |
119 | 122 | def maybe_save_locale |
120 | - # save locale if forced | |
121 | - if params[:lang] | |
122 | - cookies[:lang] = params[:lang] | |
123 | + if Noosfero.available_locales.size > 1 | |
124 | + # save locale if forced | |
125 | + if params[:lang] | |
126 | + cookies[:lang] = params[:lang] | |
127 | + end | |
128 | + # force GetText to load a matching locale | |
129 | + GetText.locale = nil | |
123 | 130 | end |
124 | - # force GetText to load a matching locale | |
125 | - GetText.locale = nil | |
126 | 131 | end |
127 | 132 | |
128 | 133 | def check_locale |
129 | 134 | available_locales = Noosfero.available_locales |
135 | + return if available_locales.size == 1 | |
130 | 136 | |
131 | 137 | # do not accept unsupported locales |
132 | 138 | if !available_locales.include?(locale.to_s) | ... | ... |
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 | - Dir.glob(File.join(RAILS_ROOT, 'locale', '*')).map { |f| File.basename(f) }.select {|item| locales.include?(item) } | |
16 | + @available_locales ||= (Dir.glob(File.join(RAILS_ROOT, 'locale', '*')).map { |f| File.basename(f) }.select {|item| locales.include?(item) }) | |
17 | 17 | end |
18 | 18 | end |
19 | 19 | ... | ... |
test/integration/locale_setting_test.rb
... | ... | @@ -6,7 +6,7 @@ class LocaleSettingTest < ActionController::IntegrationTest |
6 | 6 | # reset GetText before every test |
7 | 7 | GetText.locale = nil |
8 | 8 | Noosfero.stubs(:default_locale).returns('en') |
9 | - Noosfero.stubs(:available_locales).returns(['pt_BR']) | |
9 | + Noosfero.stubs(:available_locales).returns(['pt_BR', 'ja_JP']) | |
10 | 10 | end |
11 | 11 | |
12 | 12 | should 'be able to set a default language' do | ... | ... |