| Class | Globalize::Locale |
| In: |
vendor/plugins/globalize/lib/globalize/localization/locale.rb
|
| Parent: | Object |
Locale defines the currenctly active locale. You‘ll mostly use it like this:
Locale.set("en-US")
en is the code for English, and US is the country code. The country code is optional, but you’ll need to define it to get a lot of the localization features.
| code | [R] | |
| country | [R] | |
| currency_code | [RW] | |
| currency_decimal_sep | [RW] | |
| currency_format | [RW] | |
| date_format | [RW] | |
| decimal_sep | [RW] | |
| language | [R] | |
| number_grouping_scheme | [RW] | |
| thousands_sep | [RW] |
Returns the active locale.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 48
48: def self.active; @@active end
Is there an active locale?
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 26
26: def self.active?; !@@active.nil? end
Is the currently active language the base language?
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 76
76: def self.base?
77: active ? active.language == base_language : true
78: end
Returns the base language. Raises an exception if none is set.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 68
68: def self.base_language
69: @@base_language ? @@base_language :
70: (@@base_language_code ?
71: (@@base_language = Language.pick(@@base_language_code)) :
72: raise(NoBaseLanguageError, "base language must be defined"))
73: end
Clears the locale cache — used mostly for testing.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 43
43: def self.clear_cache
44: @@cache.clear
45: end
Returns the currently active country model or nil.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 91
91: def self.country
92: active? ? active.country : nil
93: end
Returns the currently active language model or nil.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 81
81: def self.language
82: active? ? active.language : nil
83: end
Returns the currently active language code or nil.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 86
86: def self.language_code
87: active? ? language.code : nil
88: end
Creates a new locale object by looking up an RFC 3066 code in the database.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 96
96: def initialize(code)
97: if code.nil?
98: return
99: end
100:
101: rfc = RFC_3066.parse(code)
102: @code = rfc.locale
103:
104: @language = Language.pick(rfc)
105: @country = Country.pick(rfc)
106:
107: setup_fields
108: end
This is the focal point of the class. Sets the locale in the familiar RFC 3066 format (see: www.faqs.org/rfcs/rfc3066.html). It can also take a Locale object. Set it to the nil object, to deactivate the locale.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 32
32: def self.set(locale)
33: if locale.kind_of? Locale
34: @@active = locale
35: elsif locale.nil?
36: @@active = nil
37: else
38: @@active = ( @@cache[locale] ||= Locale.new(locale) )
39: end
40: end
Sets the base language. The base language is the language that has complete coverage in the database. For instance, if you have a Category model with a name field, the base language is the language in which names are stored in the model itself, and not in the translations table.
Takes either a language code (valid RFC 3066 code like en or en-US) or a language object.
May be set with a language code in environment.rb, without accessing the db.
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 59
59: def self.set_base_language(lang)
60: if lang.kind_of? Language
61: @@base_language = lang
62: else
63: @@base_language_code = RFC_3066.parse lang
64: end
65: end
Sets the translation for key.
If language is given, define a translation using that language model, otherwise use the active language.
Multiple translation strings may be given, in order to define plural forms. In English, there are only two plural forms, singular and plural, so you would provide two strings at the most. The order is determined by the formula in the languages database. For English, the order is: singular form, then plural.
Example:
Locale.set_translation("There are %d items in your cart",
"There is one item in your cart", "There are %d items in your cart")
# File vendor/plugins/globalize/lib/globalize/localization/locale.rb, line 128
128: def self.set_translation(key, *options)
129: key = key.to_s.gsub('_', ' ') if key.kind_of? Symbol
130: if options.first.kind_of? Language
131: language = options.shift
132: else
133: language = self.language
134: end
135:
136: raise ArgumentError, "No translations given" if options.empty?
137: translator.set(key, language, *options)
138: end