Module: Locale

Module | Locale
In: | lib/locale/driver/env.rb lib/locale/driver/jruby.rb lib/locale/driver/posix.rb lib/locale/driver/win32_table.rb lib/locale/driver/win32.rb lib/locale/driver/cgi.rb lib/locale/version.rb lib/locale/tag.rb lib/locale/tag/cldr.rb lib/locale/tag/common.rb lib/locale/tag/rfc.rb lib/locale/tag/irregular.rb lib/locale/tag/posix.rb lib/locale/tag/simple.rb lib/locale/info/language.rb lib/locale/info/region.rb lib/locale/taglist.rb lib/locale/util/memoizable.rb lib/locale.rb

Locale module manages the locale informations of the application. These functions are the most important APIs in this library. Almost of all i18n/l10n programs use this APIs only.

### Methods

app_language_tags candidates cgi= charset clear clear_all current current= default default= driver_module init set_app_language_tags set_cgi set_current set_default set_request

### Included Modules

Locale::Util::Memoizable

### Classes and Modules

Module Locale::Driver
Module Locale::Info
Module Locale::Tag
Module Locale::Util
Class Locale::TagList

### Constants

VERSION | = | "2.0.4"
ROOT | = | File.dirname(__FILE__)

### Public Instance methods

app_language_tags()

Returns the app_language_tags. Default is nil. See set_app_language_tags for more details.

candidates(options = {})

Returns the language tags which are variations of the current locales order by priority.

For example, if the current locales are ["fr", "ja_JP", "en_US", "en-Latn-GB-VARIANT"], then returns ["fr", "ja_JP", "en_US", "en-Latn-GB-VARIANT", "en_Latn_GB", "en_GB", "ja", "en"]. "en" is the default locale(You can change it using set_default). The default locale is added at the end of the list even if it isn‘t exist.

Usually, this method is used to find the locale data as the path(or a kind of IDs).

- options: options as a Hash or nil.
  - :supported_language_tags - An Array of the language tags order by the priority. This option restricts the locales which are supported by the library/application. Default is nil if you don‘t need to restrict the locales.

         (e.g.1) ["fr_FR", "en_GB", "en_US", ...]

  - :type - The type of language tag. :common, :rfc, :cldr, :posix and :simple are available. Default value is :common

cgi=(cgi)

Sets a CGI object.This is the convenient function of set_request().

This method is appeared when Locale.init(:driver => :cgi) is called.

- cgi: CGI object
- Returns: cgi

charset()

Gets the current charset.

This returns the current user/system charset. This value is read only, so you can‘t set it by yourself.

- Returns: the current charset.

clear()

Clear current locale.

- Returns: self

clear_all()

Clear all locales and charsets of all threads. This doesn‘t clear the default and app_language_tags. Use Locale.default = nil to unset the default locale.

- Returns: self

current()

Gets the current locales (Locale::Tag‘s class). If the current locale is not set, this returns system/default locale.

This method returns the current language tags even if it isn‘t included in app_language_tags.

Usually, the programs should use Locale.candidates to find the correct locale, not this method.

- Returns: an Array of the current locales (Locale::Tag‘s class).

current=(tag)

Sets a current locale. This is a single argument version of Locale.set_current.

- tag: the language tag such as "ja-JP"
- Returns: an Array of the current locale (Locale::Tag‘s class).

       Locale.current = "ja-JP"
       Locale.current = "ja_JP.eucJP"

default()

Gets the default locale(language tag).

If the default language tag is not set, this returns nil.

- Returns: the default locale (Locale::Tag‘s class).

default=(tag)

Same as Locale.set_default.

- locale: the default locale (Locale::Tag‘s class) or a String such as "ja-JP".
- Returns: locale.

driver_module()

Gets the driver module.

Usually you don‘t need to call this method.

- Returns: the driver module.

init(opts = {})

Initialize Locale library. Usually, you don‘t need to call this directly, because this is called when Locale‘s methods are called.

If you use this library with CGI or the kind of CGI. You need to call Locale.init(:driver => :cgi).

#### For Framework designers/programers:

If your framework is for WWW, call this once like: Locale.init(:driver => :cgi).

#### To Application programers:

If your framework doesn‘t use ruby-locale and the application is for WWW, call this once like: Locale.init(:driver => :cgi).

#### To Library authors:

Don‘t call this, even if your application is only for WWW.

- opts: Options as a Hash.
  - :driver - The driver. :cgi if you use Locale module with CGI, nil if you use system locale.

          (ex) Locale.init(:driver => :cgi)

set_app_language_tags(*tags)

Set the language tags which is supported by the Application. This value is same with supported_language_tags in Locale.candidates to restrict the result but is the global setting. If you set a language tag, the application works as the single locale application.

If the current locale is not included in app_language_tags, Locale.default value is used. Use Locale.set_default() to set correct language if "en" is not included in the language tags.

Set nil if clear the value.

Note that the libraries/plugins shouldn‘t set this value.

     (e.g.) Locale.set_app_language_tags("fr_FR", "en-GB", "en_US", ...)

set_cgi(cgi)

Sets a CGI object. This is the convenient function of set_request().

This method is appeared when Locale.init(:driver => :cgi) is called.

- cgi: CGI object
- Returns: self

set_current(*tags)

Sets the locales of the current thread order by the priority. Each thread has a current locales. The system locale/default locale is used if the thread doesn‘t have current locales.

- tag: Locale::Language::Tag‘s class or the language tag as a String. nil if you need to clear current locales.
- charset: the charset (override the charset even if the locale name has charset) or nil.
- Returns: self

(e.g.)

       Locale.set_current("ja_JP.eucJP")
       Locale.set_current("ja-JP")
       Locale.set_current("en_AU", "en_US", ...)
       Locale.set_current(Locale::Tag::Simple.new("ja", "JP"), ...)

set_default(tag)

Sets the default locale as the language tag (Locale::Tag‘s class or String(such as "ja_JP")).

- tag: the default language_tag
- Returns: self.

set_request(query_langs, cookie_langs, accept_language, accept_charset)

Sets a request values for lang/charset.

- query_langs: An Array of QUERY_STRING value "lang".
- cookie_langs: An Array of cookie value "lang".
- accept_language: The value of HTTP_ACCEPT_LANGUAGE
- accept_charset: The value of HTTP_ACCEPT_CHARSET

[Validate]