Class: Worldwide::Locale
- Inherits:
-
Object
- Object
- Worldwide::Locale
- Defined in:
- lib/worldwide/locale.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#unknown ⇒ Object
readonly
Returns the value of attribute unknown.
Instance Method Summary collapse
-
#endonym(context: :middle_of_sentence, throw: true) ⇒ Object
The language’s name in that language (“endonym”).
-
#initialize(code) ⇒ Locale
constructor
A new instance of Locale.
- #language_subtag ⇒ Object
- #name(locale: I18n.locale, context: :middle_of_sentence, throw: true) ⇒ Object
- #script ⇒ Object
- #sub_locales ⇒ Object
Constructor Details
#initialize(code) ⇒ Locale
7 8 9 10 11 12 13 14 |
# File 'lib/worldwide/locale.rb', line 7 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
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/worldwide/locale.rb', line 5 def code @code end |
#unknown ⇒ Object (readonly)
Returns the value of attribute unknown.
18 19 20 |
# File 'lib/worldwide/locale.rb', line 18 def unknown @unknown end |
Instance Method Details
#endonym(context: :middle_of_sentence, throw: true) ⇒ Object
The language’s name in that language (“endonym”).
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/worldwide/locale.rb', line 81 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_subtag ⇒ Object
20 21 22 |
# File 'lib/worldwide/locale.rb', line 20 def language_subtag code.to_s.split("-", 2).first end |
#name(locale: I18n.locale, context: :middle_of_sentence, throw: true) ⇒ Object
49 50 51 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 |
# File 'lib/worldwide/locale.rb', line 49 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 |
#script ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/worldwide/locale.rb', line 24 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("_") = Worldwide::Locales.[adjusted_locale] return .split("_")[1] unless .nil? # If the locale's language code has an entry in the likely_subtags list, then use that = Worldwide::Locales.[parts.first] return .split("_")[1] unless .nil? nil end |
#sub_locales ⇒ Object
45 46 47 |
# File 'lib/worldwide/locale.rb', line 45 def sub_locales Worldwide::Locales.sub_locales[code] || [] end |