i18n 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of i18n might be problematic. Click here for more details.

data/CHANGELOG.textile CHANGED
@@ -1,57 +1,57 @@
1
- h1. Changelog
2
-
3
- h2. master
4
-
5
- * (no changes)
6
-
7
- h2. 0.2.0 (2009-07-12)
8
-
9
- * "Allow using Ruby 1.9 syntax for string interpolation (API addition)":http://github.com/svenfuchs/i18n/commit/c6e0b06d512f2af57199a843a1d8a40241b32861
10
- * "Allow configuring the default scope separator, allow to pass a custom scope separator(API addition)":http://github.com/svenfuchs/i18n/commit/5b75bfbc348061adc11e3790187a187275bfd471 (e.g. I18n.t(:'foo|bar', :separator => '|')
11
- * "Pass :format option to #translate for #localize more useful lambda support":http://github.com/svenfuchs/i18n/commit/e277711b3c844fe7589b8d3f9af0f7d1b969a273
12
- * "Refactor Simple backend #resolve to #default and #resolve for more consistency. Now allows to pass lambdas as defaults and re-resolve Symbols":http://github.com/svenfuchs/i18n/commit/8c4ce3d923ce5fa73e973fe28217e18165549aba
13
- * "Add lambda support to #translate (API addition)":http://github.com/svenfuchs/i18n/commit/c90e62d8f7d3d5b78f34cfe328d871b58884f115
14
- * "Add lambda support to #localize (API addition)":http://github.com/svenfuchs/i18n/commit/9d390afcf33f3f469bb95e6888147152f6cc7442
15
-
16
- h2. 0.1.3 (2009-02-27)
17
-
18
- * "Remove unnecessary string encoding handling in the i18n simple backend which made the backend break on Ruby 1.9":http://github.com/svenfuchs/i18n/commit/4c3a970783861a94f2e89f46714fb3434e4f4f8d
19
-
20
- h2. 0.1.2 (2009-01-09)
21
-
22
- * "added #available_locales (returns an array of locales for which translations are available)":http://github.com/svenfuchs/i18n/commit/411f8fe7c8f3f89e9b6b921fa62ed66cb92f3af4
23
- * "flatten load_path before using it so that a nested array of paths won't throw up":http://github.com/svenfuchs/i18n/commit/d473a068a2b90aba98135deb225d6eb6d8104d70
24
-
25
- h2. 0.1.1 (2008-11-20)
26
-
27
- * "Use :'en' as a default locale (in favor of :'en-US')":http://github.com/svenfuchs/i18n/commit/c4b10b246aecf7da78cb2568dd0d2ab7e6b8a230
28
- * "Add #reload! to Simple backend":http://github.com/svenfuchs/i18n/commit/36dd2bd9973b9e1559728749a9daafa44693e964
29
-
30
- h2. 0.1.0 (2008-10-25)
31
-
32
- * "Fix Simple backend to distinguish false from nil values":http://github.com/svenfuchs/i18n/commit/39d9a47da14b5f3ba126af48923af8c30e135166
33
- * "Add #load_path to public api, add initialize to simple backend and remove #load_translations from public api":http://github.com/svenfuchs/i18n/commit/c4c5649e6bc8f020f1aaf5a5470bde048e22c82d
34
- * "Speed up Backend::Simple#interpolate":http://github.com/svenfuchs/i18n/commit/9e1ac6bf8833304e036323ec9932b9f33c468a35
35
- * "Remove #populate and #store_translations from public API":http://github.com/svenfuchs/i18n/commit/f4e514a80be7feb509f66824ee311905e2940900
36
- * "Use :other instead of :many as a plural key":http://github.com/svenfuchs/i18n/commit/0f8f20a2552bf6a2aa758d8fdd62a7154e4a1bf6
37
- * "Use a class instead of a module for Simple backend":http://github.com/svenfuchs/i18n/commit/08f051aa61320c17debde24a83268bc74e33b995
38
- * "Make Simple backend #interpolate deal with non-ASCII string encodings":http://github.com/svenfuchs/i18n/commit/d84a3f3f55543c084d5dc5d1fed613b8df148789
39
- * "Fix default arrays of non-existant keys returning the default array":http://github.com/svenfuchs/i18n/commit/6c04ca86c87f97dc78f07c2a4023644e5ba8b839
40
-
41
- h2. Initial implementation (June/July 2008)
42
-
43
- Initial implementation by "Sven Fuchs":http://www.workingwithrails.com/person/9963-sven-fuchs based on previous discussion/consensus of the rails-i18n team (alphabetical order) and many others:
44
-
45
- * "Matt Aimonetti":http://railsontherun.com
46
- * "Sven Fuchs":http://www.workingwithrails.com/person/9963-sven-fuchs
47
- * "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
48
- * "Saimon Moore":http://saimonmoore.net
49
- * "Stephan Soller":http://www.arkanis-development.de
50
-
51
- h2. More information
52
-
53
- * "Homepage":http://rails-i18n.org
54
- * "Wiki":http://rails-i18n.org/wiki
55
- * "Mailinglist":http://groups.google.com/group/rails-i18n
56
- * "About the project/history":http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized
1
+ h1. Changelog
2
+
3
+ h2. master
4
+
5
+ * (no changes)
6
+
7
+ h2. 0.2.0 (2009-07-12)
8
+
9
+ * "Allow using Ruby 1.9 syntax for string interpolation (API addition)":http://github.com/svenfuchs/i18n/commit/c6e0b06d512f2af57199a843a1d8a40241b32861
10
+ * "Allow configuring the default scope separator, allow to pass a custom scope separator(API addition)":http://github.com/svenfuchs/i18n/commit/5b75bfbc348061adc11e3790187a187275bfd471 (e.g. I18n.t(:'foo|bar', :separator => '|')
11
+ * "Pass :format option to #translate for #localize more useful lambda support":http://github.com/svenfuchs/i18n/commit/e277711b3c844fe7589b8d3f9af0f7d1b969a273
12
+ * "Refactor Simple backend #resolve to #default and #resolve for more consistency. Now allows to pass lambdas as defaults and re-resolve Symbols":http://github.com/svenfuchs/i18n/commit/8c4ce3d923ce5fa73e973fe28217e18165549aba
13
+ * "Add lambda support to #translate (API addition)":http://github.com/svenfuchs/i18n/commit/c90e62d8f7d3d5b78f34cfe328d871b58884f115
14
+ * "Add lambda support to #localize (API addition)":http://github.com/svenfuchs/i18n/commit/9d390afcf33f3f469bb95e6888147152f6cc7442
15
+
16
+ h2. 0.1.3 (2009-02-27)
17
+
18
+ * "Remove unnecessary string encoding handling in the i18n simple backend which made the backend break on Ruby 1.9":http://github.com/svenfuchs/i18n/commit/4c3a970783861a94f2e89f46714fb3434e4f4f8d
19
+
20
+ h2. 0.1.2 (2009-01-09)
21
+
22
+ * "added #available_locales (returns an array of locales for which translations are available)":http://github.com/svenfuchs/i18n/commit/411f8fe7c8f3f89e9b6b921fa62ed66cb92f3af4
23
+ * "flatten load_path before using it so that a nested array of paths won't throw up":http://github.com/svenfuchs/i18n/commit/d473a068a2b90aba98135deb225d6eb6d8104d70
24
+
25
+ h2. 0.1.1 (2008-11-20)
26
+
27
+ * "Use :'en' as a default locale (in favor of :'en-US')":http://github.com/svenfuchs/i18n/commit/c4b10b246aecf7da78cb2568dd0d2ab7e6b8a230
28
+ * "Add #reload! to Simple backend":http://github.com/svenfuchs/i18n/commit/36dd2bd9973b9e1559728749a9daafa44693e964
29
+
30
+ h2. 0.1.0 (2008-10-25)
31
+
32
+ * "Fix Simple backend to distinguish false from nil values":http://github.com/svenfuchs/i18n/commit/39d9a47da14b5f3ba126af48923af8c30e135166
33
+ * "Add #load_path to public api, add initialize to simple backend and remove #load_translations from public api":http://github.com/svenfuchs/i18n/commit/c4c5649e6bc8f020f1aaf5a5470bde048e22c82d
34
+ * "Speed up Backend::Simple#interpolate":http://github.com/svenfuchs/i18n/commit/9e1ac6bf8833304e036323ec9932b9f33c468a35
35
+ * "Remove #populate and #store_translations from public API":http://github.com/svenfuchs/i18n/commit/f4e514a80be7feb509f66824ee311905e2940900
36
+ * "Use :other instead of :many as a plural key":http://github.com/svenfuchs/i18n/commit/0f8f20a2552bf6a2aa758d8fdd62a7154e4a1bf6
37
+ * "Use a class instead of a module for Simple backend":http://github.com/svenfuchs/i18n/commit/08f051aa61320c17debde24a83268bc74e33b995
38
+ * "Make Simple backend #interpolate deal with non-ASCII string encodings":http://github.com/svenfuchs/i18n/commit/d84a3f3f55543c084d5dc5d1fed613b8df148789
39
+ * "Fix default arrays of non-existant keys returning the default array":http://github.com/svenfuchs/i18n/commit/6c04ca86c87f97dc78f07c2a4023644e5ba8b839
40
+
41
+ h2. Initial implementation (June/July 2008)
42
+
43
+ Initial implementation by "Sven Fuchs":http://www.workingwithrails.com/person/9963-sven-fuchs based on previous discussion/consensus of the rails-i18n team (alphabetical order) and many others:
44
+
45
+ * "Matt Aimonetti":http://railsontherun.com
46
+ * "Sven Fuchs":http://www.workingwithrails.com/person/9963-sven-fuchs
47
+ * "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
48
+ * "Saimon Moore":http://saimonmoore.net
49
+ * "Stephan Soller":http://www.arkanis-development.de
50
+
51
+ h2. More information
52
+
53
+ * "Homepage":http://rails-i18n.org
54
+ * "Wiki":http://rails-i18n.org/wiki
55
+ * "Mailinglist":http://groups.google.com/group/rails-i18n
56
+ * "About the project/history":http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized
57
57
  * "Initial API Intro":http://www.artweb-design.de/2008/7/18/the-ruby-on-rails-i18n-core-api
data/MIT-LICENSE CHANGED
@@ -1,20 +1,20 @@
1
- Copyright (c) 2008 The Ruby I18n team
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1
+ Copyright (c) 2008 The Ruby I18n team
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile CHANGED
@@ -1,20 +1,20 @@
1
- h1. Ruby I18n gem
2
-
3
- I18n and localization solution for Ruby.
4
-
5
- For information please refer to http://rails-i18n.org
6
-
7
- h2. Authors
8
-
9
- * "Matt Aimonetti":http://railsontherun.com
10
- * "Sven Fuchs":http://www.artweb-design.de
11
- * "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
12
- * "Saimon Moore":http://saimonmoore.net
13
- * "Stephan Soller":http://www.arkanis-development.de
14
-
15
- h2. License
16
-
17
- MIT License. See the included MIT-LICENCE file.
18
-
19
-
20
-
1
+ h1. Ruby I18n gem
2
+
3
+ I18n and localization solution for Ruby.
4
+
5
+ For information please refer to http://rails-i18n.org
6
+
7
+ h2. Authors
8
+
9
+ * "Matt Aimonetti":http://railsontherun.com
10
+ * "Sven Fuchs":http://www.artweb-design.de
11
+ * "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
12
+ * "Saimon Moore":http://saimonmoore.net
13
+ * "Stephan Soller":http://www.arkanis-development.de
14
+
15
+ h2. License
16
+
17
+ MIT License. See the included MIT-LICENCE file.
18
+
19
+
20
+
data/Rakefile CHANGED
@@ -1,21 +1,20 @@
1
- task :default => [:test]
2
-
3
- task :test do
4
- ruby "test/all.rb"
5
- end
6
-
7
- begin
8
- require 'jeweler'
9
- Jeweler::Tasks.new do |s|
10
- s.name = "i18n"
11
- s.rubyforge_project = "i18n"
12
- s.summary = "New wave Internationalization support for Ruby"
13
- s.email = "rails-i18n@googlegroups.com"
14
- s.homepage = "http://rails-i18n.org"
15
- s.description = "Add Internationalization support to your Ruby application."
16
- s.authors = ['Sven Fuchs', 'Joshua Harvey', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore']
17
- s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
18
- end
19
- rescue LoadError
20
- puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
1
+ task :default => [:test]
2
+
3
+ task :test do
4
+ ruby "test/all.rb"
21
5
  end
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |s|
10
+ s.name = "i18n"
11
+ s.summary = "New wave Internationalization support for Ruby"
12
+ s.email = "rails-i18n@googlegroups.com"
13
+ s.homepage = "http://rails-i18n.org"
14
+ s.description = "Add Internationalization support to your Ruby application."
15
+ s.authors = ['Sven Fuchs', 'Joshua Harvey', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore']
16
+ s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/lib/i18n.rb CHANGED
@@ -1,229 +1,229 @@
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
- require 'i18n/string'
11
-
12
- module I18n
13
- @@backend = nil
14
- @@load_path = nil
15
- @@default_locale = :'en'
16
- @@default_separator = '.'
17
- @@exception_handler = :default_exception_handler
18
-
19
- class << self
20
- # Returns the current backend. Defaults to +Backend::Simple+.
21
- def backend
22
- @@backend ||= Backend::Simple.new
23
- end
24
-
25
- # Sets the current backend. Used to set a custom backend.
26
- def backend=(backend)
27
- @@backend = backend
28
- end
29
-
30
- # Returns the current default locale. Defaults to :'en'
31
- def default_locale
32
- @@default_locale
33
- end
34
-
35
- # Sets the current default locale. Used to set a custom default locale.
36
- def default_locale=(locale)
37
- @@default_locale = locale
38
- end
39
-
40
- # Returns the current locale. Defaults to I18n.default_locale.
41
- def locale
42
- Thread.current[:locale] ||= default_locale
43
- end
44
-
45
- # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
46
- def locale=(locale)
47
- Thread.current[:locale] = locale
48
- end
49
-
50
- # Returns an array of locales for which translations are available
51
- def available_locales
52
- backend.available_locales
53
- end
54
-
55
- # Returns the current default scope separator. Defaults to '.'
56
- def default_separator
57
- @@default_separator
58
- end
59
-
60
- # Sets the current default scope separator.
61
- def default_separator=(separator)
62
- @@default_separator = separator
63
- end
64
-
65
- # Sets the exception handler.
66
- def exception_handler=(exception_handler)
67
- @@exception_handler = exception_handler
68
- end
69
-
70
- # Allow clients to register paths providing translation data sources. The
71
- # backend defines acceptable sources.
72
- #
73
- # E.g. the provided SimpleBackend accepts a list of paths to translation
74
- # files which are either named *.rb and contain plain Ruby Hashes or are
75
- # named *.yml and contain YAML data. So for the SimpleBackend clients may
76
- # register translation files like this:
77
- # I18n.load_path << 'path/to/locale/en.yml'
78
- def load_path
79
- @@load_path ||= []
80
- end
81
-
82
- # Sets the load path instance. Custom implementations are expected to
83
- # behave like a Ruby Array.
84
- def load_path=(load_path)
85
- @@load_path = load_path
86
- end
87
-
88
- # Tells the backend to reload translations. Used in situations like the
89
- # Rails development environment. Backends can implement whatever strategy
90
- # is useful.
91
- def reload!
92
- backend.reload!
93
- end
94
-
95
- # Translates, pluralizes and interpolates a given key using a given locale,
96
- # scope, and default, as well as interpolation values.
97
- #
98
- # *LOOKUP*
99
- #
100
- # Translation data is organized as a nested hash using the upper-level keys
101
- # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
102
- # <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
103
- #
104
- # Translations can be looked up at any level of this hash using the key argument
105
- # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
106
- # returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
107
- #
108
- # Key can be either a single key or a dot-separated key (both Strings and Symbols
109
- # work). <em>E.g.</em>, the short format can be looked up using both:
110
- # I18n.t 'date.formats.short'
111
- # I18n.t :'date.formats.short'
112
- #
113
- # Scope can be either a single key, a dot-separated key or an array of keys
114
- # or dot-separated keys. Keys and scopes can be combined freely. So these
115
- # examples will all look up the same short date format:
116
- # I18n.t 'date.formats.short'
117
- # I18n.t 'formats.short', :scope => 'date'
118
- # I18n.t 'short', :scope => 'date.formats'
119
- # I18n.t 'short', :scope => %w(date formats)
120
- #
121
- # *INTERPOLATION*
122
- #
123
- # Translations can contain interpolation variables which will be replaced by
124
- # values passed to #translate as part of the options hash, with the keys matching
125
- # the interpolation variable names.
126
- #
127
- # <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
128
- # value for the key +bar+ will be interpolated into the translation:
129
- # I18n.t :foo, :bar => 'baz' # => 'foo baz'
130
- #
131
- # *PLURALIZATION*
132
- #
133
- # Translation data can contain pluralized translations. Pluralized translations
134
- # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
135
- #
136
- # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
137
- # pluralization rules. Other algorithms can be supported by custom backends.
138
- #
139
- # This returns the singular version of a pluralized translation:
140
- # I18n.t :foo, :count => 1 # => 'Foo'
141
- #
142
- # These both return the plural version of a pluralized translation:
143
- # I18n.t :foo, :count => 0 # => 'Foos'
144
- # I18n.t :foo, :count => 2 # => 'Foos'
145
- #
146
- # The <tt>:count</tt> option can be used both for pluralization and interpolation.
147
- # <em>E.g.</em>, with the translation
148
- # <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
149
- # be interpolated to the pluralized translation:
150
- # I18n.t :foo, :count => 1 # => '1 foo'
151
- #
152
- # *DEFAULTS*
153
- #
154
- # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
155
- # I18n.t :foo, :default => 'default'
156
- #
157
- # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
158
- # translation for <tt>:foo</tt> was found:
159
- # I18n.t :foo, :default => :bar
160
- #
161
- # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
162
- # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
163
- # I18n.t :foo, :default => [:bar, 'default']
164
- #
165
- # *BULK LOOKUP*
166
- #
167
- # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
168
- # I18n.t [:foo, :bar]
169
- #
170
- # Can be used with dot-separated nested keys:
171
- # I18n.t [:'baz.foo', :'baz.bar']
172
- #
173
- # Which is the same as using a scope option:
174
- # I18n.t [:foo, :bar], :scope => :baz
175
- #
176
- # *LAMBDAS*
177
- #
178
- # Both translations and defaults can be given as Ruby lambdas. Lambdas will be
179
- # called and passed the key and options.
180
- #
181
- # E.g. assuming the key <tt>:salutation</tt> resolves to:
182
- # lambda { |key, options| options[:gender] == 'm' ? "Mr. {{options[:name]}}" : "Mrs. {{options[:name]}}" }
183
- #
184
- # Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
185
- #
186
- # It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
187
- # a cache layer is put in front of I18n.translate it will generate a cache key
188
- # from the argument values passed to #translate. Therefor your lambdas should
189
- # always return the same translations/values per unique combination of argument
190
- # values.
191
- def translate(*args)
192
- options = args.last.is_a?(Hash) ? args.pop : {}
193
- key = args.shift
194
- locale = options.delete(:locale) || I18n.locale
195
- backend.translate(locale, key, options)
196
- rescue I18n::ArgumentError => e
197
- raise e if options[:raise]
198
- send(@@exception_handler, e, locale, key, options)
199
- end
200
- alias :t :translate
201
-
202
- # Localizes certain objects, such as dates and numbers to local formatting.
203
- def localize(object, options = {})
204
- locale = options[:locale] || I18n.locale
205
- format = options[:format] || :default
206
- backend.localize(locale, object, format)
207
- end
208
- alias :l :localize
209
-
210
- protected
211
- # Handles exceptions raised in the backend. All exceptions except for
212
- # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
213
- # was caught and the option :raise is not set the handler returns an error
214
- # message string containing the key/scope.
215
- def default_exception_handler(exception, locale, key, options)
216
- return exception.message if MissingTranslationData === exception
217
- raise exception
218
- end
219
-
220
- # Merges the given locale, key and scope into a single array of keys.
221
- # Splits keys that contain dots into multiple keys. Makes sure all
222
- # keys are Symbols.
223
- def normalize_translation_keys(locale, key, scope, separator = nil)
224
- keys = [locale] + Array(scope) + Array(key)
225
- keys = keys.map { |k| k.to_s.split(separator || I18n.default_separator) }
226
- keys.flatten.map { |k| k.to_sym }
227
- end
228
- end
229
- end
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
+ require 'i18n/string'
11
+
12
+ module I18n
13
+ @@backend = nil
14
+ @@load_path = nil
15
+ @@default_locale = :'en'
16
+ @@default_separator = '.'
17
+ @@exception_handler = :default_exception_handler
18
+
19
+ class << self
20
+ # Returns the current backend. Defaults to +Backend::Simple+.
21
+ def backend
22
+ @@backend ||= Backend::Simple.new
23
+ end
24
+
25
+ # Sets the current backend. Used to set a custom backend.
26
+ def backend=(backend)
27
+ @@backend = backend
28
+ end
29
+
30
+ # Returns the current default locale. Defaults to :'en'
31
+ def default_locale
32
+ @@default_locale
33
+ end
34
+
35
+ # Sets the current default locale. Used to set a custom default locale.
36
+ def default_locale=(locale)
37
+ @@default_locale = locale
38
+ end
39
+
40
+ # Returns the current locale. Defaults to I18n.default_locale.
41
+ def locale
42
+ Thread.current[:locale] ||= default_locale
43
+ end
44
+
45
+ # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
46
+ def locale=(locale)
47
+ Thread.current[:locale] = locale
48
+ end
49
+
50
+ # Returns an array of locales for which translations are available
51
+ def available_locales
52
+ backend.available_locales
53
+ end
54
+
55
+ # Returns the current default scope separator. Defaults to '.'
56
+ def default_separator
57
+ @@default_separator
58
+ end
59
+
60
+ # Sets the current default scope separator.
61
+ def default_separator=(separator)
62
+ @@default_separator = separator
63
+ end
64
+
65
+ # Sets the exception handler.
66
+ def exception_handler=(exception_handler)
67
+ @@exception_handler = exception_handler
68
+ end
69
+
70
+ # Allow clients to register paths providing translation data sources. The
71
+ # backend defines acceptable sources.
72
+ #
73
+ # E.g. the provided SimpleBackend accepts a list of paths to translation
74
+ # files which are either named *.rb and contain plain Ruby Hashes or are
75
+ # named *.yml and contain YAML data. So for the SimpleBackend clients may
76
+ # register translation files like this:
77
+ # I18n.load_path << 'path/to/locale/en.yml'
78
+ def load_path
79
+ @@load_path ||= []
80
+ end
81
+
82
+ # Sets the load path instance. Custom implementations are expected to
83
+ # behave like a Ruby Array.
84
+ def load_path=(load_path)
85
+ @@load_path = load_path
86
+ end
87
+
88
+ # Tells the backend to reload translations. Used in situations like the
89
+ # Rails development environment. Backends can implement whatever strategy
90
+ # is useful.
91
+ def reload!
92
+ backend.reload!
93
+ end
94
+
95
+ # Translates, pluralizes and interpolates a given key using a given locale,
96
+ # scope, and default, as well as interpolation values.
97
+ #
98
+ # *LOOKUP*
99
+ #
100
+ # Translation data is organized as a nested hash using the upper-level keys
101
+ # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
102
+ # <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
103
+ #
104
+ # Translations can be looked up at any level of this hash using the key argument
105
+ # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
106
+ # returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
107
+ #
108
+ # Key can be either a single key or a dot-separated key (both Strings and Symbols
109
+ # work). <em>E.g.</em>, the short format can be looked up using both:
110
+ # I18n.t 'date.formats.short'
111
+ # I18n.t :'date.formats.short'
112
+ #
113
+ # Scope can be either a single key, a dot-separated key or an array of keys
114
+ # or dot-separated keys. Keys and scopes can be combined freely. So these
115
+ # examples will all look up the same short date format:
116
+ # I18n.t 'date.formats.short'
117
+ # I18n.t 'formats.short', :scope => 'date'
118
+ # I18n.t 'short', :scope => 'date.formats'
119
+ # I18n.t 'short', :scope => %w(date formats)
120
+ #
121
+ # *INTERPOLATION*
122
+ #
123
+ # Translations can contain interpolation variables which will be replaced by
124
+ # values passed to #translate as part of the options hash, with the keys matching
125
+ # the interpolation variable names.
126
+ #
127
+ # <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
128
+ # value for the key +bar+ will be interpolated into the translation:
129
+ # I18n.t :foo, :bar => 'baz' # => 'foo baz'
130
+ #
131
+ # *PLURALIZATION*
132
+ #
133
+ # Translation data can contain pluralized translations. Pluralized translations
134
+ # are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
135
+ #
136
+ # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
137
+ # pluralization rules. Other algorithms can be supported by custom backends.
138
+ #
139
+ # This returns the singular version of a pluralized translation:
140
+ # I18n.t :foo, :count => 1 # => 'Foo'
141
+ #
142
+ # These both return the plural version of a pluralized translation:
143
+ # I18n.t :foo, :count => 0 # => 'Foos'
144
+ # I18n.t :foo, :count => 2 # => 'Foos'
145
+ #
146
+ # The <tt>:count</tt> option can be used both for pluralization and interpolation.
147
+ # <em>E.g.</em>, with the translation
148
+ # <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
149
+ # be interpolated to the pluralized translation:
150
+ # I18n.t :foo, :count => 1 # => '1 foo'
151
+ #
152
+ # *DEFAULTS*
153
+ #
154
+ # This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
155
+ # I18n.t :foo, :default => 'default'
156
+ #
157
+ # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
158
+ # translation for <tt>:foo</tt> was found:
159
+ # I18n.t :foo, :default => :bar
160
+ #
161
+ # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
162
+ # or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
163
+ # I18n.t :foo, :default => [:bar, 'default']
164
+ #
165
+ # *BULK LOOKUP*
166
+ #
167
+ # This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
168
+ # I18n.t [:foo, :bar]
169
+ #
170
+ # Can be used with dot-separated nested keys:
171
+ # I18n.t [:'baz.foo', :'baz.bar']
172
+ #
173
+ # Which is the same as using a scope option:
174
+ # I18n.t [:foo, :bar], :scope => :baz
175
+ #
176
+ # *LAMBDAS*
177
+ #
178
+ # Both translations and defaults can be given as Ruby lambdas. Lambdas will be
179
+ # called and passed the key and options.
180
+ #
181
+ # E.g. assuming the key <tt>:salutation</tt> resolves to:
182
+ # lambda { |key, options| options[:gender] == 'm' ? "Mr. {{options[:name]}}" : "Mrs. {{options[:name]}}" }
183
+ #
184
+ # Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
185
+ #
186
+ # It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
187
+ # a cache layer is put in front of I18n.translate it will generate a cache key
188
+ # from the argument values passed to #translate. Therefor your lambdas should
189
+ # always return the same translations/values per unique combination of argument
190
+ # values.
191
+ def translate(*args)
192
+ options = args.last.is_a?(Hash) ? args.pop : {}
193
+ key = args.shift
194
+ locale = options.delete(:locale) || I18n.locale
195
+ backend.translate(locale, key, options)
196
+ rescue I18n::ArgumentError => e
197
+ raise e if options[:raise]
198
+ send(@@exception_handler, e, locale, key, options)
199
+ end
200
+ alias :t :translate
201
+
202
+ # Localizes certain objects, such as dates and numbers to local formatting.
203
+ def localize(object, options = {})
204
+ locale = options[:locale] || I18n.locale
205
+ format = options[:format] || :default
206
+ backend.localize(locale, object, format)
207
+ end
208
+ alias :l :localize
209
+
210
+ protected
211
+ # Handles exceptions raised in the backend. All exceptions except for
212
+ # MissingTranslationData exceptions are re-raised. When a MissingTranslationData
213
+ # was caught and the option :raise is not set the handler returns an error
214
+ # message string containing the key/scope.
215
+ def default_exception_handler(exception, locale, key, options)
216
+ return exception.message if MissingTranslationData === exception
217
+ raise exception
218
+ end
219
+
220
+ # Merges the given locale, key and scope into a single array of keys.
221
+ # Splits keys that contain dots into multiple keys. Makes sure all
222
+ # keys are Symbols.
223
+ def normalize_translation_keys(locale, key, scope, separator = nil)
224
+ keys = [locale] + Array(scope) + Array(key)
225
+ keys = keys.map { |k| k.to_s.split(separator || I18n.default_separator) }
226
+ keys.flatten.map { |k| k.to_sym }
227
+ end
228
+ end
229
+ end