Class: SgtnClient::LocaleUtil
- Inherits:
-
Object
- Object
- SgtnClient::LocaleUtil
- Defined in:
- lib/sgtn-client/util/locale-util.rb
Overview
:nodoc:
Constant Summary collapse
- MAP_LOCALES =
{ 'zh-cn' => 'zh-hans', 'zh-tw' => 'zh-hant', 'zh-hans-cn' => 'zh-hans', 'zh-hant-tw' => 'zh-hant' }.freeze
- LOCALE_SEPARATOR =
'-'- EN_LOCALE =
'en'
Class Method Summary collapse
- .get_best_locale(locale, component) ⇒ Object
- .get_default_locale ⇒ Object
- .get_fallback_locale ⇒ Object
- .get_source_locale ⇒ Object
- .locale_fallbacks ⇒ Object
Class Method Details
.get_best_locale(locale, component) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sgtn-client/util/locale-util.rb', line 21 def self.get_best_locale(locale, component) component_result = @locale_match_results[component] ||= begin components = SgtnClient.config.available_components unless components.empty? || components.include?(component) raise SingletonError, "component '#{component}' doesn't exist!" end Concurrent::Map.new end component_result[locale] ||= begin component_result.delete(component_result.keys[Random.rand(component_result.size)]) if component_result.size >= 50 if SgtnClient.config.available_locales(component).include?(locale) locale elsif locale.nil? get_fallback_locale else locale = locale.to_s if locale.empty? get_fallback_locale else candidates = lowercase_locales_map(component) if candidates.empty? locale else get_best_match(locale.gsub('_', LOCALE_SEPARATOR).downcase, candidates) end end end end end |
.get_default_locale ⇒ Object
68 69 70 |
# File 'lib/sgtn-client/util/locale-util.rb', line 68 def self.get_default_locale EN_LOCALE end |
.get_fallback_locale ⇒ Object
72 73 74 |
# File 'lib/sgtn-client/util/locale-util.rb', line 72 def self.get_fallback_locale locale_fallbacks[0] end |
.get_source_locale ⇒ Object
64 65 66 |
# File 'lib/sgtn-client/util/locale-util.rb', line 64 def self.get_source_locale EN_LOCALE end |
.locale_fallbacks ⇒ Object
76 77 78 |
# File 'lib/sgtn-client/util/locale-util.rb', line 76 def self.locale_fallbacks @locale_fallbacks ||= [get_default_locale, get_source_locale, EN_LOCALE].uniq(&:to_s) - [nil, ''] end |