Namespace

Included Modules

Locale

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.


Refer from activesupport-2.2.0.

Constants

ROOT
VERSION

Public Instance Methods

app_language_tags() click to toggle source

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

     # File lib/locale.rb, line 327
327:   def app_language_tags
328:     @@app_language_tags
329:   end
candidates(options = {}) click to toggle source

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

     # File lib/locale.rb, line 211
211:   def candidates(options = {})
212:     opts = {:supported_language_tags => nil, :current => current,
213:       :type => :common}.merge(options)
214: 
215:     if Thread.current[:candidates_caches]
216:       cache = Thread.current[:candidates_caches][opts.hash]
217:       return cache if cache
218:     else
219:       Thread.current[:candidates_caches] = {} 
220:     end
221:     Thread.current[:candidates_caches][opts.hash] =
222:       collect_candidates(opts[:type], opts[:current],
223:                          opts[:supported_language_tags])
224:   end
cgi=(cgi) click to toggle source

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

     # File lib/locale/driver/cgi.rb, line 132
132:   def cgi=(cgi)
133:     set_cgi(cgi)
134:     cgi
135:   end
charset() click to toggle source

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.

     # File lib/locale.rb, line 272
272:   def charset
273:     driver_module.charset || "UTF-8"
274:   end
clear() click to toggle source

Clear current locale.

  • Returns: self

     # File lib/locale.rb, line 279
279:   def clear
280:     Thread.current[:current_languages] = nil
281:     Thread.current[:candidates_caches] = nil
282:     self
283:   end
clear_all() click to toggle source

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

     # File lib/locale.rb, line 289
289:   def clear_all
290:     Thread.list.each do |thread|
291:       thread[:current_languages] = nil
292:       thread[:candidates_caches] = nil
293:     end
294:     memoize_clear
295:     self
296:   end
current() click to toggle source

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).

     # File lib/locale.rb, line 176
176:   def current
177:     unless Thread.current[:current_languages]
178:       loc = driver_module.locales
179:       Thread.current[:current_languages] = loc ? loc : Locale::TagList.new([default])
180:     end
181:     Thread.current[:current_languages]
182:   end
current=(tag) click to toggle source

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"
     # File lib/locale.rb, line 163
163:   def current=(tag)
164:     set_current(tag)
165:     Thread.current[:current_languages]
166:   end
default() click to toggle source

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).

     # File lib/locale.rb, line 125
125:   def default
126:     @@default_tag || DEFAULT_LANGUAGE_TAG
127:   end
default=(tag) click to toggle source

Same as Locale.set_default.

  • locale: the default locale (Locale::Tag’s class) or a String such as “ja-JP”.

  • Returns: locale.

     # File lib/locale.rb, line 115
115:   def default=(tag)
116:     set_default(tag)
117:     @@default_tag
118:   end
driver_module() click to toggle source

Gets the driver module.

Usually you don’t need to call this method.

  • Returns: the driver module.

    # File lib/locale.rb, line 88
88:   def driver_module 
89:     unless @@locale_driver_module
90:       Locale.init
91:     end
92:     @@locale_driver_module
93:   end
init(opts = {}) click to toggle source

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)
    # File lib/locale.rb, line 69
69:   def init(opts = {})
70:     if opts[:driver]
71:       require_driver opts[:driver]
72:     else
73:       if /cygwin|mingw|win32/ =~ RUBY_PLATFORM
74:         require_driver 'win32' 
75:       elsif /java/ =~ RUBY_PLATFORM
76:         require_driver 'jruby' 
77:       else
78:         require_driver 'posix' 
79:       end
80:     end
81:   end
set_app_language_tags(*tags) click to toggle source

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", ...)
     # File lib/locale.rb, line 315
315:   def set_app_language_tags(*tags)
316:     if tags[0]
317:       @@app_language_tags = tags.collect{|v| Locale::Tag.parse(v)}
318:     else
319:       @@app_language_tags = nil
320:     end
321:     
322:     clear_all
323:     self
324:   end
set_cgi(cgi) click to toggle source

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

     # File lib/locale/driver/cgi.rb, line 120
120:   def set_cgi(cgi)
121:     set_request(cgi.params["lang"], cgi.cookies["lang"],
122:                 cgi.accept_language, cgi.accept_charset)
123:     self
124:   end
set_current(*tags) click to toggle source

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"), ...)
     # File lib/locale.rb, line 143
143:   def set_current(*tags)
144:     languages = nil
145:     if tags[0]
146:       languages = Locale::TagList.new
147:       tags.each do |tag|
148:         languages << create_language_tag(tag)
149:       end
150:     end
151:     Thread.current[:current_languages] = languages
152:     Thread.current[:candidates_caches] = nil
153:     self
154:   end
set_default(tag) click to toggle source

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.

     # File lib/locale.rb, line 102
102:   def set_default(tag)
103:     Thread.list.each do |thread|
104:       thread[:current_languages] = nil
105:       thread[:candidates_caches] = nil
106:     end
107:     @@default_tag = create_language_tag(tag)
108:     self
109:   end
set_request(query_langs, cookie_langs, accept_language, accept_charset) click to toggle source

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

     # File lib/locale/driver/cgi.rb, line 109
109:   def set_request(query_langs, cookie_langs, accept_language, accept_charset)
110:     @@locale_driver_module.set_request(query_langs, cookie_langs, accept_language, accept_charset)
111:     self
112:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.