module 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.
Copyright © 2012 Kouhei Sutou <kou@clear-code.com>
License: Ruby’s or LGPL
This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <www.gnu.org/licenses/>.
win32_table.rb - Locale
table for win32
Copyright © 2008 Masao Mutoh <mutomasa at gmail.com>
You may redistribute it and/or modify it under the same license terms as Ruby.
Original: Ruby-GetText-Package-1.92.0.
$Id: win32_table.rb 27 2008-12-03 15:06:50Z mutoh $
locale/tag/posix.rb - Locale::Tag::Posix
Copyright © 2008 Masao Mutoh Copyright © 2018 Kouhei Sutou <kou@clear-code.com>
You may redistribute it and/or modify it under the same license terms as Ruby.
$Id: posix.rb 27 2008-12-03 15:06:50Z mutoh $
locale/tag/simple.rb - Locale::Tag::Simple
Copyright © 2008,2009 Masao Mutoh Copyright © 2018 Kouhei Sutou <kou@clear-code.com>
You may redistribute it and/or modify it under the same license terms as Ruby.
taglist.rb - Locale
module
Copyright © 2008 Masao Mutoh
You may redistribute it and/or modify it under the same license terms as Ruby.
$Id: taglist.rb 27 2008-12-03 15:06:50Z mutoh $
version - version information of Ruby-Locale
Copyright © 2008 Masao Mutoh Copyright © 2013-2020 Sutou Kouhei <kou@clear-code.com>
You may redistribute it and/or modify it under the same license terms as Ruby.
Constants
- VERSION
Public Instance Methods
Source
# File lib/locale.rb, line 212 def candidates(options = {}) opts = { :supported_language_tags => nil, :current => current, :type => :common, }.merge(options) Thread.current[:candidates_caches] ||= {} Thread.current[:candidates_caches][opts] ||= collect_candidates(opts[:type], opts[:current], opts[:supported_language_tags]) end
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
-
Source
# File lib/locale/driver/cgi.rb, line 145 def cgi=(cgi) set_cgi(cgi) cgi end
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
Source
# File lib/locale.rb, line 270 def charset driver_module.charset || "UTF-8" end
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.
Source
# File lib/locale.rb, line 276 def clear Thread.current[:current_languages] = nil Thread.current[:candidates_caches] = nil self end
Clear current locale.
-
Returns: self
Source
# File lib/locale.rb, line 286 def clear_all Thread.list.each do |thread| thread[:current_languages] = nil thread[:candidates_caches] = nil end self end
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
Source
# File lib/locale.rb, line 177 def current unless Thread.current[:current_languages] loc = driver_module.locales Thread.current[:current_languages] = loc ? loc : Locale::TagList.new([default]) end Thread.current[:current_languages] end
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).
Source
# File lib/locale.rb, line 164 def current=(tag) set_current(tag) Thread.current[:current_languages] end
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"
Source
# File lib/locale.rb, line 121 def default @@default_tag || DEFAULT_LANGUAGE_TAG end
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).
Source
# File lib/locale.rb, line 111 def default=(tag) set_default(tag) @@default_tag end
Same as Locale.set_default
.
-
locale: the default locale (
Locale::Tag
‘s class) or a String such as “ja-JP”. -
Returns: locale.
Source
# File lib/locale.rb, line 86 def driver_module Locale.init if @@driver_name.nil? Driver::MODULES[@@driver_name] end
Gets the driver module.
Usually you don’t need to call this method.
-
Returns: the driver module.
Source
# File lib/locale.rb, line 67 def init(opts = {}) if opts[:driver] require_driver opts[:driver] else if /cygwin|mingw|win32/ =~ RUBY_PLATFORM require_driver 'win32' elsif /java/ =~ RUBY_PLATFORM require_driver 'jruby' else require_driver 'posix' end end end
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)
-
Source
# File lib/locale/driver/cgi.rb, line 133 def set_cgi(cgi) set_request(cgi.params["lang"], cgi.cookies["lang"], cgi.accept_language, cgi.accept_charset) self end
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
Source
# File lib/locale.rb, line 139 def set_current(*tags) languages = nil if tags[0] languages = Locale::TagList.new tags.each do |tag| case tag when Locale::TagList languages.concat(tag) else languages << create_language_tag(tag) end end end Thread.current[:current_languages] = languages Thread.current[:candidates_caches] = nil self end
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"), ...)
Source
# File lib/locale.rb, line 98 def set_default(tag) Thread.list.each do |thread| thread[:current_languages] = nil thread[:candidates_caches] = nil end @@default_tag = create_language_tag(tag) self end
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.
Source
# File lib/locale/driver/cgi.rb, line 122 def set_request(query_langs, cookie_langs, accept_language, accept_charset) driver_module.set_request(query_langs, cookie_langs, accept_language, accept_charset) self end
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