Class: TwitterCldr::Resources::CldrLocale

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/resources/cldr_locale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_locale, cldr_requirement) ⇒ CldrLocale

Returns a new instance of CldrLocale.



14
15
16
17
# File 'lib/twitter_cldr/resources/cldr_locale.rb', line 14

def initialize(base_locale, cldr_requirement)
  @base_locale = base_locale
  @cldr_requirement = cldr_requirement
end

Instance Attribute Details

#base_localeObject (readonly)

Returns the value of attribute base_locale.



12
13
14
# File 'lib/twitter_cldr/resources/cldr_locale.rb', line 12

def base_locale
  @base_locale
end

#cldr_requirementObject (readonly)

Returns the value of attribute cldr_requirement.



12
13
14
# File 'lib/twitter_cldr/resources/cldr_locale.rb', line 12

def cldr_requirement
  @cldr_requirement
end

Instance Method Details

#ancestorsObject



19
20
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
# File 'lib/twitter_cldr/resources/cldr_locale.rb', line 19

def ancestors
  @ancestors ||= [].tap do |ancestry|
    locale = from_fs(base_locale)
    ancestry << locale

    loop do
      cur = from_fs(ancestry.last)

      if parents = parent_locales[cur]
        ancestry << to_fs(parents)
      elsif I18n::Locale::Tag.tag(cur).self_and_parents.count > 1
        ancestry << I18n::Locale::Tag.tag(cur).self_and_parents.last.to_sym
      else
        parents = TwitterCldr::Shared::Locale
          .parse(cur)
          .permutations

        ancestry.concat(parents - [cur])
        break
      end
    end

    ancestry.select! do |locale|
      File.exist?(File.join(cldr_requirement.common_path, 'main', "#{from_fs(locale)}.xml"))
    end

    ancestry << "root"
    ancestry.freeze
  end
end