Commits

Anonymous committed b44d1ce

Cache languages names to avoid loading all translations files.

Comments (0)

Files changed (3)

app/helpers/application_helper.rb

   end
 
   def lang_options_for_select(blank=true)
-    (blank ? [["(auto)", ""]] : []) +
-      valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
+    (blank ? [["(auto)", ""]] : []) + languages_options
   end
 
   def label_tag_for(name, option_tags = nil, options = {})

lib/redmine/i18n.rb

       ::I18n.available_locales
     end
 
+    # Returns an array of languages names and code sorted by names, example:
+    # [["Deutsch", "de"], ["English", "en"] ...]
+    #
+    # The result is cached to prevent from loading all translations files.
+    def languages_options
+      ActionController::Base.cache_store.fetch "i18n/languages_options" do
+        valid_languages.map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.sort {|x,y| x.first <=> y.first }
+      end      
+    end
+
     def find_language(lang)
       @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
       @@languages_lookup[lang.to_s.downcase]

test/unit/lib/redmine/i18n_test.rb

     assert valid_languages.first.is_a?(Symbol)
   end
 
+  def test_languages_options
+    options = languages_options
+
+    assert options.is_a?(Array)
+    assert_equal valid_languages.size, options.size
+    assert_nil options.detect {|option| !option.is_a?(Array)}
+    assert_nil options.detect {|option| option.size != 2}
+    assert_nil options.detect {|option| !option.first.is_a?(String) || !option.last.is_a?(String)}
+    assert_include ["English", "en"], options
+  end
+
   def test_locales_validness
     lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
     assert_equal lang_files_count, valid_languages.size