76361bed90a4ffafb821ca5c43f590f55504fde6
[feedcatcher.git] / vendor / rails / activesupport / lib / active_support / vendor / i18n-0.1.3 / lib / i18n.rb
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
7 # License:: MIT
8 require 'i18n/backend/simple'
9 require 'i18n/exceptions'
10
11 module I18n
12 @@backend = nil
13 @@load_path = nil
14 @@default_locale = :'en'
15 @@exception_handler = :default_exception_handler
16
17 class << self
18 # Returns the current backend. Defaults to +Backend::Simple+.
19 def backend
20 @@backend ||= Backend::Simple.new
21 end
22
23 # Sets the current backend. Used to set a custom backend.
24 def backend=(backend)
25 @@backend = backend
26 end
27
28 # Returns the current default locale. Defaults to :'en'
29 def default_locale
30 @@default_locale
31 end
32
33 # Sets the current default locale. Used to set a custom default locale.
34 def default_locale=(locale)
35 @@default_locale = locale
36 end
37
38 # Returns the current locale. Defaults to I18n.default_locale.
39 def locale
40 Thread.current[:locale] ||= default_locale
41 end
42
43 # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
44 def locale=(locale)
45 Thread.current[:locale] = locale
46 end
47
48 # Returns an array of locales for which translations are available
49 def available_locales
50 backend.available_locales
51 end
52
53 # Sets the exception handler.
54 def exception_handler=(exception_handler)
55 @@exception_handler = exception_handler
56 end
57
58 # Allow clients to register paths providing translation data sources. The
59 # backend defines acceptable sources.
60 #
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'
66 def load_path
67 @@load_path ||= []
68 end
69
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
74 end
75
76 # Tells the backend to reload translations. Used in situations like the
77 # Rails development environment. Backends can implement whatever strategy
78 # is useful.
79 def reload!
80 backend.reload!
81 end
82
83 # Translates, pluralizes and interpolates a given key using a given locale,
84 # scope, and default, as well as interpolation values.
85 #
86 # *LOOKUP*
87 #
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>.
91 #
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>.
95 #
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'
100 #
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)
108 #
109 # *INTERPOLATION*
110 #
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.
114 #
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'
118 #
119 # *PLURALIZATION*
120 #
121 # Translation data can contain pluralized translations. Pluralized translations
122 # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
123 #
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.
126 #
127 # This returns the singular version of a pluralized translation:
128 # I18n.t :foo, :count => 1 # => 'Foo'
129 #
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'
133 #
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'
139 #
140 # *DEFAULTS*
141 #
142 # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
143 # I18n.t :foo, :default => 'default'
144 #
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
148 #
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']
152 #
153 # <b>BULK LOOKUP</b>
154 #
155 # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
156 # I18n.t [:foo, :bar]
157 #
158 # Can be used with dot-separated nested keys:
159 # I18n.t [:'baz.foo', :'baz.bar']
160 #
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)
169 end
170 alias :t :translate
171
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)
177 end
178 alias :l :localize
179
180 protected
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
187 raise exception
188 end
189
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
192 # keys are Symbols.
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
198 end
199 end