pepe-i18n 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.textile +57 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +42 -0
- data/Rakefile +21 -0
- data/VERSION +1 -0
- data/lib/i18n.rb +270 -0
- data/lib/i18n/backend/base.rb +251 -0
- data/lib/i18n/backend/cache.rb +71 -0
- data/lib/i18n/backend/chain.rb +64 -0
- data/lib/i18n/backend/fallbacks.rb +53 -0
- data/lib/i18n/backend/gettext.rb +65 -0
- data/lib/i18n/backend/pluralization.rb +56 -0
- data/lib/i18n/backend/simple.rb +23 -0
- data/lib/i18n/exceptions.rb +61 -0
- data/lib/i18n/gettext.rb +25 -0
- data/lib/i18n/helpers/gettext.rb +35 -0
- data/lib/i18n/locale/fallbacks.rb +100 -0
- data/lib/i18n/locale/tag.rb +27 -0
- data/lib/i18n/locale/tag/parents.rb +24 -0
- data/lib/i18n/locale/tag/rfc4646.rb +78 -0
- data/lib/i18n/locale/tag/simple.rb +44 -0
- data/lib/i18n/string.rb +95 -0
- data/test/all.rb +5 -0
- data/test/api/basics.rb +15 -0
- data/test/api/interpolation.rb +85 -0
- data/test/api/lambda.rb +52 -0
- data/test/api/link.rb +47 -0
- data/test/api/localization/date.rb +65 -0
- data/test/api/localization/date_time.rb +63 -0
- data/test/api/localization/lambda.rb +26 -0
- data/test/api/localization/time.rb +63 -0
- data/test/api/pluralization.rb +37 -0
- data/test/api/translation.rb +51 -0
- data/test/backend/cache/cache_test.rb +57 -0
- data/test/backend/chain/api_test.rb +80 -0
- data/test/backend/chain/chain_test.rb +64 -0
- data/test/backend/fallbacks/api_test.rb +79 -0
- data/test/backend/fallbacks/fallbacks_test.rb +29 -0
- data/test/backend/pluralization/api_test.rb +81 -0
- data/test/backend/pluralization/pluralization_test.rb +39 -0
- data/test/backend/simple/all.rb +5 -0
- data/test/backend/simple/api_test.rb +90 -0
- data/test/backend/simple/lookup_test.rb +24 -0
- data/test/backend/simple/setup.rb +147 -0
- data/test/backend/simple/translations_test.rb +89 -0
- data/test/fixtures/locales/de.po +61 -0
- data/test/fixtures/locales/en.rb +3 -0
- data/test/fixtures/locales/en.yml +3 -0
- data/test/fixtures/locales/plurals.rb +112 -0
- data/test/gettext/api_test.rb +78 -0
- data/test/gettext/backend_test.rb +35 -0
- data/test/i18n_exceptions_test.rb +97 -0
- data/test/i18n_load_path_test.rb +23 -0
- data/test/i18n_test.rb +163 -0
- data/test/locale/fallbacks_test.rb +128 -0
- data/test/locale/tag/rfc4646_test.rb +147 -0
- data/test/locale/tag/simple_test.rb +35 -0
- data/test/string_test.rb +94 -0
- data/test/test_helper.rb +71 -0
- data/test/with_options.rb +34 -0
- metadata +151 -0
data/CHANGELOG.textile
ADDED
@@ -0,0 +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
|
57
|
+
* "Initial API Intro":http://www.artweb-design.de/2008/7/18/the-ruby-on-rails-i18n-core-api
|
data/MIT-LICENSE
ADDED
@@ -0,0 +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
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
h1. Ruby I18n
|
2
|
+
|
3
|
+
Ruby Internationalization and localization solution.
|
4
|
+
|
5
|
+
Features:
|
6
|
+
|
7
|
+
* translation and localization
|
8
|
+
* interpolation of values to translations (Ruby 1.9 compatible syntax)
|
9
|
+
* pluralization (CLDR compatible)
|
10
|
+
* flexible defaults
|
11
|
+
* bulk lookup
|
12
|
+
* lambdas as translation data
|
13
|
+
* custom key/scope separator
|
14
|
+
* custom exception handlers
|
15
|
+
* extensible architecture with a swappable backend
|
16
|
+
|
17
|
+
Experimental, pluggable features:
|
18
|
+
|
19
|
+
* lambda pluralizers stored as translation data
|
20
|
+
* RFC4647 compliant locale fallbacks (with optional RFC4646 locale validation)
|
21
|
+
* backend cache
|
22
|
+
|
23
|
+
For more information and lots of resources see: "http://rails-i18n.org/wiki":http://rails-i18n.org/wiki
|
24
|
+
|
25
|
+
h2. Install
|
26
|
+
|
27
|
+
gem install i18n
|
28
|
+
|
29
|
+
h2. Authors
|
30
|
+
|
31
|
+
* "Sven Fuchs":http://www.artweb-design.de
|
32
|
+
* "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
|
33
|
+
* "Stephan Soller":http://www.arkanis-development.de
|
34
|
+
* "Saimon Moore":http://saimonmoore.net
|
35
|
+
* "Matt Aimonetti":http://railsontherun.com
|
36
|
+
|
37
|
+
h2. License
|
38
|
+
|
39
|
+
MIT License. See the included MIT-LICENCE file.
|
40
|
+
|
41
|
+
|
42
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
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"
|
21
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
data/lib/i18n.rb
ADDED
@@ -0,0 +1,270 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Authors:: Matt Aimonetti (http://railsontherun.com/),
|
4
|
+
# Sven Fuchs (http://www.artweb-design.de),
|
5
|
+
# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
|
6
|
+
# Saimon Moore (http://saimonmoore.net),
|
7
|
+
# Stephan Soller (http://www.arkanis-development.de/)
|
8
|
+
# Copyright:: Copyright (c) 2008 The Ruby i18n Team
|
9
|
+
# License:: MIT
|
10
|
+
require 'i18n/backend/simple'
|
11
|
+
require 'i18n/exceptions'
|
12
|
+
require 'i18n/string'
|
13
|
+
|
14
|
+
module I18n
|
15
|
+
@@backend = nil
|
16
|
+
@@load_path = nil
|
17
|
+
@@default_locale = :en
|
18
|
+
@@default_separator = '.'
|
19
|
+
@@exception_handler = :default_exception_handler
|
20
|
+
|
21
|
+
class << self
|
22
|
+
# Returns the current backend. Defaults to +Backend::Simple+.
|
23
|
+
def backend
|
24
|
+
@@backend ||= Backend::Simple.new
|
25
|
+
end
|
26
|
+
|
27
|
+
# Sets the current backend. Used to set a custom backend.
|
28
|
+
def backend=(backend)
|
29
|
+
@@backend = backend
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the current default locale. Defaults to :'en'
|
33
|
+
def default_locale
|
34
|
+
@@default_locale
|
35
|
+
end
|
36
|
+
|
37
|
+
# Sets the current default locale. Used to set a custom default locale.
|
38
|
+
def default_locale=(locale)
|
39
|
+
@@default_locale = locale.to_sym rescue nil
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the current locale. Defaults to I18n.default_locale.
|
43
|
+
def locale
|
44
|
+
Thread.current[:locale] ||= default_locale
|
45
|
+
end
|
46
|
+
|
47
|
+
# Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
|
48
|
+
def locale=(locale)
|
49
|
+
Thread.current[:locale] = locale.to_sym rescue nil
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns an array of locales for which translations are available.
|
53
|
+
# Unless you explicitely set the these through I18n.available_locales=
|
54
|
+
# the call will be delegated to the backend and memoized on the I18n module.
|
55
|
+
def available_locales
|
56
|
+
@@available_locales ||= backend.available_locales
|
57
|
+
end
|
58
|
+
|
59
|
+
# Sets the available locales.
|
60
|
+
def available_locales=(locales)
|
61
|
+
@@available_locales = locales
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns the current default scope separator. Defaults to '.'
|
65
|
+
def default_separator
|
66
|
+
@@default_separator
|
67
|
+
end
|
68
|
+
|
69
|
+
# Sets the current default scope separator.
|
70
|
+
def default_separator=(separator)
|
71
|
+
@@default_separator = separator
|
72
|
+
end
|
73
|
+
|
74
|
+
# Sets the exception handler.
|
75
|
+
def exception_handler=(exception_handler)
|
76
|
+
@@exception_handler = exception_handler
|
77
|
+
end
|
78
|
+
|
79
|
+
# Allow clients to register paths providing translation data sources. The
|
80
|
+
# backend defines acceptable sources.
|
81
|
+
#
|
82
|
+
# E.g. the provided SimpleBackend accepts a list of paths to translation
|
83
|
+
# files which are either named *.rb and contain plain Ruby Hashes or are
|
84
|
+
# named *.yml and contain YAML data. So for the SimpleBackend clients may
|
85
|
+
# register translation files like this:
|
86
|
+
# I18n.load_path << 'path/to/locale/en.yml'
|
87
|
+
def load_path
|
88
|
+
@@load_path ||= []
|
89
|
+
end
|
90
|
+
|
91
|
+
# Sets the load path instance. Custom implementations are expected to
|
92
|
+
# behave like a Ruby Array.
|
93
|
+
def load_path=(load_path)
|
94
|
+
@@load_path = load_path
|
95
|
+
end
|
96
|
+
|
97
|
+
# Tells the backend to reload translations. Used in situations like the
|
98
|
+
# Rails development environment. Backends can implement whatever strategy
|
99
|
+
# is useful.
|
100
|
+
def reload!
|
101
|
+
backend.reload!
|
102
|
+
end
|
103
|
+
|
104
|
+
# Translates, pluralizes and interpolates a given key using a given locale,
|
105
|
+
# scope, and default, as well as interpolation values.
|
106
|
+
#
|
107
|
+
# *LOOKUP*
|
108
|
+
#
|
109
|
+
# Translation data is organized as a nested hash using the upper-level keys
|
110
|
+
# as namespaces. <em>E.g.</em>, ActionView ships with the translation:
|
111
|
+
# <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
|
112
|
+
#
|
113
|
+
# Translations can be looked up at any level of this hash using the key argument
|
114
|
+
# and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
|
115
|
+
# returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
|
116
|
+
#
|
117
|
+
# Key can be either a single key or a dot-separated key (both Strings and Symbols
|
118
|
+
# work). <em>E.g.</em>, the short format can be looked up using both:
|
119
|
+
# I18n.t 'date.formats.short'
|
120
|
+
# I18n.t :'date.formats.short'
|
121
|
+
#
|
122
|
+
# Scope can be either a single key, a dot-separated key or an array of keys
|
123
|
+
# or dot-separated keys. Keys and scopes can be combined freely. So these
|
124
|
+
# examples will all look up the same short date format:
|
125
|
+
# I18n.t 'date.formats.short'
|
126
|
+
# I18n.t 'formats.short', :scope => 'date'
|
127
|
+
# I18n.t 'short', :scope => 'date.formats'
|
128
|
+
# I18n.t 'short', :scope => %w(date formats)
|
129
|
+
#
|
130
|
+
# *INTERPOLATION*
|
131
|
+
#
|
132
|
+
# Translations can contain interpolation variables which will be replaced by
|
133
|
+
# values passed to #translate as part of the options hash, with the keys matching
|
134
|
+
# the interpolation variable names.
|
135
|
+
#
|
136
|
+
# <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
|
137
|
+
# value for the key +bar+ will be interpolated into the translation:
|
138
|
+
# I18n.t :foo, :bar => 'baz' # => 'foo baz'
|
139
|
+
#
|
140
|
+
# *PLURALIZATION*
|
141
|
+
#
|
142
|
+
# Translation data can contain pluralized translations. Pluralized translations
|
143
|
+
# are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
|
144
|
+
#
|
145
|
+
# Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
|
146
|
+
# pluralization rules. Other algorithms can be supported by custom backends.
|
147
|
+
#
|
148
|
+
# This returns the singular version of a pluralized translation:
|
149
|
+
# I18n.t :foo, :count => 1 # => 'Foo'
|
150
|
+
#
|
151
|
+
# These both return the plural version of a pluralized translation:
|
152
|
+
# I18n.t :foo, :count => 0 # => 'Foos'
|
153
|
+
# I18n.t :foo, :count => 2 # => 'Foos'
|
154
|
+
#
|
155
|
+
# The <tt>:count</tt> option can be used both for pluralization and interpolation.
|
156
|
+
# <em>E.g.</em>, with the translation
|
157
|
+
# <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
|
158
|
+
# be interpolated to the pluralized translation:
|
159
|
+
# I18n.t :foo, :count => 1 # => '1 foo'
|
160
|
+
#
|
161
|
+
# *DEFAULTS*
|
162
|
+
#
|
163
|
+
# This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
|
164
|
+
# I18n.t :foo, :default => 'default'
|
165
|
+
#
|
166
|
+
# This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
|
167
|
+
# translation for <tt>:foo</tt> was found:
|
168
|
+
# I18n.t :foo, :default => :bar
|
169
|
+
#
|
170
|
+
# Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
|
171
|
+
# or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
|
172
|
+
# I18n.t :foo, :default => [:bar, 'default']
|
173
|
+
#
|
174
|
+
# *BULK LOOKUP*
|
175
|
+
#
|
176
|
+
# This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
|
177
|
+
# I18n.t [:foo, :bar]
|
178
|
+
#
|
179
|
+
# Can be used with dot-separated nested keys:
|
180
|
+
# I18n.t [:'baz.foo', :'baz.bar']
|
181
|
+
#
|
182
|
+
# Which is the same as using a scope option:
|
183
|
+
# I18n.t [:foo, :bar], :scope => :baz
|
184
|
+
#
|
185
|
+
# *LAMBDAS*
|
186
|
+
#
|
187
|
+
# Both translations and defaults can be given as Ruby lambdas. Lambdas will be
|
188
|
+
# called and passed the key and options.
|
189
|
+
#
|
190
|
+
# E.g. assuming the key <tt>:salutation</tt> resolves to:
|
191
|
+
# lambda { |key, options| options[:gender] == 'm' ? "Mr. {{options[:name]}}" : "Mrs. {{options[:name]}}" }
|
192
|
+
#
|
193
|
+
# Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
|
194
|
+
#
|
195
|
+
# It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
|
196
|
+
# a cache layer is put in front of I18n.translate it will generate a cache key
|
197
|
+
# from the argument values passed to #translate. Therefor your lambdas should
|
198
|
+
# always return the same translations/values per unique combination of argument
|
199
|
+
# values.
|
200
|
+
def translate(*args)
|
201
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
202
|
+
key = args.shift
|
203
|
+
locale = options.delete(:locale) || I18n.locale
|
204
|
+
backend.translate(locale, key, options)
|
205
|
+
rescue I18n::ArgumentError => exception
|
206
|
+
raise exception if options[:raise]
|
207
|
+
handle_exception(exception, locale, key, options)
|
208
|
+
end
|
209
|
+
alias :t :translate
|
210
|
+
|
211
|
+
def translate!(key, options = {})
|
212
|
+
translate(key, options.merge( :raise => true ))
|
213
|
+
end
|
214
|
+
alias :t! :translate!
|
215
|
+
|
216
|
+
# Localizes certain objects, such as dates and numbers to local formatting.
|
217
|
+
def localize(object, options = {})
|
218
|
+
locale = options[:locale] || I18n.locale
|
219
|
+
format = options[:format] || :default
|
220
|
+
backend.localize(locale, object, format)
|
221
|
+
end
|
222
|
+
alias :l :localize
|
223
|
+
|
224
|
+
protected
|
225
|
+
|
226
|
+
# Handles exceptions raised in the backend. All exceptions except for
|
227
|
+
# MissingTranslationData exceptions are re-raised. When a MissingTranslationData
|
228
|
+
# was caught and the option :raise is not set the handler returns an error
|
229
|
+
# message string containing the key/scope.
|
230
|
+
def default_exception_handler(exception, locale, key, options)
|
231
|
+
return exception.message if MissingTranslationData === exception
|
232
|
+
raise exception
|
233
|
+
end
|
234
|
+
|
235
|
+
# Any exceptions thrown in translate will be sent to the @@exception_handler
|
236
|
+
# which can be a Symbol, a Proc or any other Object.
|
237
|
+
#
|
238
|
+
# If exception_handler is a Symbol then it will simply be sent to I18n as
|
239
|
+
# a method call. A Proc will simply be called. In any other case the
|
240
|
+
# method #call will be called on the exception_handler object.
|
241
|
+
#
|
242
|
+
# Examples:
|
243
|
+
#
|
244
|
+
# I18n.exception_handler = :default_exception_handler # this is the default
|
245
|
+
# I18n.default_exception_handler(exception, locale, key, options) # will be called like this
|
246
|
+
#
|
247
|
+
# I18n.exception_handler = lambda { |*args| ... } # a lambda
|
248
|
+
# I18n.exception_handler.call(exception, locale, key, options) # will be called like this
|
249
|
+
#
|
250
|
+
# I18n.exception_handler = I18nExceptionHandler.new # an object
|
251
|
+
# I18n.exception_handler.call(exception, locale, key, options) # will be called like this
|
252
|
+
def handle_exception(exception, locale, key, options)
|
253
|
+
case @@exception_handler
|
254
|
+
when Symbol
|
255
|
+
send(@@exception_handler, exception, locale, key, options)
|
256
|
+
else
|
257
|
+
@@exception_handler.call(exception, locale, key, options)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
# Merges the given locale, key and scope into a single array of keys.
|
262
|
+
# Splits keys that contain dots into multiple keys. Makes sure all
|
263
|
+
# keys are Symbols.
|
264
|
+
def normalize_translation_keys(locale, key, scope, separator = nil)
|
265
|
+
keys = [locale] + Array(scope) + Array(key)
|
266
|
+
keys = keys.map { |k| k.to_s.split(separator || I18n.default_separator) }
|
267
|
+
keys.flatten.map { |k| k.to_sym }
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
@@ -0,0 +1,251 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module I18n
|
6
|
+
module Backend
|
7
|
+
class Base
|
8
|
+
RESERVED_KEYS = [:scope, :default, :separator]
|
9
|
+
INTERPOLATION_SYNTAX_PATTERN = /(\\)?\{\{([^\}]+)\}\}/
|
10
|
+
|
11
|
+
# Accepts a list of paths to translation files. Loads translations from
|
12
|
+
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
|
13
|
+
# for details.
|
14
|
+
def load_translations(*filenames)
|
15
|
+
filenames.each { |filename| load_file(filename) }
|
16
|
+
end
|
17
|
+
|
18
|
+
# Stores translations for the given locale in memory.
|
19
|
+
# This uses a deep merge for the translations hash, so existing
|
20
|
+
# translations will be overwritten by new ones only at the deepest
|
21
|
+
# level of the hash.
|
22
|
+
def store_translations(locale, data)
|
23
|
+
merge_translations(locale, data)
|
24
|
+
end
|
25
|
+
|
26
|
+
def translate(locale, key, options = {})
|
27
|
+
raise InvalidLocale.new(locale) if locale.nil?
|
28
|
+
return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)
|
29
|
+
|
30
|
+
count, scope, default, separator = options.values_at(:count, *RESERVED_KEYS)
|
31
|
+
values = options.reject { |name, value| RESERVED_KEYS.include?(name) }
|
32
|
+
|
33
|
+
entry = lookup(locale, key, scope, separator)
|
34
|
+
entry = entry.nil? ? default(locale, key, default, options) : resolve(locale, key, entry, options)
|
35
|
+
|
36
|
+
raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?
|
37
|
+
entry = pluralize(locale, entry, count)
|
38
|
+
entry = interpolate(locale, entry, values)
|
39
|
+
entry
|
40
|
+
end
|
41
|
+
|
42
|
+
# Acts the same as +strftime+, but uses a localized version of the
|
43
|
+
# format string. Takes a key from the date/time formats translations as
|
44
|
+
# a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
|
45
|
+
def localize(locale, object, format = :default, options = {})
|
46
|
+
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
|
47
|
+
|
48
|
+
if Symbol === format
|
49
|
+
key = format
|
50
|
+
type = object.respond_to?(:sec) ? 'time' : 'date'
|
51
|
+
format = lookup(locale, :"#{type}.formats.#{key}")
|
52
|
+
raise(MissingTranslationData.new(locale, key, options)) if format.nil?
|
53
|
+
end
|
54
|
+
|
55
|
+
format = resolve(locale, object, format, options)
|
56
|
+
format = format.to_s.gsub(/%[aAbBp]/) do |match|
|
57
|
+
case match
|
58
|
+
when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
|
59
|
+
when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
|
60
|
+
when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
|
61
|
+
when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
|
62
|
+
when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
object.strftime(format)
|
67
|
+
end
|
68
|
+
|
69
|
+
def initialized?
|
70
|
+
@initialized ||= false
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns an array of locales for which translations are available
|
74
|
+
# ignoring the reserved translation meta data key :i18n.
|
75
|
+
def available_locales
|
76
|
+
init_translations unless initialized?
|
77
|
+
translations.inject([]) do |locales, (locale, data)|
|
78
|
+
locales << locale unless (data.keys - [:i18n]).empty?
|
79
|
+
locales
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def reload!
|
84
|
+
@initialized = false
|
85
|
+
@translations = nil
|
86
|
+
end
|
87
|
+
|
88
|
+
protected
|
89
|
+
def init_translations
|
90
|
+
load_translations(*I18n.load_path.flatten)
|
91
|
+
@initialized = true
|
92
|
+
end
|
93
|
+
|
94
|
+
def translations
|
95
|
+
@translations ||= {}
|
96
|
+
end
|
97
|
+
|
98
|
+
# Looks up a translation from the translations hash. Returns nil if
|
99
|
+
# eiher key is nil, or locale, scope or key do not exist as a key in the
|
100
|
+
# nested translations hash. Splits keys or scopes containing dots
|
101
|
+
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
|
102
|
+
# <tt>%w(currency format)</tt>.
|
103
|
+
def lookup(locale, key, scope = [], separator = nil)
|
104
|
+
return unless key
|
105
|
+
init_translations unless initialized?
|
106
|
+
keys = I18n.send(:normalize_translation_keys, locale, key, scope, separator)
|
107
|
+
keys.inject(translations) do |result, key|
|
108
|
+
key = key.to_sym
|
109
|
+
if result.respond_to?(:has_key?) and result.has_key?(key)
|
110
|
+
result[key]
|
111
|
+
else
|
112
|
+
return nil
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# Evaluates defaults.
|
118
|
+
# If given subject is an Array, it walks the array and returns the
|
119
|
+
# first translation that can be resolved. Otherwise it tries to resolve
|
120
|
+
# the translation directly.
|
121
|
+
def default(locale, object, subject, options = {})
|
122
|
+
options = options.dup.reject { |key, value| key == :default }
|
123
|
+
case subject
|
124
|
+
when Array
|
125
|
+
subject.each do |subject|
|
126
|
+
result = resolve(locale, object, subject, options) and return result
|
127
|
+
end and nil
|
128
|
+
else
|
129
|
+
resolve(locale, object, subject, options)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# Resolves a translation.
|
134
|
+
# If the given subject is a Symbol, it will be translated with the
|
135
|
+
# given options. If it is a Proc then it will be evaluated. All other
|
136
|
+
# subjects will be returned directly.
|
137
|
+
def resolve(locale, object, subject, options = {})
|
138
|
+
case subject
|
139
|
+
when Symbol
|
140
|
+
translate(locale, subject, options)
|
141
|
+
when Proc
|
142
|
+
resolve(locale, object, subject.call(object, options), options = {})
|
143
|
+
else
|
144
|
+
subject
|
145
|
+
end
|
146
|
+
rescue MissingTranslationData
|
147
|
+
nil
|
148
|
+
end
|
149
|
+
|
150
|
+
# Picks a translation from an array according to English pluralization
|
151
|
+
# rules. It will pick the first translation if count is not equal to 1
|
152
|
+
# and the second translation if it is equal to 1. Other backends can
|
153
|
+
# implement more flexible or complex pluralization rules.
|
154
|
+
def pluralize(locale, entry, count)
|
155
|
+
return entry unless entry.is_a?(Hash) and count
|
156
|
+
|
157
|
+
key = :zero if count == 0 && entry.has_key?(:zero)
|
158
|
+
key ||= count == 1 ? :one : :other
|
159
|
+
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
|
160
|
+
entry[key]
|
161
|
+
end
|
162
|
+
|
163
|
+
# Interpolates values into a given string.
|
164
|
+
#
|
165
|
+
# interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
|
166
|
+
# # => "file test.txt opened by {{user}}"
|
167
|
+
#
|
168
|
+
# Note that you have to double escape the <tt>\\</tt> when you want to escape
|
169
|
+
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
|
170
|
+
# interpolation).
|
171
|
+
def interpolate(locale, string, values = {})
|
172
|
+
return string unless string.is_a?(String) && !values.empty?
|
173
|
+
|
174
|
+
s = string.gsub(INTERPOLATION_SYNTAX_PATTERN) do
|
175
|
+
escaped, key = $1, $2.to_sym
|
176
|
+
if escaped
|
177
|
+
"{{#{key}}}"
|
178
|
+
elsif RESERVED_KEYS.include?(key)
|
179
|
+
raise ReservedInterpolationKey.new(key, string)
|
180
|
+
else
|
181
|
+
"%{#{key}}"
|
182
|
+
end
|
183
|
+
end
|
184
|
+
values.each { |key, value| values[key] = value.call if interpolate_lambda?(value, s, key) }
|
185
|
+
s % values
|
186
|
+
|
187
|
+
rescue KeyError => e
|
188
|
+
raise MissingInterpolationArgument.new(values, string)
|
189
|
+
end
|
190
|
+
|
191
|
+
# returns true when the given value responds to :call and the key is
|
192
|
+
# an interpolation placeholder in the given string
|
193
|
+
def interpolate_lambda?(object, string, key)
|
194
|
+
object.respond_to?(:call) && string =~ /%\{#{key}\}|%\<#{key}>.*?\d*\.?\d*[bBdiouxXeEfgGcps]\}/
|
195
|
+
end
|
196
|
+
|
197
|
+
# Loads a single translations file by delegating to #load_rb or
|
198
|
+
# #load_yml depending on the file extension and directly merges the
|
199
|
+
# data to the existing translations. Raises I18n::UnknownFileType
|
200
|
+
# for all other file extensions.
|
201
|
+
def load_file(filename)
|
202
|
+
type = File.extname(filename).tr('.', '').downcase
|
203
|
+
raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")
|
204
|
+
data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
|
205
|
+
data.each { |locale, d| merge_translations(locale, d) }
|
206
|
+
end
|
207
|
+
|
208
|
+
# Loads a plain Ruby translations file. eval'ing the file must yield
|
209
|
+
# a Hash containing translation data with locales as toplevel keys.
|
210
|
+
def load_rb(filename)
|
211
|
+
eval(IO.read(filename), binding, filename)
|
212
|
+
end
|
213
|
+
|
214
|
+
# Loads a YAML translations file. The data must have locales as
|
215
|
+
# toplevel keys.
|
216
|
+
def load_yml(filename)
|
217
|
+
YAML::load(IO.read(filename))
|
218
|
+
end
|
219
|
+
|
220
|
+
# Deep merges the given translations hash with the existing translations
|
221
|
+
# for the given locale
|
222
|
+
def merge_translations(locale, data)
|
223
|
+
locale = locale.to_sym
|
224
|
+
translations[locale] ||= {}
|
225
|
+
data = deep_symbolize_keys(data)
|
226
|
+
|
227
|
+
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
228
|
+
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
229
|
+
translations[locale].merge!(data, &merger)
|
230
|
+
end
|
231
|
+
|
232
|
+
# Return a new hash with all keys and nested keys converted to symbols.
|
233
|
+
def deep_symbolize_keys(hash)
|
234
|
+
hash.inject({}) { |result, (key, value)|
|
235
|
+
value = deep_symbolize_keys(value) if value.is_a?(Hash)
|
236
|
+
result[(key.to_sym rescue key) || key] = value
|
237
|
+
result
|
238
|
+
}
|
239
|
+
end
|
240
|
+
|
241
|
+
# Flatten the given array once
|
242
|
+
def flatten_once(array)
|
243
|
+
result = []
|
244
|
+
for element in array # a little faster than each
|
245
|
+
result.push(*element)
|
246
|
+
end
|
247
|
+
result
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|