Cache languages names to avoid loading all translations files.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10844 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-11-18 18:01:24 +00:00
parent 3739810afa
commit a49de1c39d
3 changed files with 22 additions and 2 deletions

View File

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

View File

@ -70,6 +70,16 @@ module Redmine
::I18n.available_locales ::I18n.available_locales
end 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) def find_language(lang)
@@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
@@languages_lookup[lang.to_s.downcase] @@languages_lookup[lang.to_s.downcase]

View File

@ -194,6 +194,17 @@ class Redmine::I18nTest < ActiveSupport::TestCase
assert valid_languages.first.is_a?(Symbol) assert valid_languages.first.is_a?(Symbol)
end 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 def test_locales_validness
lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size lang_files_count = Dir["#{Rails.root}/config/locales/*.yml"].size
assert_equal lang_files_count, valid_languages.size assert_equal lang_files_count, valid_languages.size