Class | Locale::Object
In: | lib/locale/object.rb
Parent: | Object

### Methods

charset= clear country= language= modifier= new parse script= to_a to_general to_iso3066 to_posix to_s to_str to_win variant=

### Attributes

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

### Public Class methods

new(language_or_locale_name, country = nil, charset = nil)

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.

[Source]

         # File lib/locale/object.rb, line 153
    153:     def initialize(language_or_locale_name, country = nil, charset = nil)
    154:       @orig_str = language_or_locale_name
    155:       @language, @country, @charset, @script, @variant, @modifier =
    156:         self.class.parse(language_or_locale_name)
    157:       @country = country if country
    158:       @charset = charset if charset
    159:       @fallback = nil
    160:       clear
    161:     end

parse(locale_name)

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

[Source]

         # File lib/locale/object.rb, line 97
     97:     def self.parse(locale_name)
     98:       if locale_name.nil? || locale_name.empty?
     99:         return ["en", nil, nil, nil, nil, nil]
    100:       else
    101:         lang_charset, modifier = locale_name.split(/@/)
    102:         lang, charset = lang_charset.split(/\./)
    103:         language, country, script, variant = lang.gsub(/_/, "-").split('-')
    104:         language = language ? language.downcase : nil
    105:         language = "en" if language == "c" || language == "posix"
    106:       end
    107:       if country
    108:         if country =~ /\A[A-Z][a-z]+\Z/  #Latn => script
    109:           tmp = script
    110:           script = country
    111:           if tmp =~ /\A[A-Z]+\Z/ #US => country
    112:             country = tmp
    113:           else
    114:             country = nil
    115:             variant = tmp
    116:           end
    117:         else
    118:           country = country.upcase
    119:           if script !~ /\A[A-Z][a-z]+\Z/ #Latn => script
    120:             variant = script
    121:             script = nil
    122:           end
    123:         end
    124:       end
    125:       [language, country, charset, script, variant, modifier]
    126:     end

### Public Instance methods

charset=(val)

Set the charset. e.g.) UTF-8, EUC-JP, Shift_JIS

[Source]

        # File lib/locale/object.rb, line 30
    30:     def charset=(val)
    31:       @charset = val
    32:       clear
    33:     end

clear()

[Source]

         # File lib/locale/object.rb, line 163
    163:     def clear
    164:       @posix = nil
    165:       @iso3066 = nil
    166:       @win = nil
    167:       @general = nil
    168:       @hash = "#{self.class}:#{to_general}.#{@charset}@#{@modifier}".hash
    169:     end

country=(val)

Set the country. e.g.) JP, US, FR, …

[Source]

        # File lib/locale/object.rb, line 24
    24:     def country=(val)
    25:       @country = val
    26:       clear
    27:     end

language=(val)

Set the language. e.g.) ja, en, fr, …

[Source]

        # File lib/locale/object.rb, line 18
    18:     def language=(val)
    19:       @language = val
    20:       clear
    21:     end

modifier=(val)

Set the modifier. e.g.) curreny=DDM

[Source]

        # File lib/locale/object.rb, line 48
    48:     def modifier=(val)
    49:       @modifier = val
    50:       clear
    51:     end

script=(val)

Set the script. e.g.) Latn

[Source]

        # File lib/locale/object.rb, line 36
    36:     def script=(val)
    37:       @script = val
    38:       clear
    39:     end

to_a()

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)

[Source]

         # File lib/locale/object.rb, line 219
    219:     def to_a
    220:       [@language, @country, @charset, @script, @variant, @modifier]
    221:     end

to_general()

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

[Source]

         # File lib/locale/object.rb, line 202
    202:     def to_general
    203:       return @general if @general
    204:
    205:       @general = @language.dup
    206:       @general << "_#{@country}" if @country
    207:       @general << "_#{@script}" if @script
    208:       @general
    209:     end

to_iso3066()

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

[Source]

         # File lib/locale/object.rb, line 181
    181:     def to_iso3066
    182:       return @iso3066 if @iso3066
    183:
    184:       @iso3066 = @language.dup
    185:       @iso3066 << "-#{@country}" if @country
    186:       @iso3066
    187:     end

to_posix()

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

[Source]

         # File lib/locale/object.rb, line 172
    172:     def to_posix
    173:       return @posix if @posix
    174:       @posix = @language.dup
    175:
    176:       @posix << "_#{@country}" if @country
    177:       @posix
    178:     end

to_s()

Alias for to_posix

to_str()

Alias for to_posix

to_win()

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

This is used to find the charset from locale table.

[Source]

         # File lib/locale/object.rb, line 192
    192:     def to_win
    193:       return @win if @win
    194:
    195:       @win = @language.dup
    196:       @win << "-#{@country}" if @country
    197:       @win << "-#{@script}" if @script
    198:       @win
    199:     end

variant=(val)

Set the variant. e.g.) Hant

[Source]

        # File lib/locale/object.rb, line 42
    42:     def variant=(val)
    43:       @variant = val
    44:       clear
    45:     end

[Validate]