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



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
# 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.shift 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_localeObject



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

def self.get_default_locale
  EN_LOCALE
end

.get_fallback_localeObject



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

def self.get_fallback_locale
  locale_fallbacks[0]
end

.get_source_localeObject



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

def self.get_source_locale
  EN_LOCALE
end

.locale_fallbacksObject



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

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