module Locale
def app_language_tags
def app_language_tags @@app_language_tags end
def candidates(options = {})
The type of language tag. :common, :rfc, :cldr, :posix and
* :type -
(e.g.1) ["fr_FR", "en_GB", "en_US", ...]
Default is nil if you don't need to restrict the locales.
restricts the locales which are supported by the library/application.
An Array of the language tags order by the priority. This option
* :supported_language_tags -
* options: options as a Hash or nil.
Usually, this method is used to find the locale data as the path(or a kind of IDs).
The default locale is added at the end of the list even if it isn't exist.
"en" is the default locale(You can change it using set_default).
then returns ["fr", "ja_JP", "en_US", "en-Latn-GB-VARIANT", "en_Latn_GB", "en_GB", "ja", "en"].
For example, if the current locales are ["fr", "ja_JP", "en_US", "en-Latn-GB-VARIANT"],
Returns the language tags which are variations of the current locales order by priority.
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
def cgi=(cgi)
* cgi: CGI object
This method is appeared when Locale.init(:driver => :cgi) is called.
Sets a CGI object.This is the convenient function of set_request().
def cgi=(cgi) set_cgi(cgi) cgi end
def charset
read only, so you can't set it by yourself.
This returns the current user/system charset. This value is
Gets the current charset.
def charset driver_module.charset || "UTF-8" end
def clear
Clear current locale.
def clear Thread.current[:current_languages] = nil Thread.current[:candidates_caches] = nil self end
def clear_all
Use Locale.default = nil to unset the default locale.
This doesn't clear the default and app_language_tags.
Clear all locales and charsets of all threads.
def clear_all Thread.list.each do |thread| thread[:current_languages] = nil thread[:candidates_caches] = nil end self end
def collect_candidates(type, tags, supported_tags) # :nodoc:
The result is shared from all threads.
collect tag candidates.
def collect_candidates(type, tags, supported_tags) # :nodoc: candidate_tags = tags.collect{|v| v.send("to_#{type}").candidates} default_tags = default.send("to_#{type}").candidates if app_language_tags app_tags = app_language_tags.collect{|v| v.send("to_#{type}")}.flatten.uniq end if supported_tags supported_tags = supported_tags.collect{|v| Locale::Tag.parse(v).send("to_#{type}")}.flatten.uniq end tags = [] unless candidate_tags.empty? (0...candidate_tags[0].size).each {|i| tags += candidate_tags.collect{|v| v[i]} } end tags += default_tags tags.uniq! all_tags = nil if app_tags if supported_tags all_tags = app_tags & supported_tags else all_tags = app_tags end elsif supported_tags all_tags = supported_tags end if all_tags tags &= all_tags tags = default_tags.uniq if tags.size == 0 end Locale::TagList.new(tags) end
def create_language_tag(tag) #:nodoc:
def create_language_tag(tag) #:nodoc: case tag when nil when Locale::Tag::Simple tag when Locale::TagList tag[0] else Locale::Tag.parse(tag) end end
def current
Usually, the programs should use Locale.candidates to find the correct locale, not this method.
This method returns the current language tags even if it isn't included in app_language_tags.
If the current locale is not set, this returns system/default locale.
Gets the current locales (Locale::Tag's class).
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
def current=(tag)
Locale.current = "ja-JP"
* Returns: an Array of the current locale (Locale::Tag's class).
* tag: the language tag such as "ja-JP"
Sets a current locale. This is a single argument version of Locale.set_current.
def current=(tag) set_current(tag) Thread.current[:current_languages] end
def default
If the default language tag is not set, this returns nil.
Gets the default locale(language tag).
def default @@default_tag || DEFAULT_LANGUAGE_TAG end
def default=(tag)
* locale: the default locale (Locale::Tag's class) or a String such as "ja-JP".
Same as Locale.set_default.
def default=(tag) set_default(tag) @@default_tag end
def driver_module
Usually you don't need to call this method.
Gets the driver module.
def driver_module Locale.init if @@driver_name.nil? Driver::MODULES[@@driver_name] end
def get #:nodoc:
Deprecated.
def get #:nodoc: current end
def init(opts = {})
(ex) Locale.init(:driver => :cgi)
nil if you use system locale.
* :driver - The driver. :cgi if you use Locale module with CGI,
* opts: Options as a Hash.
Don't call this, even if your application is only for WWW.
==== To Library authors:
call this once like: Locale.init(:driver => :cgi).
If your framework doesn't use ruby-locale and the application is for WWW,
==== To Application programers:
If your framework is for WWW, call this once like: Locale.init(:driver => :cgi).
==== For Framework designers/programers:
You need to call Locale.init(:driver => :cgi).
If you use this library with CGI or the kind of CGI.
this is called when Locale's methods are called.
Usually, you don't need to call this directly, because
Initialize Locale library.
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
def require_driver(name) #:nodoc:
def require_driver(name) #:nodoc: require "locale/driver/#{name}" @@driver_name = name.to_sym end
def set(tag) #:nodoc:
Deprecated.
def set(tag) #:nodoc: set_current(tag) end
def set_app_language_tags(*tags)
Note that the libraries/plugins shouldn't set this value.
Set nil if clear the value.
if "en" is not included in the language tags.
Use Locale.set_default() to set correct language
Locale.default value is used.
If the current locale is not included in app_language_tags,
application.
If you set a language tag, the application works as the single locale
to restrict the result but is the global setting.
This value is same with supported_language_tags in Locale.candidates
Set the language tags which is supported by the Application.
def set_app_language_tags(*tags) if tags[0] @@app_language_tags = tags.collect{|v| Locale::Tag.parse(v)} else @@app_language_tags = nil end clear_all self end
def set_cgi(cgi)
* cgi: CGI object
This method is appeared when Locale.init(:driver => :cgi) is called.
Sets a CGI object. This is the convenient function of set_request().
def set_cgi(cgi) set_request(cgi.params["lang"], cgi.cookies["lang"], cgi.accept_language, cgi.accept_charset) self end
def set_current(*tags)
Locale.set_current("en_AU", "en_US", ...)
Locale.set_current("ja-JP")
Locale.set_current("ja_JP.eucJP")
(e.g.)
* Returns: self
* charset: the charset (override the charset even if the locale name has charset) or nil.
clear current locales.
* tag: Locale::Language::Tag's class or the language tag as a String. nil if you need to
The system locale/default locale is used if the thread doesn't have current locales.
Each thread has a current locales.
Sets the locales of the current thread order by the priority.
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
def set_default(tag)
* tag: the default language_tag
(Locale::Tag's class or String(such as "ja_JP")).
Sets the default locale as the language tag
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
def set_request(query_langs, cookie_langs, accept_language, accept_charset)
* accept_language: The value of HTTP_ACCEPT_LANGUAGE
* cookie_langs: An Array of cookie value "lang".
* query_langs: An Array of QUERY_STRING value "lang".
Sets a request values for lang/charset.
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