2 require 'i18n/exceptions'
3 require 'i18n/interpolate/ruby'
6 autoload :Backend, 'i18n/backend'
7 autoload :Config, 'i18n/config'
8 autoload :Gettext, 'i18n/gettext'
9 autoload :Locale, 'i18n/locale'
10 autoload :Tests, 'i18n/tests'
12 RESERVED_KEYS = [:scope, :default, :separator, :resolve, :object, :fallback, :format, :cascade, :throw, :raise, :rescue_format]
13 RESERVED_KEYS_PATTERN = /%\{(#{RESERVED_KEYS.join("|")})\}/
16 # Gets I18n configuration object.
18 Thread.current[:i18n_config] ||= I18n::Config.new
21 # Sets I18n configuration object.
23 Thread.current[:i18n_config] = value
26 # Write methods which delegates to the configuration object
27 %w(locale backend default_locale available_locales default_separator
28 exception_handler load_path).each do |method|
29 module_eval <<-DELEGATORS, __FILE__, __LINE__ + 1
35 config.#{method} = (value)
40 # Tells the backend to reload translations. Used in situations like the
41 # Rails development environment. Backends can implement whatever strategy
44 config.backend.reload!
47 # Translates, pluralizes and interpolates a given key using a given locale,
48 # scope, and default, as well as interpolation values.
52 # Translation data is organized as a nested hash using the upper-level keys
53 # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
54 # <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
56 # Translations can be looked up at any level of this hash using the key argument
57 # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
58 # returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
60 # Key can be either a single key or a dot-separated key (both Strings and Symbols
61 # work). <em>E.g.</em>, the short format can be looked up using both:
62 # I18n.t 'date.formats.short'
63 # I18n.t :'date.formats.short'
65 # Scope can be either a single key, a dot-separated key or an array of keys
66 # or dot-separated keys. Keys and scopes can be combined freely. So these
67 # examples will all look up the same short date format:
68 # I18n.t 'date.formats.short'
69 # I18n.t 'formats.short', :scope => 'date'
70 # I18n.t 'short', :scope => 'date.formats'
71 # I18n.t 'short', :scope => %w(date formats)
75 # Translations can contain interpolation variables which will be replaced by
76 # values passed to #translate as part of the options hash, with the keys matching
77 # the interpolation variable names.
79 # <em>E.g.</em>, with a translation <tt>:foo => "foo %{bar}"</tt> the option
80 # value for the key +bar+ will be interpolated into the translation:
81 # I18n.t :foo, :bar => 'baz' # => 'foo baz'
85 # Translation data can contain pluralized translations. Pluralized translations
86 # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
88 # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
89 # pluralization rules. Other algorithms can be supported by custom backends.
91 # This returns the singular version of a pluralized translation:
92 # I18n.t :foo, :count => 1 # => 'Foo'
94 # These both return the plural version of a pluralized translation:
95 # I18n.t :foo, :count => 0 # => 'Foos'
96 # I18n.t :foo, :count => 2 # => 'Foos'
98 # The <tt>:count</tt> option can be used both for pluralization and interpolation.
99 # <em>E.g.</em>, with the translation
100 # <tt>:foo => ['%{count} foo', '%{count} foos']</tt>, count will
101 # be interpolated to the pluralized translation:
102 # I18n.t :foo, :count => 1 # => '1 foo'
106 # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
107 # I18n.t :foo, :default => 'default'
109 # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
110 # translation for <tt>:foo</tt> was found:
111 # I18n.t :foo, :default => :bar
113 # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
114 # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
115 # I18n.t :foo, :default => [:bar, 'default']
119 # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
120 # I18n.t [:foo, :bar]
122 # Can be used with dot-separated nested keys:
123 # I18n.t [:'baz.foo', :'baz.bar']
125 # Which is the same as using a scope option:
126 # I18n.t [:foo, :bar], :scope => :baz
130 # Both translations and defaults can be given as Ruby lambdas. Lambdas will be
131 # called and passed the key and options.
133 # E.g. assuming the key <tt>:salutation</tt> resolves to:
134 # lambda { |key, options| options[:gender] == 'm' ? "Mr. %{options[:name]}" : "Mrs. %{options[:name]}" }
136 # Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
138 # It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
139 # a cache layer is put in front of I18n.translate it will generate a cache key
140 # from the argument values passed to #translate. Therefor your lambdas should
141 # always return the same translations/values per unique combination of argument
144 options = args.last.is_a?(Hash) ? args.pop : {}
146 backend = config.backend
147 locale = options.delete(:locale) || config.locale
148 handling = options.delete(:throw) && :throw || options.delete(:raise) && :raise # TODO deprecate :raise
150 raise I18n::ArgumentError if key.is_a?(String) && key.empty?
152 result = catch(:exception) do
154 key.map { |k| backend.translate(locale, k, options) }
156 backend.translate(locale, key, options)
159 result.is_a?(MissingTranslation) ? handle_exception(handling, result, locale, key, options) : result
163 # Wrapper for <tt>translate</tt> that adds <tt>:raise => true</tt>. With
164 # this option, if no translation is found, it will raise <tt>I18n::MissingTranslationData</tt>
165 def translate!(key, options={})
166 translate(key, options.merge(:raise => true))
168 alias :t! :translate!
170 # Transliterates UTF-8 characters to ASCII. By default this method will
171 # transliterate only Latin strings to an ASCII approximation:
173 # I18n.transliterate("Ærøskøbing")
176 # I18n.transliterate("日本語")
179 # It's also possible to add support for per-locale transliterations. I18n
180 # expects transliteration rules to be stored at
181 # <tt>i18n.transliterate.rule</tt>.
183 # Transliteration rules can either be a Hash or a Proc. Procs must accept a
184 # single string argument. Hash rules inherit the default transliteration
185 # rules, while Procs do not.
189 # Setting a Hash in <locale>.yml:
197 # Setting a Hash using Ruby:
199 # store_translations(:de, :i18n => {
200 # :transliterate => {
210 # translit = lambda {|string| MyTransliterator.transliterate(string) }
211 # store_translations(:xx, :i18n => {:transliterate => {:rule => translit})
213 # Transliterating strings:
216 # I18n.transliterate("Jürgen") # => "Jurgen"
218 # I18n.transliterate("Jürgen") # => "Juergen"
219 # I18n.transliterate("Jürgen", :locale => :en) # => "Jurgen"
220 # I18n.transliterate("Jürgen", :locale => :de) # => "Juergen"
221 def transliterate(*args)
222 options = args.pop if args.last.is_a?(Hash)
224 locale = options && options.delete(:locale) || config.locale
225 handling = options && (options.delete(:throw) && :throw || options.delete(:raise) && :raise)
226 replacement = options && options.delete(:replacement)
227 config.backend.transliterate(locale, key, replacement)
228 rescue I18n::ArgumentError => exception
229 handle_exception(handling, exception, locale, key, options || {})
232 # Localizes certain objects, such as dates and numbers to local formatting.
233 def localize(object, options = {})
234 locale = options.delete(:locale) || config.locale
235 format = options.delete(:format) || :default
236 config.backend.localize(locale, object, format, options)
240 # Executes block with given I18n.locale set.
241 def with_locale(tmp_locale = nil)
243 current_locale = self.locale
244 self.locale = tmp_locale
248 self.locale = current_locale if tmp_locale
251 # Merges the given locale, key and scope into a single array of keys.
252 # Splits keys that contain dots into multiple keys. Makes sure all
254 def normalize_keys(locale, key, scope, separator = nil)
255 separator ||= I18n.default_separator
258 keys.concat normalize_key(locale, separator)
259 keys.concat normalize_key(scope, separator)
260 keys.concat normalize_key(key, separator)
264 # making these private until Ruby 1.9.2 can send to protected methods again
265 # see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
268 # Any exceptions thrown in translate will be sent to the @@exception_handler
269 # which can be a Symbol, a Proc or any other Object unless they're forced to
270 # be raised or thrown (MissingTranslation).
272 # If exception_handler is a Symbol then it will simply be sent to I18n as
273 # a method call. A Proc will simply be called. In any other case the
274 # method #call will be called on the exception_handler object.
278 # I18n.exception_handler = :default_exception_handler # this is the default
279 # I18n.default_exception_handler(exception, locale, key, options) # will be called like this
281 # I18n.exception_handler = lambda { |*args| ... } # a lambda
282 # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
284 # I18n.exception_handler = I18nExceptionHandler.new # an object
285 # I18n.exception_handler.call(exception, locale, key, options) # will be called like this
286 def handle_exception(handling, exception, locale, key, options)
289 raise(exception.respond_to?(:to_exception) ? exception.to_exception : exception)
291 throw :exception, exception
293 case handler = options[:exception_handler] || config.exception_handler
295 send(handler, exception, locale, key, options)
297 handler.call(exception, locale, key, options)
302 def normalize_key(key, separator)
303 normalized_key_cache[separator][key] ||=
306 key.map { |k| normalize_key(k, separator) }.flatten
308 keys = key.to_s.split(separator)
310 keys.map! { |k| k.to_sym }
315 def normalized_key_cache
316 @normalized_key_cache ||= Hash.new { |h,k| h[k] = {} }
319 # DEPRECATED. Use I18n.normalize_keys instead.
320 def normalize_translation_keys(locale, key, scope, separator = nil)
321 puts "I18n.normalize_translation_keys is deprecated. Please use the class I18n.normalize_keys instead."
322 normalize_keys(locale, key, scope, separator)
325 # DEPRECATED. Please use the I18n::ExceptionHandler class instead.
326 def default_exception_handler(exception, locale, key, options)
327 puts "I18n.default_exception_handler is deprecated. Please use the class I18n::ExceptionHandler instead " +
328 "(an instance of which is set to I18n.exception_handler by default)."
329 exception.is_a?(MissingTranslation) ? exception.message : raise(exception)