Commit cb10c0e1d05a3384ab18b12b57b6260c59a66bd9
1 parent
091b7cf1
Exists in
master
and in
29 other branches
Fixed language_chooser to avoid crashes of themes
Showing
2 changed files
with
21 additions
and
4 deletions
Show diff stats
app/helpers/language_helper.rb
... | ... | @@ -13,19 +13,20 @@ module LanguageHelper |
13 | 13 | |
14 | 14 | alias :calendar_date_select_language :tinymce_language |
15 | 15 | |
16 | - def language_chooser(environment, options = {}) | |
17 | - return if environment.locales.size < 2 | |
16 | + def language_chooser(environment=nil, options = {}) | |
17 | + locales = environment.nil? ? Noosfero.locales : environment.locales | |
18 | + return if locales.size < 2 | |
18 | 19 | current = language |
19 | 20 | separator = options[:separator] || ' — ' |
20 | 21 | |
21 | 22 | if options[:element] == 'dropdown' |
22 | 23 | select_tag('lang', |
23 | - options_for_select(environment.locales.map{|code,name| [name, code]}, current), | |
24 | + options_for_select(locales.map{|code,name| [name, code]}, current), | |
24 | 25 | :onchange => "document.location.href= #{url_for(params.merge(:lang => 'LANGUAGE')).inspect}.replace(/LANGUAGE/, this.value) ;", |
25 | 26 | :help => _('The language you choose here is the language used for options, buttons, etc. It does not affect the language of the content created by other users.') |
26 | 27 | ) |
27 | 28 | else |
28 | - languages = environment.locales.map do |code,name| | |
29 | + languages = locales.map do |code,name| | |
29 | 30 | if code == current |
30 | 31 | content_tag('strong', name) |
31 | 32 | else | ... | ... |
test/unit/language_helper_test.rb
... | ... | @@ -65,6 +65,22 @@ class LanguageHelperTest < ActiveSupport::TestCase |
65 | 65 | assert result.blank? |
66 | 66 | end |
67 | 67 | |
68 | + should 'get noosfero locales if environment is not defined' do | |
69 | + self.expects(:language).returns('en') | |
70 | + Noosfero.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro' }).at_least_once | |
71 | + result = self.language_chooser | |
72 | + assert_match /Português Brasileiro/, result | |
73 | + assert_match /English/, result | |
74 | + end | |
75 | + | |
76 | + should 'get noosfero locales if environment is not defined and has options' do | |
77 | + self.expects(:language).returns('en') | |
78 | + Noosfero.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro' }).at_least_once | |
79 | + result = self.language_chooser(nil, :separator=>"<span class=\"language-separator\"/>") | |
80 | + assert_match /Português Brasileiro/, result | |
81 | + assert_match /English/, result | |
82 | + end | |
83 | + | |
68 | 84 | protected |
69 | 85 | include NoosferoTestHelper |
70 | 86 | include ActionView::Helpers::FormOptionsHelper | ... | ... |