lawrencepit-i18n 0.1.6 → 0.2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.textile +57 -0
- data/README.textile +27 -5
- data/Rakefile +21 -0
- data/VERSION +1 -0
- data/lib/i18n.rb +56 -12
- 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 +17 -229
- data/lib/i18n/exceptions.rb +12 -4
- 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
- metadata +27 -10
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/README.textile
CHANGED
@@ -1,16 +1,38 @@
|
|
1
|
-
h1. Ruby I18n
|
1
|
+
h1. Ruby I18n
|
2
2
|
|
3
|
-
|
3
|
+
Ruby Internationalization and localization solution.
|
4
4
|
|
5
|
-
|
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
|
6
28
|
|
7
29
|
h2. Authors
|
8
30
|
|
9
|
-
* "Matt Aimonetti":http://railsontherun.com
|
10
31
|
* "Sven Fuchs":http://www.artweb-design.de
|
11
32
|
* "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
|
12
|
-
* "Saimon Moore":http://saimonmoore.net
|
13
33
|
* "Stephan Soller":http://www.arkanis-development.de
|
34
|
+
* "Saimon Moore":http://saimonmoore.net
|
35
|
+
* "Matt Aimonetti":http://railsontherun.com
|
14
36
|
|
15
37
|
h2. License
|
16
38
|
|
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.0
|
data/lib/i18n.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
# Authors:: Matt Aimonetti (http://railsontherun.com/),
|
2
4
|
# Sven Fuchs (http://www.artweb-design.de),
|
3
5
|
# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
|
@@ -7,11 +9,12 @@
|
|
7
9
|
# License:: MIT
|
8
10
|
require 'i18n/backend/simple'
|
9
11
|
require 'i18n/exceptions'
|
12
|
+
require 'i18n/string'
|
10
13
|
|
11
14
|
module I18n
|
12
15
|
@@backend = nil
|
13
16
|
@@load_path = nil
|
14
|
-
@@default_locale = :
|
17
|
+
@@default_locale = :en
|
15
18
|
@@default_separator = '.'
|
16
19
|
@@exception_handler = :default_exception_handler
|
17
20
|
|
@@ -33,7 +36,7 @@ module I18n
|
|
33
36
|
|
34
37
|
# Sets the current default locale. Used to set a custom default locale.
|
35
38
|
def default_locale=(locale)
|
36
|
-
@@default_locale = locale
|
39
|
+
@@default_locale = locale.to_sym rescue nil
|
37
40
|
end
|
38
41
|
|
39
42
|
# Returns the current locale. Defaults to I18n.default_locale.
|
@@ -43,12 +46,19 @@ module I18n
|
|
43
46
|
|
44
47
|
# Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
|
45
48
|
def locale=(locale)
|
46
|
-
Thread.current[:locale] = locale
|
49
|
+
Thread.current[:locale] = locale.to_sym rescue nil
|
47
50
|
end
|
48
51
|
|
49
|
-
# Returns an array of locales for which translations are available
|
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.
|
50
55
|
def available_locales
|
51
|
-
backend.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
|
52
62
|
end
|
53
63
|
|
54
64
|
# Returns the current default scope separator. Defaults to '.'
|
@@ -175,27 +185,34 @@ module I18n
|
|
175
185
|
# *LAMBDAS*
|
176
186
|
#
|
177
187
|
# Both translations and defaults can be given as Ruby lambdas. Lambdas will be
|
178
|
-
# called and passed the key and options.
|
188
|
+
# called and passed the key and options.
|
179
189
|
#
|
180
190
|
# E.g. assuming the key <tt>:salutation</tt> resolves to:
|
181
191
|
# lambda { |key, options| options[:gender] == 'm' ? "Mr. {{options[:name]}}" : "Mrs. {{options[:name]}}" }
|
182
192
|
#
|
183
193
|
# Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
|
184
|
-
#
|
194
|
+
#
|
185
195
|
# It is recommended to use/implement lambdas in an "idempotent" way. E.g. when
|
186
196
|
# a cache layer is put in front of I18n.translate it will generate a cache key
|
187
197
|
# from the argument values passed to #translate. Therefor your lambdas should
|
188
198
|
# always return the same translations/values per unique combination of argument
|
189
199
|
# values.
|
190
|
-
def translate(
|
191
|
-
|
200
|
+
def translate(*args)
|
201
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
202
|
+
key = args.shift
|
203
|
+
locale = options.delete(:locale) || I18n.locale
|
192
204
|
backend.translate(locale, key, options)
|
193
|
-
rescue I18n::ArgumentError =>
|
194
|
-
raise
|
195
|
-
|
205
|
+
rescue I18n::ArgumentError => exception
|
206
|
+
raise exception if options[:raise]
|
207
|
+
handle_exception(exception, locale, key, options)
|
196
208
|
end
|
197
209
|
alias :t :translate
|
198
210
|
|
211
|
+
def translate!(key, options = {})
|
212
|
+
translate(key, options.merge( :raise => true ))
|
213
|
+
end
|
214
|
+
alias :t! :translate!
|
215
|
+
|
199
216
|
# Localizes certain objects, such as dates and numbers to local formatting.
|
200
217
|
def localize(object, options = {})
|
201
218
|
locale = options[:locale] || I18n.locale
|
@@ -205,6 +222,7 @@ module I18n
|
|
205
222
|
alias :l :localize
|
206
223
|
|
207
224
|
protected
|
225
|
+
|
208
226
|
# Handles exceptions raised in the backend. All exceptions except for
|
209
227
|
# MissingTranslationData exceptions are re-raised. When a MissingTranslationData
|
210
228
|
# was caught and the option :raise is not set the handler returns an error
|
@@ -214,6 +232,32 @@ module I18n
|
|
214
232
|
raise exception
|
215
233
|
end
|
216
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
|
+
|
217
261
|
# Merges the given locale, key and scope into a single array of keys.
|
218
262
|
# Splits keys that contain dots into multiple keys. Makes sure all
|
219
263
|
# keys are Symbols.
|
@@ -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
|