Class: TwitterCldr::Shared::Locale

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language, script = nil, region = nil, variants = []) ⇒ Locale

Returns a new instance of Locale.



214
215
216
217
218
219
# File 'lib/twitter_cldr/shared/locale.rb', line 214

def initialize(language, script = nil, region = nil, variants = [])
  @language = language ? language.to_s : nil
  @script = script ? script.to_s : nil
  @region = region ? region.to_s : nil
  @variants = Array(variants)
end

Instance Attribute Details

#languageObject

Returns the value of attribute language.



212
213
214
# File 'lib/twitter_cldr/shared/locale.rb', line 212

def language
  @language
end

#regionObject

Returns the value of attribute region.



212
213
214
# File 'lib/twitter_cldr/shared/locale.rb', line 212

def region
  @region
end

#scriptObject

Returns the value of attribute script.



212
213
214
# File 'lib/twitter_cldr/shared/locale.rb', line 212

def script
  @script
end

#variantsObject

Returns the value of attribute variants.



212
213
214
# File 'lib/twitter_cldr/shared/locale.rb', line 212

def variants
  @variants
end

Class Method Details

.parse(locale_text) ⇒ Object

unicode.org/reports/tr35/tr35-9.html#Likely_Subtags

  1. Make sure the input locale is in canonical form: uses the right separator, and has the right casing.

  2. Replace any deprecated subtags with their canonical values using the <alias> data in supplemental metadata. Use the first value in the replacement list, if it exists.

  3. If the tag is grandfathered (see <variable id=“$grandfathered” type=“choice”> in the supplemental data), then return it. (NOTE: grandfathered subtags are no longer part of CLDR)

  4. Remove the script code ‘Zzzz’ and the region code ‘ZZ’ if they occur; change an empty language subtag to ‘und’.

  5. Get the components of the cleaned-up tag (language¹, script¹, and region¹), plus any variants if they exist (including keywords).



29
30
31
32
33
34
35
36
# File 'lib/twitter_cldr/shared/locale.rb', line 29

def parse(locale_text)
  locale_text = locale_text.to_s.strip

  normalize(locale_text).tap do |locale|
    replace_aliased_subtags(locale)
    remove_placeholder_tags(locale)
  end
end

.parse_likely(locale_text) ⇒ Object



46
47
48
# File 'lib/twitter_cldr/shared/locale.rb', line 46

def parse_likely(locale_text)
  LikelySubtags.locale_for(locale_text)
end

.split(locale_text) ⇒ Object



50
51
52
# File 'lib/twitter_cldr/shared/locale.rb', line 50

def split(locale_text)
  locale_text.strip.split(/[-_ ]/)
end

.valid?(locale_text) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/twitter_cldr/shared/locale.rb', line 38

def valid?(locale_text)
  # make sure all subtags have at least one identity, i.e. they exist
  # in one of the language/script/region/variant lists
  identify_subtags(locale_text.strip).all? do |subtag|
    !subtag.last.empty?
  end
end

Instance Method Details

#abbreviated_scriptObject



226
227
228
# File 'lib/twitter_cldr/shared/locale.rb', line 226

def abbreviated_script
  @short_script ||= PropertyValueAliases.abbreviated_alias_for('sc', script) || script
end

#dasherizedObject



248
249
250
# File 'lib/twitter_cldr/shared/locale.rb', line 248

def dasherized
  join('-')
end

#full_scriptObject



221
222
223
224
# File 'lib/twitter_cldr/shared/locale.rb', line 221

def full_script
  # fall back to abbreviated script if long alias can't be found
  @full_script ||= PropertyValueAliases.long_alias_for('sc', script) || script
end

#join(delimiter = '_') ⇒ Object Also known as: underscored, to_s



252
253
254
# File 'lib/twitter_cldr/shared/locale.rb', line 252

def join(delimiter = '_')
  to_a.join(delimiter)
end

#max_supportedObject



234
235
236
# File 'lib/twitter_cldr/shared/locale.rb', line 234

def max_supported
  @max_supported ||= maximize.supported
end

#maximizeObject



230
231
232
# File 'lib/twitter_cldr/shared/locale.rb', line 230

def maximize
  LikelySubtags.locale_for(to_s)
end

#permutations(delimiter = '_') ⇒ Object



263
264
265
266
267
268
269
270
271
272
# File 'lib/twitter_cldr/shared/locale.rb', line 263

def permutations(delimiter = '_')
  perms = [
    [language, script, region].compact.join(delimiter),
    [language, script].compact.join(delimiter),
    [language, region].compact.join(delimiter),
    language,
  ]

  perms.uniq
end

#supportedObject



238
239
240
241
242
243
244
245
246
# File 'lib/twitter_cldr/shared/locale.rb', line 238

def supported
  @supported ||= begin
    found = permutations('-').find do |perm|
      TwitterCldr.supported_locale?(perm)
    end

    self.class.new(found) if found
  end
end

#to_aObject



259
260
261
# File 'lib/twitter_cldr/shared/locale.rb', line 259

def to_a
  ([language, script, region] + variants).compact
end