Skip to content

Commit

Permalink
cache country_translations in Thread.current
Browse files Browse the repository at this point in the history
  • Loading branch information
durandom committed Dec 13, 2013
1 parent 5abe3de commit 2ad3895
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/i18n_country_select/instance_tag.rb
Expand Up @@ -10,13 +10,6 @@ def to_country_code_select_tag(priority_countries, options = {}, html_options =
def country_code_select(priority_countries, options, html_options)
selected = object.send(@method_name) if object.respond_to?(@method_name)

country_translations = country_translations = COUNTRY_CODES.map do |code|
translation = I18n.t(code, :scope => :countries, :default => 'missing')
translation == 'missing' ? nil : [translation, code]
end.compact.sort_by do |translation, code|
normalize_translation(translation)
end

countries = ""

if options.present? and options[:include_blank]
Expand All @@ -37,6 +30,18 @@ def country_code_select(priority_countries, options, html_options)
content_tag(:select, countries.html_safe, html_options)
end

def country_translations
Thread.current[:country_translations] ||= {}
Thread.current[:country_translations][I18n.locale] ||= begin
COUNTRY_CODES.map do |code|
translation = I18n.t(code, :scope => :countries, :default => 'missing')
translation == 'missing' ? nil : [translation, code]
end.compact.sort_by do |translation, code|
normalize_translation(translation)
end
end
end

private
def normalize_translation(translation)
UnicodeUtils.canonical_decomposition(translation).split('').select do |c|
Expand Down

0 comments on commit 2ad3895

Please sign in to comment.