Class: Locale::Object

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language_or_locale_name, country = nil, charset = nil) ⇒ Object

Initialize Locale::Object.

  • language_or_locale_name: language(ISO 639) or POSIX or RFC3066 style locale name

  • country: an uppercase ISO 3166-1 country/region identifier, or nil

  • charset: charset(codeset) (no standard), or nil

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.


149
150
151
152
153
154
155
156
157
# File 'lib/locale/object.rb', line 149

def initialize(language_or_locale_name, country = nil, charset = nil)
  @orig_str = language_or_locale_name
  @language, @country, @charset, @script, @variant, @modifier = 
	self.class.parse(language_or_locale_name)
  @country = country if country
  @charset = charset if charset
  @fallback = nil
  clear
end

Instance Attribute Details

#charsetObject

Returns the value of attribute charset.



15
16
17
# File 'lib/locale/object.rb', line 15

def charset
  @charset
end

#countryObject

Returns the value of attribute country.



15
16
17
# File 'lib/locale/object.rb', line 15

def country
  @country
end

#fallbackObject

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.



55
56
57
# File 'lib/locale/object.rb', line 55

def fallback
  @fallback
end

#languageObject

Returns the value of attribute language.



15
16
17
# File 'lib/locale/object.rb', line 15

def language
  @language
end

#modifierObject

Returns the value of attribute modifier.



15
16
17
# File 'lib/locale/object.rb', line 15

def modifier
  @modifier
end

#orig_strObject (readonly)

Returns the value of attribute orig_str.



15
16
17
# File 'lib/locale/object.rb', line 15

def orig_str
  @orig_str
end

#scriptObject

Returns the value of attribute script.



15
16
17
# File 'lib/locale/object.rb', line 15

def script
  @script
end

#variantObject

Returns the value of attribute variant.



15
16
17
# File 'lib/locale/object.rb', line 15

def variant
  @variant
end

Class Method Details

.parse(locale_name) ⇒ Object

Parse POSIX or RFC 3066 style locale name to Array.

  • locale_name: locale name as String

    • Basic POSIX format: <language>_<COUNTRY>.<charset>@<modifier>

      • Both of POSIX and C are converted to “en”.

    • Basic RFC3066 format: <language>-<COUNTRY>

    • Win32 format: <language>-<COUNTRY>-<Script>_<sort order>

    • CLDR format: <language>_<Script>_<COUNTRY>_<variant>@<modifier>

    • Some broken format: <language>_<country>_<script> # Don’t use this.

    • The max locale format is below:

      • <language>-<COUNTRY>-<Script>_<sort order>.<charset>@<modifier>

      • format: <language>_<Script>_<COUNTRY>_<variant>@<modifier>

        • both of ‘-’ and ‘_’ are separators.

        • each elements are omittable.

    (e.g.) uz-UZ-Latn, ja_JP.eucJP, wa_BE.iso885915@euro

  • Returns: [language, country, charset, script, modifier]

    • language: a lowercase ISO 639(or 639-2/T) language code.

    • country: an uppercase ISO 3166-1 country/region identifier.

    • charset: charset(codeset) (no standard)

    • script: an initial-uppercase ISO 15924 script code.

    • variant: variant value in CLDR or sort order in Win32.

    • modifier: (no standard)

(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"]


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/locale/object.rb', line 97

def self.parse(locale_name)
  lang_charset, modifier = locale_name.split(/@/)
  lang, charset = lang_charset.split(/\./)
  language, country, script, variant = lang.gsub(/_/, "-").split('-')
  language = language ? language.downcase : nil
  language = "en" if language == "c" || language == "posix"
  if country
	if country =~ /\A[A-Z][a-z]+\Z/  #Latn => script
	  tmp = script
	  script = country
	  if tmp =~ /\A[A-Z]+\Z/ #US => country
 country = tmp
	  else
 country = nil
 variant = tmp
	  end
	else
	  country = country.upcase
	  if script !~ /\A[A-Z][a-z]+\Z/ #Latn => script
 variant = script
 script = nil
	  end
	end
  end
  [language, country, charset, script, variant, modifier]
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



219
220
221
# File 'lib/locale/object.rb', line 219

def ==(other)  #:nodoc:
  other != nil and @hash == other.hash
end

#clearObject



159
160
161
162
163
164
165
# File 'lib/locale/object.rb', line 159

def clear
  @posix = nil
  @iso3066 = nil
  @win = nil
  @general = nil
  @hash = "#{self.class}:#{to_general}.#{@charset}@#{@modifier}".hash
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


223
224
225
# File 'lib/locale/object.rb', line 223

def eql?(other) #:nodoc:
  self.==(other)
end

#hashObject

:nodoc:



227
228
229
# File 'lib/locale/object.rb', line 227

def hash #:nodoc:
  @hash
end

#to_aObject

Gets the locale informations as an Array.

  • Returns [language, country, charset, script, variant, modifier]

    • language: a lowercase ISO 639(or 639-2/T) language code.

    • country: an uppercase ISO 3166-1 country/region identifier.

    • charset: charset(codeset) (no standard)

    • script: an initial-uppercase ISO 15924 script code.

    • variant: variant value in CLDR or sort order in Win32.

    • modifier: (no standard)



215
216
217
# File 'lib/locale/object.rb', line 215

def to_a
  [@language, @country, @charset, @script, @variant, @modifier]
end

#to_generalObject

Returns the locale as ‘ruby’ general format. (e.g.) “az_AZ_Latn”



198
199
200
201
202
203
204
205
# File 'lib/locale/object.rb', line 198

def to_general
  return @general if @general
 
  @general = @language.dup
  @general << "_#{@country}" if @country
  @general << "_#{@script}" if @script 
  @general
end

#to_iso3066Object

Returns the locale as ISO3066 format. (e.g.) “ja-JP”



177
178
179
180
181
182
183
# File 'lib/locale/object.rb', line 177

def to_iso3066
  return @iso3066 if @iso3066

  @iso3066 = @language.dup
  @iso3066 << "-#{@country}" if @country
  @iso3066
end

#to_posixObject Also known as: to_s, to_str

Returns the locale as POSIX format(but charset is ignored). (e.g.) “ja_JP”



168
169
170
171
172
173
174
# File 'lib/locale/object.rb', line 168

def to_posix
  return @posix if @posix
  @posix = @language.dup

  @posix << "_#{@country}" if @country
  @posix
end

#to_winObject

Returns the locale as Win32 format. (e.g.) “az-AZ-Latn”.

This is used to find the charset from locale table.



188
189
190
191
192
193
194
195
# File 'lib/locale/object.rb', line 188

def to_win
  return @win if @win

  @win = @language.dup
  @win << "-#{@country}" if @country
  @win << "-#{@script}" if @script
  @win
end