Class | Locale::Object |
In: |
lib/locale/object.rb
|
Parent: | Object |
charset | [R] | |
country | [R] | |
fallback | [RW] | A fallback locale. With GetText, you don‘t need to set English(en,C,POSIX) by yourself because English is used as the last fallback locale anytime. |
language | [R] | |
modifier | [R] | |
orig_str | [R] | |
script | [R] | |
variant | [R] |
Initialize Locale::Object.
Locale::Object.new("ja", "JP", "eucJP") -> language = "ja", country = "JP", charset = "eucJP". Locale::Object.new("ja", "JP") -> language = "ja", country = "JP", charset = nil. Locale::Object.new("ja_JP.eucJP") -> language = "ja", country = "JP", charset = "eucJP". Locale::Object.new("ja_JP.eucJP", nil, "UTF-8") -> language = "ja", country = "JP", charset = "UTF-8". Locale::Object.new("en-US", "CA") -> language = "en", country = "CA", charset = nil. Locale::Object.new("uz-uz-latn") -> language = "uz", country = "UZ", charset = nil, script = "Latn" Locale::Object.new("uz_UZ_Latn") -> language = "uz", country = "UZ", charset = nil, script = "Latn" Locale::Object.new("we_BE.iso885915@euro") -> language = "we", country = "BE", charset = "iso885915", modifier = "euroo". Locale::Object.new("C") -> language = "en", country = nil, charset = nil. Locale::Object.new("POSIX") -> language = "en", country = nil, charset = nil.
# File lib/locale/object.rb, line 149 149: def initialize(language_or_locale_name, country = nil, charset = nil) 150: @orig_str = language_or_locale_name 151: @language, @country, @charset, @script, @variant, @modifier = 152: self.class.parse(language_or_locale_name) 153: @country = country if country 154: @charset = charset if charset 155: @fallback = nil 156: clear 157: end
Parse POSIX or RFC 3066 style locale name to Array.
(e.g.) uz-UZ-Latn, ja_JP.eucJP, wa_BE.iso885915@euro
(e.g.) "ja_JP.eucJP" => ["ja", "JP", "eucJP", nil, nil] "ja-jp.utf-8" => ["ja", "JP", "utf-8", nil, nil] "ja-jp" => ["ja", "JP", nil, nil, nil] "ja" => ["ja", nil, nil, nil, nil] "uz@Latn" => ["uz", nil, nil, nil, "Latn"] "uz-UZ-Latn" => ["uz", "UZ", nil, "Latn", nil] "uz_UZ_Latn" => ["uz", "UZ", nil, "Latn", nil] "wa_BE.iso885915@euro" => ["wa", "BE", "iso885915", nil, "euro"] "C" => ["en", nil, nil, nil, nil] "POSIX" => ["en", nil, nil, nil, nil] "zh_Hant" => ["zh", nil, nil, "Hant", nil] "zh_Hant_HK" => ["zh", "HK", nil, "Hant", nil] "de_DE@collation=phonebook,currency=DDM" => ["de", "DE", nil, nil, "collation=phonebook,currency=DDM"]
# File lib/locale/object.rb, line 97 97: def self.parse(locale_name) 98: lang_charset, modifier = locale_name.split(/@/) 99: lang, charset = lang_charset.split(/\./) 100: language, country, script, variant = lang.gsub(/_/, "-").split('-') 101: language = language ? language.downcase : nil 102: language = "en" if language == "c" || language == "posix" 103: if country 104: if country =~ /\A[A-Z][a-z]+\Z/ #Latn => script 105: tmp = script 106: script = country 107: if tmp =~ /\A[A-Z]+\Z/ #US => country 108: country = tmp 109: else 110: country = nil 111: variant = tmp 112: end 113: else 114: country = country.upcase 115: if script !~ /\A[A-Z][a-z]+\Z/ #Latn => script 116: variant = script 117: script = nil 118: end 119: end 120: end 121: [language, country, charset, script, variant, modifier] 122: end
Set the charset. e.g.) UTF-8, EUC-JP, Shift_JIS
# File lib/locale/object.rb, line 30 30: def charset=(val) 31: @charset = val 32: clear 33: end
# File lib/locale/object.rb, line 159 159: def clear 160: @posix = nil 161: @iso3066 = nil 162: @win = nil 163: @general = nil 164: @hash = "#{self.class}:#{to_general}.#{@charset}@#{@modifier}".hash 165: end
Set the country. e.g.) JP, US, FR, …
# File lib/locale/object.rb, line 24 24: def country=(val) 25: @country = val 26: clear 27: end
Set the language. e.g.) ja, en, fr, …
# File lib/locale/object.rb, line 18 18: def language=(val) 19: @language = val 20: clear 21: end
Set the modifier. e.g.) curreny=DDM
# File lib/locale/object.rb, line 48 48: def modifier=(val) 49: @modifier = val 50: clear 51: end
Set the script. e.g.) Latn
# File lib/locale/object.rb, line 36 36: def script=(val) 37: @script = val 38: clear 39: end
Gets the locale informations as an Array.
# File lib/locale/object.rb, line 215 215: def to_a 216: [@language, @country, @charset, @script, @variant, @modifier] 217: end
Returns the locale as ‘ruby’ general format. (e.g.) "az_AZ_Latn"
# File lib/locale/object.rb, line 198 198: def to_general 199: return @general if @general 200: 201: @general = @language.dup 202: @general << "_#{@country}" if @country 203: @general << "_#{@script}" if @script 204: @general 205: end
Returns the locale as ISO3066 format. (e.g.) "ja-JP"
# File lib/locale/object.rb, line 177 177: def to_iso3066 178: return @iso3066 if @iso3066 179: 180: @iso3066 = @language.dup 181: @iso3066 << "-#{@country}" if @country 182: @iso3066 183: end
Returns the locale as POSIX format(but charset is ignored). (e.g.) "ja_JP"
# File lib/locale/object.rb, line 168 168: def to_posix 169: return @posix if @posix 170: @posix = @language.dup 171: 172: @posix << "_#{@country}" if @country 173: @posix 174: end
Returns the locale as Win32 format. (e.g.) "az-AZ-Latn".
This is used to find the charset from locale table.
# File lib/locale/object.rb, line 188 188: def to_win 189: return @win if @win 190: 191: @win = @language.dup 192: @win << "-#{@country}" if @country 193: @win << "-#{@script}" if @script 194: @win 195: end