Skip to content

Commit

Permalink
Bring translations lookup fallback to a lower level so at any point a…
Browse files Browse the repository at this point in the history
… user can ask for a `pt-br` style locale.

TODO: verify handling of capitalization with rails locales.
  • Loading branch information
rposborne committed Sep 20, 2016
1 parent dfd5c18 commit eb4ba56
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/countries/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def local_names
ISO3166.configuration.locales = (ISO3166.configuration.locales + languages.map(&:to_sym)).uniq
reload

@local_names ||= languages.map { |language| translations[language] || translations[language.sub(/-.*/, '')]}
@local_names ||= languages.map { |language| translations[language] }
end

def local_name
Expand Down
7 changes: 4 additions & 3 deletions lib/countries/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def call
end

class << self

def register(data)
alpha2 = data[:alpha2].upcase
@@registered_data[alpha2] = \
data.inject({}) { |a,(k,v)| a[k.to_s] = v; a }
data.inject({}) { |a,(k,v)| a[k.to_s] = v; a }
@@registered_data[alpha2]["translations"] = \
Translations.new.merge(data[:translations] || {})
@@cache = cache.merge(@@registered_data)
end

Expand Down Expand Up @@ -105,7 +106,7 @@ def loaded_locales
def load_translations(locale)
locale_names = load_cache(['cache', 'locales', "#{locale}.json"])
internal_codes.each do |alpha2|
@@cache[alpha2]['translations'] ||= {}
@@cache[alpha2]['translations'] ||= Translations.new
@@cache[alpha2]['translations'][locale] = locale_names[alpha2].freeze
@@cache[alpha2]['translated_names'] = @@cache[alpha2]['translations'].values.freeze
end
Expand Down
1 change: 1 addition & 0 deletions lib/countries/iso3166.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'countries/configuration'
require 'countries/data'
require 'countries/structure'
require 'countries/translations'
require 'countries/country/class_methods'
require 'countries/country/emoji'
require 'countries/country'
Expand Down
7 changes: 7 additions & 0 deletions lib/countries/translations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ISO3166
class Translations < Hash
def [](locale)
super(locale) || super(locale.sub(/-.*/, ""))
end
end
end

0 comments on commit eb4ba56

Please sign in to comment.