Class: TwitterCldr::Shared::Locale
- Inherits:
-
Object
- Object
- TwitterCldr::Shared::Locale
- Defined in:
- lib/twitter_cldr/shared/locale.rb
Instance Attribute Summary collapse
-
#language ⇒ Object
Returns the value of attribute language.
-
#region ⇒ Object
Returns the value of attribute region.
-
#script ⇒ Object
Returns the value of attribute script.
-
#variants ⇒ Object
Returns the value of attribute variants.
Class Method Summary collapse
- .parse(locale_text) ⇒ Object
- .parse_likely(locale_text) ⇒ Object
- .split(locale_text) ⇒ Object
- .valid?(locale_text) ⇒ Boolean
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #abbreviated_script ⇒ Object
- #ancestor_chain ⇒ Object
- #dasherized ⇒ Object
- #full_script ⇒ Object
- #hash ⇒ Object
-
#initialize(language, script = nil, region = nil, variants = []) ⇒ Locale
constructor
A new instance of Locale.
- #join(delimiter = '_') ⇒ Object (also: #underscored, #to_s)
- #max_supported ⇒ Object
- #maximize ⇒ Object
- #permutations(delimiter = '_') ⇒ Object
- #sort_key ⇒ Object
- #supported ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(language, script = nil, region = nil, variants = []) ⇒ Locale
Returns a new instance of Locale.
220 221 222 223 224 225 |
# 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 |
Instance Attribute Details
#language ⇒ Object
Returns the value of attribute language.
218 219 220 |
# File 'lib/twitter_cldr/shared/locale.rb', line 218 def language @language end |
#region ⇒ Object
Returns the value of attribute region.
218 219 220 |
# File 'lib/twitter_cldr/shared/locale.rb', line 218 def region @region end |
#script ⇒ Object
Returns the value of attribute script.
218 219 220 |
# File 'lib/twitter_cldr/shared/locale.rb', line 218 def script @script end |
#variants ⇒ Object
Returns the value of attribute variants.
218 219 220 |
# File 'lib/twitter_cldr/shared/locale.rb', line 218 def variants @variants end |
Class Method Details
.parse(locale_text) ⇒ Object
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).
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| (locale) (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
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 (locale_text.strip).all? do |subtag| !subtag.last.empty? end end |
Instance Method Details
#<=>(other) ⇒ Object
299 300 301 |
# File 'lib/twitter_cldr/shared/locale.rb', line 299 def <=>(other) other.sort_key <=> sort_key end |
#==(other) ⇒ Object Also known as: eql?
278 279 280 281 282 283 |
# File 'lib/twitter_cldr/shared/locale.rb', line 278 def ==(other) language == other.language && script == other.script && region == other.region && variants == other.variants end |
#abbreviated_script ⇒ Object
232 233 234 |
# File 'lib/twitter_cldr/shared/locale.rb', line 232 def abbreviated_script @short_script ||= PropertyValueAliases.abbreviated_alias_for('sc', script) || script end |
#ancestor_chain ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# 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 ⇒ Object
252 253 254 |
# File 'lib/twitter_cldr/shared/locale.rb', line 252 def dasherized join('-') end |
#full_script ⇒ Object
227 228 229 230 |
# 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 ⇒ Object
287 288 289 |
# File 'lib/twitter_cldr/shared/locale.rb', line 287 def hash to_a.hash end |
#join(delimiter = '_') ⇒ Object Also known as: underscored, to_s
256 257 258 |
# File 'lib/twitter_cldr/shared/locale.rb', line 256 def join(delimiter = '_') to_a.join(delimiter) end |
#max_supported ⇒ Object
240 241 242 |
# File 'lib/twitter_cldr/shared/locale.rb', line 240 def max_supported @max_supported ||= maximize.supported end |
#maximize ⇒ Object
236 237 238 |
# File 'lib/twitter_cldr/shared/locale.rb', line 236 def maximize LikelySubtags.locale_for(to_s) end |
#permutations(delimiter = '_') ⇒ Object
267 268 269 270 271 272 273 274 275 276 |
# 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 ⇒ Object
291 292 293 294 295 296 297 |
# 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 ⇒ Object
244 245 246 247 248 249 250 |
# 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 ⇒ Object
263 264 265 |
# File 'lib/twitter_cldr/shared/locale.rb', line 263 def to_a ([language, script, region] + variants).compact end |