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
- .grandfathered?(locale_text) ⇒ Boolean
- .parse(locale_text) ⇒ Object
- .parse_likely(locale_text) ⇒ Object
- .split(locale_text) ⇒ Object
- .valid?(locale_text) ⇒ Boolean
Instance Method Summary collapse
- #abbreviated_script ⇒ Object
- #dasherized ⇒ Object
- #full_script ⇒ Object
-
#initialize(language, script = nil, region = nil, variants = []) ⇒ Locale
constructor
A new instance of Locale.
- #join(delimiter = '_') ⇒ Object (also: #underscored, #to_s)
- #maximize ⇒ Object
- #permutations(delimiter = '_') ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(language, script = nil, region = nil, variants = []) ⇒ Locale
Returns a new instance of Locale.
210 211 212 213 214 215 |
# File 'lib/twitter_cldr/shared/locale.rb', line 210 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.
208 209 210 |
# File 'lib/twitter_cldr/shared/locale.rb', line 208 def language @language end |
#region ⇒ Object
Returns the value of attribute region.
208 209 210 |
# File 'lib/twitter_cldr/shared/locale.rb', line 208 def region @region end |
#script ⇒ Object
Returns the value of attribute script.
208 209 210 |
# File 'lib/twitter_cldr/shared/locale.rb', line 208 def script @script end |
#variants ⇒ Object
Returns the value of attribute variants.
208 209 210 |
# File 'lib/twitter_cldr/shared/locale.rb', line 208 def variants @variants end |
Class Method Details
.grandfathered?(locale_text) ⇒ Boolean
58 59 60 |
# File 'lib/twitter_cldr/shared/locale.rb', line 58 def grandfathered?(locale_text) grandfathered.include?(locale_text) end |
.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.
-
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).
28 29 30 31 32 33 34 35 36 |
# File 'lib/twitter_cldr/shared/locale.rb', line 28 def parse(locale_text) locale_text = locale_text.to_s.strip return Locale.new(locale_text) if grandfathered?(locale_text) normalize(locale_text).tap do |locale| (locale) (locale) end end |
.parse_likely(locale_text) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/twitter_cldr/shared/locale.rb', line 46 def parse_likely(locale_text) if grandfathered?(locale_text) new(locale_text.strip) else LikelySubtags.locale_for(locale_text) end end |
.split(locale_text) ⇒ Object
54 55 56 |
# File 'lib/twitter_cldr/shared/locale.rb', line 54 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
#abbreviated_script ⇒ Object
222 223 224 |
# File 'lib/twitter_cldr/shared/locale.rb', line 222 def abbreviated_script @short_script ||= PropertyValueAliases.abbreviated_alias_for('sc', script) || script end |
#dasherized ⇒ Object
234 235 236 |
# File 'lib/twitter_cldr/shared/locale.rb', line 234 def dasherized join('-') end |
#full_script ⇒ Object
217 218 219 220 |
# File 'lib/twitter_cldr/shared/locale.rb', line 217 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
238 239 240 |
# File 'lib/twitter_cldr/shared/locale.rb', line 238 def join(delimiter = '_') to_a.join(delimiter) end |
#maximize ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/twitter_cldr/shared/locale.rb', line 226 def maximize if Locale.grandfathered?(to_s) self else LikelySubtags.locale_for(to_s) end end |
#permutations(delimiter = '_') ⇒ Object
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/twitter_cldr/shared/locale.rb', line 249 def permutations(delimiter = '_') perms = [ [language, script, region].compact.join(delimiter), [language, script].compact.join(delimiter), [language, region].compact.join(delimiter), language, ] perms.uniq end |
#to_a ⇒ Object
245 246 247 |
# File 'lib/twitter_cldr/shared/locale.rb', line 245 def to_a ([language, script, region] + variants).compact end |