class TwitterCldr::Shared::Locale
Attributes
language[RW]
region[RW]
script[RW]
variants[RW]
Public Class Methods
new(language, script = nil, region = nil, variants = [])
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 220 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
parse(locale_text)
click to toggle source
unicode.org/reports/tr35/tr35-9.html#Likely_Subtags
-
Make sure the input locale is in canonical form: uses the right separator, and has the right casing.
-
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.
-
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)
-
Remove the script code 'Zzzz' and the region code 'ZZ' if they occur; change an empty language subtag to 'und'.
-
Get the components of the cleaned-up tag (language¹, script¹, and region¹), plus any variants if they exist (including keywords).
# 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)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 46 def parse_likely(locale_text) LikelySubtags.locale_for(locale_text) end
split(locale_text)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 50 def split(locale_text) locale_text.strip.split(/[-_ ]/) end
valid?(locale_text)
click to toggle source
# 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
Private Class Methods
aliases_resource()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 203 def aliases_resource @aliases_resource ||= TwitterCldr.get_resource('shared', 'aliases')[:aliases] end
identify_subtag(subtag)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 101 def identify_subtag(subtag) [].tap do |types| types << :language if language?(subtag) types << :script if script?(subtag) types << :region if region?(subtag) types << :variant if variant?(subtag) types << :language if language?(normalize_subtag(subtag, :language)) types << :script if script?(normalize_subtag(subtag, :script)) types << :region if region?(normalize_subtag(subtag, :region)) types << :variant if variant?(normalize_subtag(subtag, :variant)) end end
language?(subtag)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 115 def language?(subtag) languages.include?(subtag) || language_aliases.include?(subtag.to_sym) end
language_aliases()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 139 def language_aliases @language_aliases ||= aliases_resource[:language].each_with_object({}) do |(_, aliases), ret| ret.merge!(aliases) end end
languages()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 181 def languages @languages ||= [:regular, :special].flat_map do |type| validity_resource[:languages][type] end end
normalize(locale_text)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 56 def normalize(locale_text) Locale.new(nil).tap do |locale| subtags = identify_subtags(locale_text) until subtags.empty? subtag, identities = subtags.shift next if identities.empty? identities.each do |identity| unless subtag_set?(locale, identity) set_subtag(locale, identity, subtag) break end end end end end
normalize_subtag(subtag, identity)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 145 def normalize_subtag(subtag, identity) case identity when :language subtag.downcase when :script subtag.capitalize when :region, :variant subtag.upcase end end
parent_locales()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 213 def parent_locales @parent_locales ||= TwitterCldr.get_resource('shared', 'parent_locales') end
region?(subtag)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 124 def region?(subtag) territories.include?(subtag) || region_aliases.include?(subtag.to_sym) end
region_aliases()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 133 def region_aliases @region_aliases ||= aliases_resource[:territory].each_with_object({}) do |(_, aliases), ret| ret.merge!(aliases) end end
script?(subtag)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 119 def script?(subtag) scripts.include?(subtag) || !!PropertyValueAliases.long_alias_for('sc', subtag) end
scripts()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 187 def scripts @scripts ||= [:regular, :special].flat_map do |type| validity_resource[:scripts][type] end end
set_subtag(locale, identity, subtag)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 83 def set_subtag(locale, identity, subtag) case identity when :variant locale.variants << normalize_subtag(subtag, identity) else locale.send( :"#{identity}=", normalize_subtag(subtag, identity) ) end end
subtag_set?(locale, identity)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 74 def subtag_set?(locale, identity) case identity when :variant false else !!locale.send(identity) end end
territories()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 193 def territories @territories ||= [:regular, :special, :macroregion].flat_map do |type| validity_resource[:regions][type] end end
validity_resource()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 208 def validity_resource @validity_resource ||= TwitterCldr.get_resource('shared', 'validity_data')[:validity_data] end
variant?(subtag)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 128 def variant?(subtag) subtag = normalize_subtag(subtag, :variant) variants.include?(subtag) end
variants()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 199 def variants validity_resource[:variants][:regular] end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 299 def <=>(other) other.sort_key <=> sort_key end
==(other)
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 278 def ==(other) language == other.language && script == other.script && region == other.region && variants == other.variants end
Also aliased as: eql?
abbreviated_script()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 232 def abbreviated_script @short_script ||= PropertyValueAliases.abbreviated_alias_for('sc', script) || script end
ancestor_chain()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 303 def ancestor_chain ancestry = [self] remaining = [self] until remaining.empty? locale = remaining.pop if parent = self.class.send(:parent_locales)[locale.to_s] parent = self.class.parse(parent) ancestry << parent remaining << parent else parents = locale.permutations.map { |p| self.class.parse(p) } remaining += parents - ancestry ancestry += parents - ancestry end end ancestry end
dasherized()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 252 def dasherized join('-') end
full_script()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 227 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
hash()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 287 def hash to_a.hash end
join(delimiter = '_')
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 256 def join(delimiter = '_') to_a.join(delimiter) end
Also aliased as: underscored, to_s
max_supported()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 240 def max_supported @max_supported ||= maximize.supported end
maximize()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 236 def maximize LikelySubtags.locale_for(to_s) end
permutations(delimiter = '_')
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 267 def permutations(delimiter = '_') perms = [ [language, script, region].compact.join(delimiter), [language, script].compact.join(delimiter), [language, region].compact.join(delimiter), language, ] perms.uniq end
sort_key()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 291 def sort_key k = 0 k += 3 if language k += 2 if script k += 1 if region k end
supported()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 244 def supported @supported ||= begin ancestor_chain.sort.find do |loc| TwitterCldr.supported_locale?(loc.dasherized) end end end
to_a()
click to toggle source
# File lib/twitter_cldr/shared/locale.rb, line 263 def to_a ([language, script, region] + variants).compact end