1 # Authors:: Matt Aimonetti (http://railsontherun.com/),
2 # Sven Fuchs (http://www.artweb-design.de),
3 # Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
4 # Saimon Moore (http://saimonmoore.net),
5 # Stephan Soller (http://www.arkanis-development.de/)
6 # Copyright:: Copyright (c) 2008 The Ruby i18n Team
8 require 'i18n/backend/simple'
9 require 'i18n/exceptions'
14 @
@default_locale = :'en'
15 @
@exception_handler = :default_exception_handler
18 # Returns the current backend. Defaults to +Backend::Simple+.
20 @
@backend ||= Backend
::Simple.new
23 # Sets the current backend. Used to set a custom backend.
28 # Returns the current default locale. Defaults to 'en'
33 # Sets the current default locale. Used to set a custom default locale.
34 def default_locale
=(locale
)
35 @
@default_locale = locale
38 # Returns the current locale. Defaults to I18n.default_locale.
40 Thread
.current
[:locale] ||= default_locale
43 # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
45 Thread
.current
[:locale] = locale
48 # Sets the exception handler.
49 def exception_handler
=(exception_handler
)
50 @
@exception_handler = exception_handler
53 # Allow clients to register paths providing translation data sources. The
54 # backend defines acceptable sources.
56 # E.g. the provided SimpleBackend accepts a list of paths to translation
57 # files which are either named *.rb and contain plain Ruby Hashes or are
58 # named *.yml and contain YAML data. So for the SimpleBackend clients may
59 # register translation files like this:
60 # I18n.load_path << 'path/to/locale/en.yml'
65 # Sets the load path instance. Custom implementations are expected to
66 # behave like a Ruby Array.
67 def load_path
=(load_path
)
68 @
@load_path = load_path
71 # Tells the backend to reload translations. Used in situations like the
72 # Rails development environment. Backends can implement whatever strategy
78 # Translates, pluralizes and interpolates a given key using a given locale,
79 # scope, and default, as well as interpolation values.
83 # Translation data is organized as a nested hash using the upper-level keys
84 # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
85 # <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
87 # Translations can be looked up at any level of this hash using the key argument
88 # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
89 # returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
91 # Key can be either a single key or a dot-separated key (both Strings and Symbols
92 # work). <em>E.g.</em>, the short format can be looked up using both:
93 # I18n.t 'date.formats.short'
94 # I18n.t :'date.formats.short'
96 # Scope can be either a single key, a dot-separated key or an array of keys
97 # or dot-separated keys. Keys and scopes can be combined freely. So these
98 # examples will all look up the same short date format:
99 # I18n.t 'date.formats.short'
100 # I18n.t 'formats.short', :scope => 'date'
101 # I18n.t 'short', :scope => 'date.formats'
102 # I18n.t 'short', :scope => %w(date formats)
106 # Translations can contain interpolation variables which will be replaced by
107 # values passed to #translate as part of the options hash, with the keys matching
108 # the interpolation variable names.
110 # <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
111 # value for the key +bar+ will be interpolated into the translation:
112 # I18n.t :foo, :bar => 'baz' # => 'foo baz'
116 # Translation data can contain pluralized translations. Pluralized translations
117 # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
119 # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
120 # pluralization rules. Other algorithms can be supported by custom backends.
122 # This returns the singular version of a pluralized translation:
123 # I18n.t :foo, :count => 1 # => 'Foo'
125 # These both return the plural version of a pluralized translation:
126 # I18n.t :foo, :count => 0 # => 'Foos'
127 # I18n.t :foo, :count => 2 # => 'Foos'
129 # The <tt>:count</tt> option can be used both for pluralization and interpolation.
130 # <em>E.g.</em>, with the translation
131 # <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
132 # be interpolated to the pluralized translation:
133 # I18n.t :foo, :count => 1 # => '1 foo'
137 # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
138 # I18n.t :foo, :default => 'default'
140 # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
141 # translation for <tt>:foo</tt> was found:
142 # I18n.t :foo, :default => :bar
144 # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
145 # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
146 # I18n.t :foo, :default => [:bar, 'default']
150 # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
151 # I18n.t [:foo, :bar]
153 # Can be used with dot-separated nested keys:
154 # I18n.t [:'baz.foo', :'baz.bar']
156 # Which is the same as using a scope option:
157 # I18n.t [:foo, :bar], :scope => :baz
158 def translate(key
, options
= {})
159 locale
= options
.delete(:locale) || I18n
.locale
160 backend
.translate(locale
, key
, options
)
161 rescue I18n
::ArgumentError => e
162 raise e
if options
[:raise]
163 send(@
@exception_handler, e
, locale
, key
, options
)
167 # Localizes certain objects, such as dates and numbers to local formatting.
168 def localize(object
, options
= {})
169 locale
= options
[:locale] || I18n
.locale
170 format
= options
[:format] || :default
171 backend
.localize(locale
, object
, format
)
176 # Handles exceptions raised in the backend. All exceptions except for
177 # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
178 # was caught and the option :raise is not set the handler returns an error
179 # message string containing the key/scope.
180 def default_exception_handler(exception
, locale
, key
, options
)
181 return exception
.message
if MissingTranslationData
=== exception
185 # Merges the given locale, key and scope into a single array of keys.
186 # Splits keys that contain dots into multiple keys. Makes sure all
188 def normalize_translation_keys(locale
, key
, scope
)
189 keys
= [locale
] + Array(scope
) + [key
]
190 keys
= keys
.map
{ |k
| k
.to_s
.split(/\./) }
191 keys
.flatten
.map
{ |k
| k
.to_sym
}