Class: Worldwide::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/worldwide/locale.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Locale

Returns a new instance of Locale.



14
15
16
17
18
19
20
21
# File 'lib/worldwide/locale.rb', line 14

def initialize(code)
  if code.nil? || code.empty?
    raise ArgumentError, "Invalid locale: cannot be nil nor an empty string"
  end

  @code = code.to_sym
  @name_cache = {}
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



12
13
14
# File 'lib/worldwide/locale.rb', line 12

def code
  @code
end

Class Method Details

.unknownObject



6
7
8
9
# File 'lib/worldwide/locale.rb', line 6

def unknown
  # Special CLDR value
  @unknown = Locale.new("und")
end

Instance Method Details

#endonym(context: :middle_of_sentence, throw: true) ⇒ Object

The language’s name in that language (“endonym”).



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/worldwide/locale.rb', line 84

def endonym(context: :middle_of_sentence, throw: true)
  return @endonym if defined?(@endonym)

  begin
    @endonym = lookup(code, locale: code)
  rescue I18n::InvalidLocale => e
    raise e if throw
  end

  @endonym = Worldwide::Cldr::ContextTransforms.transform(@endonym, :languages, context, locale: code) unless @endonym.nil?
  @endonym ||= I18n::MissingTranslation.new(code, "languages.#{code}")

  if @endonym.is_a?(I18n::MissingTranslation)
    raise @endonym if throw

    return nil
  end

  @endonym
end

#language_subtagObject



23
24
25
# File 'lib/worldwide/locale.rb', line 23

def language_subtag
  code.to_s.split("-", 2).first
end

#name(locale: I18n.locale, context: :middle_of_sentence, throw: true) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/worldwide/locale.rb', line 52

def name(locale: I18n.locale, context: :middle_of_sentence, throw: true)
  @name_cache[context] ||= I18n.with_locale(locale) do
    # Try to find a language name as specific as possible in the locale we wanted.
    exonym(target_locale: locale, context: context) ||

      # We didn't find a language name in the locale we wanted.
      # In cases like this, try to look up the language's native name ("endonym").
      endonym(context: context, throw: throw) ||

      # CLDR does not define language names (`languages.yml`) in some languages
      # (example: 'cu' Church Slavic does not have a `languages.yml`, so every
      # language name lookup in that locale will fail, since `cu` lookups only
      # fall back to `root`, which also does not have a `languages.yml`).
      # This means that we cannot provide the endonym for `cu`.

      # If we have a translation in the default locale, we'll return that.
      name_in_default_locale(context: context) ||

      # We still don't know anything about the language, and we're out of options.
      I18n::MissingTranslation.new(locale, "languages.#{code}")
  end

  if @name_cache[context].is_a?(I18n::MissingTranslation)
    raise @name_cache[context] if throw

    return nil
  end

  @name_cache[context]
end

#scriptObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/worldwide/locale.rb', line 27

def script
  locale_string = code.to_s.tr("-", "_")

  parts = locale_string.downcase.split("_")

  # If the locale includes a script specification, then return that script
  return parts[1].capitalize if parts.size >= 2 && parts[1].size == 4

  # If the full locale has an entry in the likely_subtags list, the use that
  parts = locale_string.split("_")
  adjusted_locale = [parts.first.downcase, parts.last.upcase].join("_")
  subtags = Worldwide::Locales.likely_subtags[adjusted_locale]
  return subtags.split("_")[1] unless subtags.nil?

  # If the locale's language code has an entry in the likely_subtags list, then use that
  subtags = Worldwide::Locales.likely_subtags[parts.first]
  return subtags.split("_")[1] unless subtags.nil?

  nil
end

#sub_localesObject



48
49
50
# File 'lib/worldwide/locale.rb', line 48

def sub_locales
  Worldwide::Locales.sub_locales[code] || []
end