Class: SgtnClient::LocaleUtil

Inherits:
Object
  • Object
show all
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

Class Method Details

.get_best_locale(locale, component) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sgtn-client/util/locale-util.rb', line 29

def self.get_best_locale(locale, component)
  return locale if SgtnClient.config.available_locales(component).include?(locale)

  component_result = @locale_match_results[component]
  component_result[locale] ||= begin
    if component_result.size >= 50
      component_result.delete(component_result.keys[Random.rand(component_result.size)])
    end

    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

.get_default_localeObject



66
67
68
# File 'lib/sgtn-client/util/locale-util.rb', line 66

def self.get_default_locale
  EN_LOCALE
end

.get_fallback_localeObject



70
71
72
# File 'lib/sgtn-client/util/locale-util.rb', line 70

def self.get_fallback_locale
  locale_fallbacks[0]
end

.get_source_localeObject



62
63
64
# File 'lib/sgtn-client/util/locale-util.rb', line 62

def self.get_source_locale
  EN_LOCALE
end

.locale_fallbacksObject



74
75
76
# File 'lib/sgtn-client/util/locale-util.rb', line 74

def self.locale_fallbacks
  @locale_fallbacks ||= ([get_default_locale, get_source_locale, EN_LOCALE].uniq(&:to_s) - [nil, '']).freeze
end