| translate | -> | t |
| localize | -> | l |
Returns an array of locales for which translations are available
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 49
49: def available_locales
50: backend.available_locales
51: end
Returns the current backend. Defaults to +Backend::Simple+.
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 19
19: def backend
20: @@backend ||= Backend::Simple.new
21: end
Sets the exception handler.
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 54
54: def exception_handler=(exception_handler)
55: @@exception_handler = exception_handler
56: end
Allow clients to register paths providing translation data sources. The backend defines acceptable sources.
E.g. the provided SimpleBackend accepts a list of paths to translation files which are either named *.rb and contain plain Ruby Hashes or are named *.yml and contain YAML data. So for the SimpleBackend clients may register translation files like this:
I18n.load_path << 'path/to/locale/en.yml'
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 66
66: def load_path
67: @@load_path ||= []
68: end
Sets the load path instance. Custom implementations are expected to behave like a Ruby Array.
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 72
72: def load_path=(load_path)
73: @@load_path = load_path
74: end
Returns the current locale. Defaults to I18n.default_locale.
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 39
39: def locale
40: Thread.current[:locale] ||= default_locale
41: end
Localizes certain objects, such as dates and numbers to local formatting.
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 173
173: def localize(object, options = {})
174: locale = options[:locale] || I18n.locale
175: format = options[:format] || :default
176: backend.localize(locale, object, format)
177: end
Translates, pluralizes and interpolates a given key using a given locale, scope, and default, as well as interpolation values.
LOOKUP
Translation data is organized as a nested hash using the upper-level keys as namespaces. E.g., ActionView ships with the translation: :date => {:formats => {:short => "%b %d"}}.
Translations can be looked up at any level of this hash using the key argument and the scope option. E.g., in this example I18n.t :date returns the whole translations hash {:formats => {:short => "%b %d"}}.
Key can be either a single key or a dot-separated key (both Strings and Symbols work). E.g., the short format can be looked up using both:
I18n.t 'date.formats.short' I18n.t :'date.formats.short'
Scope can be either a single key, a dot-separated key or an array of keys or dot-separated keys. Keys and scopes can be combined freely. So these examples will all look up the same short date format:
I18n.t 'date.formats.short' I18n.t 'formats.short', :scope => 'date' I18n.t 'short', :scope => 'date.formats' I18n.t 'short', :scope => %w(date formats)
INTERPOLATION
Translations can contain interpolation variables which will be replaced by values passed to translate as part of the options hash, with the keys matching the interpolation variable names.
E.g., with a translation :foo => "foo {{bar}}" the option value for the key bar will be interpolated into the translation:
I18n.t :foo, :bar => 'baz' # => 'foo baz'
PLURALIZATION
Translation data can contain pluralized translations. Pluralized translations are arrays of singluar/plural versions of translations like [‘Foo’, ‘Foos’].
Note that I18n::Backend::Simple only supports an algorithm for English pluralization rules. Other algorithms can be supported by custom backends.
This returns the singular version of a pluralized translation:
I18n.t :foo, :count => 1 # => 'Foo'
These both return the plural version of a pluralized translation:
I18n.t :foo, :count => 0 # => 'Foos' I18n.t :foo, :count => 2 # => 'Foos'
The :count option can be used both for pluralization and interpolation. E.g., with the translation :foo => [’{{count}} foo’, ’{{count}} foos’], count will be interpolated to the pluralized translation:
I18n.t :foo, :count => 1 # => '1 foo'
DEFAULTS
This returns the translation for :foo or default if no translation was found:
I18n.t :foo, :default => 'default'
This returns the translation for :foo or the translation for :bar if no translation for :foo was found:
I18n.t :foo, :default => :bar
Returns the translation for :foo or the translation for :bar or default if no translations for :foo and :bar were found.
I18n.t :foo, :default => [:bar, 'default']
BULK LOOKUP
This returns an array with the translations for :foo and :bar.
I18n.t [:foo, :bar]
Can be used with dot-separated nested keys:
I18n.t [:'baz.foo', :'baz.bar']
Which is the same as using a scope option:
I18n.t [:foo, :bar], :scope => :baz
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 163
163: def translate(key, options = {})
164: locale = options.delete(:locale) || I18n.locale
165: backend.translate(locale, key, options)
166: rescue I18n::ArgumentError => e
167: raise e if options[:raise]
168: send(@@exception_handler, e, locale, key, options)
169: end
Handles exceptions raised in the backend. All exceptions except for MissingTranslationData exceptions are re-raised. When a MissingTranslationData was caught and the option :raise is not set the handler returns an error message string containing the key/scope.
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 185
185: def default_exception_handler(exception, locale, key, options)
186: return exception.message if MissingTranslationData === exception
187: raise exception
188: end
Merges the given locale, key and scope into a single array of keys. Splits keys that contain dots into multiple keys. Makes sure all keys are Symbols.
# File vendor/rails/activesupport/lib/active_support/vendor/i18n-0.1.3/lib/i18n.rb, line 193
193: def normalize_translation_keys(locale, key, scope)
194: keys = [locale] + Array(scope) + [key]
195: keys = keys.map { |k| k.to_s.split(/\./) }
196: keys.flatten.map { |k| k.to_sym }
197: end