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 # Returns an array of locales for which translations are available
50 backend
.available_locales
53 # Sets the exception handler.
54 def exception_handler
=(exception_handler
)
55 @
@exception_handler = exception_handler
58 # Allow clients to register paths providing translation data sources. The
59 # backend defines acceptable sources.
61 # E.g. the provided SimpleBackend accepts a list of paths to translation
62 # files which are either named *.rb and contain plain Ruby Hashes or are
63 # named *.yml and contain YAML data. So for the SimpleBackend clients may
64 # register translation files like this:
65 # I18n.load_path << 'path/to/locale/en.yml'
70 # Sets the load path instance. Custom implementations are expected to
71 # behave like a Ruby Array.
72 def load_path
=(load_path
)
73 @
@load_path = load_path
76 # Tells the backend to reload translations. Used in situations like the
77 # Rails development environment. Backends can implement whatever strategy
83 # Translates, pluralizes and interpolates a given key using a given locale,
84 # scope, and default, as well as interpolation values.
88 # Translation data is organized as a nested hash using the upper-level keys
89 # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
90 # <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
92 # Translations can be looked up at any level of this hash using the key argument
93 # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
94 # returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
96 # Key can be either a single key or a dot-separated key (both Strings and Symbols
97 # work). <em>E.g.</em>, the short format can be looked up using both:
98 # I18n.t 'date.formats.short'
99 # I18n.t :'date.formats.short'
101 # Scope can be either a single key, a dot-separated key or an array of keys
102 # or dot-separated keys. Keys and scopes can be combined freely. So these
103 # examples will all look up the same short date format:
104 # I18n.t 'date.formats.short'
105 # I18n.t 'formats.short', :scope => 'date'
106 # I18n.t 'short', :scope => 'date.formats'
107 # I18n.t 'short', :scope => %w(date formats)
111 # Translations can contain interpolation variables which will be replaced by
112 # values passed to #translate as part of the options hash, with the keys matching
113 # the interpolation variable names.
115 # <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
116 # value for the key +bar+ will be interpolated into the translation:
117 # I18n.t :foo, :bar => 'baz' # => 'foo baz'
121 # Translation data can contain pluralized translations. Pluralized translations
122 # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
124 # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
125 # pluralization rules. Other algorithms can be supported by custom backends.
127 # This returns the singular version of a pluralized translation:
128 # I18n.t :foo, :count => 1 # => 'Foo'
130 # These both return the plural version of a pluralized translation:
131 # I18n.t :foo, :count => 0 # => 'Foos'
132 # I18n.t :foo, :count => 2 # => 'Foos'
134 # The <tt>:count</tt> option can be used both for pluralization and interpolation.
135 # <em>E.g.</em>, with the translation
136 # <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
137 # be interpolated to the pluralized translation:
138 # I18n.t :foo, :count => 1 # => '1 foo'
142 # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
143 # I18n.t :foo, :default => 'default'
145 # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
146 # translation for <tt>:foo</tt> was found:
147 # I18n.t :foo, :default => :bar
149 # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
150 # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
151 # I18n.t :foo, :default => [:bar, 'default']
155 # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
156 # I18n.t [:foo, :bar]
158 # Can be used with dot-separated nested keys:
159 # I18n.t [:'baz.foo', :'baz.bar']
161 # Which is the same as using a scope option:
162 # I18n.t [:foo, :bar], :scope => :baz
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
)
172 # Localizes certain objects, such as dates and numbers to local formatting.
173 def localize(object
, options
= {})
174 locale
= options
[:locale] || I18n
.locale
175 format
= options
[:format] || :default
176 backend
.localize(locale
, object
, format
)
181 # Handles exceptions raised in the backend. All exceptions except for
182 # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
183 # was caught and the option :raise is not set the handler returns an error
184 # message string containing the key/scope.
185 def default_exception_handler(exception
, locale
, key
, options
)
186 return exception
.message
if MissingTranslationData
=== exception
190 # Merges the given locale, key and scope into a single array of keys.
191 # Splits keys that contain dots into multiple keys. Makes sure all
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
}