i18n 0.1.0 → 0.2.0
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 +57 -0
- data/MIT-LICENSE +19 -19
- data/README.textile +20 -18
- data/Rakefile +21 -0
- data/VERSION +1 -0
- data/lib/i18n.rb +229 -192
- data/lib/i18n/backend/simple.rb +233 -193
- data/lib/i18n/exceptions.rb +59 -53
- data/lib/i18n/string.rb +93 -0
- data/test/all.rb +3 -5
- data/test/api/basics.rb +13 -0
- data/test/api/interpolation.rb +63 -0
- data/test/api/lambda.rb +50 -0
- data/test/api/link.rb +45 -0
- data/test/api/localization/date.rb +63 -0
- data/test/api/localization/date_time.rb +61 -0
- data/test/api/localization/lambda.rb +24 -0
- data/test/api/localization/time.rb +61 -0
- data/test/api/pluralization.rb +40 -0
- data/test/api/translation.rb +49 -0
- data/test/backend/simple/all.rb +3 -0
- data/test/backend/simple/api_test.rb +85 -0
- data/test/backend/simple/lookup_test.rb +23 -0
- data/test/backend/simple/setup/base.rb +21 -0
- data/test/backend/simple/setup/localization.rb +129 -0
- data/test/backend/simple/translations_test.rb +88 -0
- data/test/fixtures/locales/en.rb +1 -0
- data/test/fixtures/locales/en.yml +3 -0
- data/test/i18n_exceptions_test.rb +94 -99
- data/test/i18n_load_path_test.rb +16 -0
- data/test/i18n_test.rb +140 -141
- data/test/string_test.rb +92 -0
- data/test/test_helper.rb +59 -0
- data/test/with_options.rb +32 -0
- metadata +71 -22
- data/test/locale/en-US.rb +0 -1
- data/test/locale/en-US.yml +0 -3
- data/test/simple_backend_test.rb +0 -473
data/lib/i18n/backend/simple.rb
CHANGED
@@ -1,193 +1,233 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
module I18n
|
4
|
-
module Backend
|
5
|
-
class Simple
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
def
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
entry
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
format =
|
52
|
-
|
53
|
-
format
|
54
|
-
|
55
|
-
format.gsub!(/%
|
56
|
-
format.gsub!(/%
|
57
|
-
format.gsub!(/%
|
58
|
-
format.gsub!(/%
|
59
|
-
|
60
|
-
object.strftime(format)
|
61
|
-
end
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
#
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module I18n
|
4
|
+
module Backend
|
5
|
+
class Simple
|
6
|
+
RESERVED_KEYS = [:scope, :default, :separator]
|
7
|
+
INTERPOLATION_SYNTAX_PATTERN = /(\\\\)?\{\{([^\}]+)\}\}/
|
8
|
+
|
9
|
+
# Accepts a list of paths to translation files. Loads translations from
|
10
|
+
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
|
11
|
+
# for details.
|
12
|
+
def load_translations(*filenames)
|
13
|
+
filenames.each { |filename| load_file(filename) }
|
14
|
+
end
|
15
|
+
|
16
|
+
# Stores translations for the given locale in memory.
|
17
|
+
# This uses a deep merge for the translations hash, so existing
|
18
|
+
# translations will be overwritten by new ones only at the deepest
|
19
|
+
# level of the hash.
|
20
|
+
def store_translations(locale, data)
|
21
|
+
merge_translations(locale, data)
|
22
|
+
end
|
23
|
+
|
24
|
+
def translate(locale, key, options = {})
|
25
|
+
raise InvalidLocale.new(locale) if locale.nil?
|
26
|
+
return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)
|
27
|
+
|
28
|
+
count, scope, default, separator = options.values_at(:count, *RESERVED_KEYS)
|
29
|
+
values = options.reject { |name, value| RESERVED_KEYS.include?(name) }
|
30
|
+
|
31
|
+
entry = lookup(locale, key, scope, separator)
|
32
|
+
entry = entry.nil? ? default(locale, key, default, options) : resolve(locale, key, entry, options)
|
33
|
+
|
34
|
+
raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?
|
35
|
+
entry = pluralize(locale, entry, count)
|
36
|
+
entry = interpolate(locale, entry, values)
|
37
|
+
entry
|
38
|
+
end
|
39
|
+
|
40
|
+
# Acts the same as +strftime+, but returns a localized version of the
|
41
|
+
# formatted date string. Takes a key from the date/time formats
|
42
|
+
# translations as a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
|
43
|
+
def localize(locale, object, format = :default, options={})
|
44
|
+
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
|
45
|
+
|
46
|
+
if Symbol === format
|
47
|
+
type = object.respond_to?(:sec) ? 'time' : 'date'
|
48
|
+
format = lookup(locale, :"#{type}.formats.#{format}")
|
49
|
+
end
|
50
|
+
|
51
|
+
format = resolve(locale, object, format, options.merge(:raise => true))
|
52
|
+
|
53
|
+
# TODO check which format strings are present, then bulk translate them, then replace them
|
54
|
+
format.gsub!(/%a/, translate(locale, :"date.abbr_day_names", :format => format)[object.wday]) if format.include?('%a')
|
55
|
+
format.gsub!(/%A/, translate(locale, :"date.day_names", :format => format)[object.wday]) if format.include?('%A')
|
56
|
+
format.gsub!(/%b/, translate(locale, :"date.abbr_month_names", :format => format)[object.mon]) if format.include?('%b')
|
57
|
+
format.gsub!(/%B/, translate(locale, :"date.month_names", :format => format)[object.mon]) if format.include?('%B')
|
58
|
+
format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}", :format => format)) if format.include?('%p') && object.respond_to?(:hour)
|
59
|
+
|
60
|
+
object.strftime(format)
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialized?
|
64
|
+
@initialized ||= false
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns an array of locales for which translations are available
|
68
|
+
def available_locales
|
69
|
+
init_translations unless initialized?
|
70
|
+
translations.keys
|
71
|
+
end
|
72
|
+
|
73
|
+
def reload!
|
74
|
+
@initialized = false
|
75
|
+
@translations = nil
|
76
|
+
end
|
77
|
+
|
78
|
+
protected
|
79
|
+
def init_translations
|
80
|
+
load_translations(*I18n.load_path.flatten)
|
81
|
+
@initialized = true
|
82
|
+
end
|
83
|
+
|
84
|
+
def translations
|
85
|
+
@translations ||= {}
|
86
|
+
end
|
87
|
+
|
88
|
+
# Looks up a translation from the translations hash. Returns nil if
|
89
|
+
# eiher key is nil, or locale, scope or key do not exist as a key in the
|
90
|
+
# nested translations hash. Splits keys or scopes containing dots
|
91
|
+
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
|
92
|
+
# <tt>%w(currency format)</tt>.
|
93
|
+
def lookup(locale, key, scope = [], separator = nil)
|
94
|
+
return unless key
|
95
|
+
init_translations unless initialized?
|
96
|
+
keys = I18n.send(:normalize_translation_keys, locale, key, scope, separator)
|
97
|
+
keys.inject(translations) do |result, k|
|
98
|
+
if (x = result[k.to_sym]).nil?
|
99
|
+
return nil
|
100
|
+
else
|
101
|
+
x
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Evaluates defaults.
|
107
|
+
# If given subject is an Array, it walks the array and returns the
|
108
|
+
# first translation that can be resolved. Otherwise it tries to resolve
|
109
|
+
# the translation directly.
|
110
|
+
def default(locale, object, subject, options = {})
|
111
|
+
options = options.dup.reject { |key, value| key == :default }
|
112
|
+
case subject
|
113
|
+
when Array
|
114
|
+
subject.each do |subject|
|
115
|
+
result = resolve(locale, object, subject, options) and return result
|
116
|
+
end and nil
|
117
|
+
else
|
118
|
+
resolve(locale, object, subject, options)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Resolves a translation.
|
123
|
+
# If the given subject is a Symbol, it will be translated with the
|
124
|
+
# given options. If it is a Proc then it will be evaluated. All other
|
125
|
+
# subjects will be returned directly.
|
126
|
+
def resolve(locale, object, subject, options = {})
|
127
|
+
case subject
|
128
|
+
when Symbol
|
129
|
+
translate(locale, subject, options)
|
130
|
+
when Proc
|
131
|
+
resolve(locale, object, subject.call(object, options), options = {})
|
132
|
+
else
|
133
|
+
subject
|
134
|
+
end
|
135
|
+
rescue MissingTranslationData
|
136
|
+
nil
|
137
|
+
end
|
138
|
+
|
139
|
+
# Picks a translation from an array according to English pluralization
|
140
|
+
# rules. It will pick the first translation if count is not equal to 1
|
141
|
+
# and the second translation if it is equal to 1. Other backends can
|
142
|
+
# implement more flexible or complex pluralization rules.
|
143
|
+
def pluralize(locale, entry, count)
|
144
|
+
return entry unless entry.is_a?(Hash) and count
|
145
|
+
|
146
|
+
key = :zero if count == 0 && entry.has_key?(:zero)
|
147
|
+
key ||= count == 1 ? :one : :other
|
148
|
+
raise InvalidPluralizationData.new(entry, count) unless entry.has_key?(key)
|
149
|
+
entry[key]
|
150
|
+
end
|
151
|
+
|
152
|
+
# Interpolates values into a given string.
|
153
|
+
#
|
154
|
+
# interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
|
155
|
+
# # => "file test.txt opened by {{user}}"
|
156
|
+
#
|
157
|
+
# Note that you have to double escape the <tt>\\</tt> when you want to escape
|
158
|
+
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
|
159
|
+
# interpolation).
|
160
|
+
def interpolate(locale, string, values = {})
|
161
|
+
return string unless string.is_a?(String) && !values.empty?
|
162
|
+
|
163
|
+
string.gsub(INTERPOLATION_SYNTAX_PATTERN) do
|
164
|
+
escaped, key = $1, $2.to_sym
|
165
|
+
|
166
|
+
if escaped
|
167
|
+
key
|
168
|
+
elsif RESERVED_KEYS.include?(key)
|
169
|
+
raise ReservedInterpolationKey.new(key, string)
|
170
|
+
else
|
171
|
+
"%{#{key}}"
|
172
|
+
end
|
173
|
+
end % values
|
174
|
+
|
175
|
+
rescue KeyError => e
|
176
|
+
raise MissingInterpolationArgument.new(values, string)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Loads a single translations file by delegating to #load_rb or
|
180
|
+
# #load_yml depending on the file extension and directly merges the
|
181
|
+
# data to the existing translations. Raises I18n::UnknownFileType
|
182
|
+
# for all other file extensions.
|
183
|
+
def load_file(filename)
|
184
|
+
type = File.extname(filename).tr('.', '').downcase
|
185
|
+
raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}")
|
186
|
+
data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
|
187
|
+
data.each { |locale, d| merge_translations(locale, d) }
|
188
|
+
end
|
189
|
+
|
190
|
+
# Loads a plain Ruby translations file. eval'ing the file must yield
|
191
|
+
# a Hash containing translation data with locales as toplevel keys.
|
192
|
+
def load_rb(filename)
|
193
|
+
eval(IO.read(filename), binding, filename)
|
194
|
+
end
|
195
|
+
|
196
|
+
# Loads a YAML translations file. The data must have locales as
|
197
|
+
# toplevel keys.
|
198
|
+
def load_yml(filename)
|
199
|
+
YAML::load(IO.read(filename))
|
200
|
+
end
|
201
|
+
|
202
|
+
# Deep merges the given translations hash with the existing translations
|
203
|
+
# for the given locale
|
204
|
+
def merge_translations(locale, data)
|
205
|
+
locale = locale.to_sym
|
206
|
+
translations[locale] ||= {}
|
207
|
+
data = deep_symbolize_keys(data)
|
208
|
+
|
209
|
+
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
210
|
+
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
211
|
+
translations[locale].merge!(data, &merger)
|
212
|
+
end
|
213
|
+
|
214
|
+
# Return a new hash with all keys and nested keys converted to symbols.
|
215
|
+
def deep_symbolize_keys(hash)
|
216
|
+
hash.inject({}) { |result, (key, value)|
|
217
|
+
value = deep_symbolize_keys(value) if value.is_a?(Hash)
|
218
|
+
result[(key.to_sym rescue key) || key] = value
|
219
|
+
result
|
220
|
+
}
|
221
|
+
end
|
222
|
+
|
223
|
+
# Flatten the given array once
|
224
|
+
def flatten_once(array)
|
225
|
+
result = []
|
226
|
+
for element in array # a little faster than each
|
227
|
+
result.push(*element)
|
228
|
+
end
|
229
|
+
result
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
data/lib/i18n/exceptions.rb
CHANGED
@@ -1,53 +1,59 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
1
|
+
class KeyError < IndexError
|
2
|
+
def initialize(message = nil)
|
3
|
+
super(message || "key not found")
|
4
|
+
end
|
5
|
+
end unless defined?(KeyError)
|
6
|
+
|
7
|
+
module I18n
|
8
|
+
class ArgumentError < ::ArgumentError; end
|
9
|
+
|
10
|
+
class InvalidLocale < ArgumentError
|
11
|
+
attr_reader :locale
|
12
|
+
def initialize(locale)
|
13
|
+
@locale = locale
|
14
|
+
super "#{locale.inspect} is not a valid locale"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MissingTranslationData < ArgumentError
|
19
|
+
attr_reader :locale, :key, :options
|
20
|
+
def initialize(locale, key, options)
|
21
|
+
@key, @locale, @options = key, locale, options
|
22
|
+
keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
|
23
|
+
keys << 'no key' if keys.size < 2
|
24
|
+
super "translation missing: #{keys.join(', ')}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class InvalidPluralizationData < ArgumentError
|
29
|
+
attr_reader :entry, :count
|
30
|
+
def initialize(entry, count)
|
31
|
+
@entry, @count = entry, count
|
32
|
+
super "translation data #{entry.inspect} can not be used with :count => #{count}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class MissingInterpolationArgument < ArgumentError
|
37
|
+
attr_reader :values, :string
|
38
|
+
def initialize(values, string)
|
39
|
+
@values, @string = values, string
|
40
|
+
super "missing interpolation argument in #{string.inspect} (#{values.inspect} given)"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class ReservedInterpolationKey < ArgumentError
|
45
|
+
attr_reader :key, :string
|
46
|
+
def initialize(key, string)
|
47
|
+
@key, @string = key, string
|
48
|
+
super "reserved key #{key.inspect} used in #{string.inspect}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class UnknownFileType < ArgumentError
|
53
|
+
attr_reader :type, :filename
|
54
|
+
def initialize(type, filename)
|
55
|
+
@type, @filename = type, filename
|
56
|
+
super "can not load translations from #{filename}, the file type #{type} is not known"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|