diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 048b7ae6f..f78dbb965 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -962,8 +962,7 @@ module ApplicationHelper 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 = {}) diff --git a/lib/redmine/i18n.rb b/lib/redmine/i18n.rb index 304175ee5..e48db09b4 100644 --- a/lib/redmine/i18n.rb +++ b/lib/redmine/i18n.rb @@ -70,6 +70,16 @@ module Redmine ::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] diff --git a/test/unit/lib/redmine/i18n_test.rb b/test/unit/lib/redmine/i18n_test.rb index 8d31821fe..21a8395b3 100644 --- a/test/unit/lib/redmine/i18n_test.rb +++ b/test/unit/lib/redmine/i18n_test.rb @@ -194,6 +194,17 @@ class Redmine::I18nTest < ActiveSupport::TestCase 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