Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / vendor / i18n-0.0.1 / 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 # Sets the exception handler.
49 def exception_handler=(exception_handler)
50 @@exception_handler = exception_handler
51 end
52
53 # Allow clients to register paths providing translation data sources. The
54 # backend defines acceptable sources.
55 #
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'
61 def load_path
62 @@load_path ||= []
63 end
64
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
69 end
70
71 # Tells the backend to reload translations. Used in situations like the
72 # Rails development environment. Backends can implement whatever strategy
73 # is useful.
74 def reload!
75 backend.reload!
76 end
77
78 # Translates, pluralizes and interpolates a given key using a given locale,
79 # scope, and default, as well as interpolation values.
80 #
81 # *LOOKUP*
82 #
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>.
86 #
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>.
90 #
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'
95 #
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)
103 #
104 # *INTERPOLATION*
105 #
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.
109 #
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'
113 #
114 # *PLURALIZATION*
115 #
116 # Translation data can contain pluralized translations. Pluralized translations
117 # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
118 #
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.
121 #
122 # This returns the singular version of a pluralized translation:
123 # I18n.t :foo, :count => 1 # => 'Foo'
124 #
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'
128 #
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'
134 #
135 # *DEFAULTS*
136 #
137 # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
138 # I18n.t :foo, :default => 'default'
139 #
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
143 #
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']
147 #
148 # <b>BULK LOOKUP</b>
149 #
150 # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
151 # I18n.t [:foo, :bar]
152 #
153 # Can be used with dot-separated nested keys:
154 # I18n.t [:'baz.foo', :'baz.bar']
155 #
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)
164 end
165 alias :t :translate
166
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)
172 end
173 alias :l :localize
174
175 protected
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
182 raise exception
183 end
184
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
187 # keys are Symbols.
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 }
192 end
193 end
194 end