i18n 1.14.1 → 1.15.2
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.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/lib/i18n/backend/base.rb +15 -5
- data/lib/i18n/backend/fallbacks.rb +16 -4
- data/lib/i18n/backend/interpolation_compiler.rb +6 -7
- data/lib/i18n/backend/simple.rb +9 -9
- data/lib/i18n/backend/transliterator.rb +3 -2
- data/lib/i18n/config.rb +35 -1
- data/lib/i18n/exceptions.rb +0 -2
- data/lib/i18n/locale/fallbacks.rb +13 -3
- data/lib/i18n/middleware.rb +5 -1
- data/lib/i18n/tests/defaults.rb +7 -0
- data/lib/i18n/tests/interpolation.rb +25 -3
- data/lib/i18n/tests/localization/procs.rb +2 -2
- data/lib/i18n/tests/lookup.rb +6 -0
- data/lib/i18n/tests/procs.rb +12 -7
- data/lib/i18n/version.rb +1 -1
- data/lib/i18n.rb +80 -8
- metadata +4 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 600a63b2ec049818152fff57053db49142c9f51ea58da7d6c3200011974a5707
|
|
4
|
+
data.tar.gz: a7ef9def35a09233a6b80f1aee94052d18db1220f5dcf6df59053f5f30d54efa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5b8196a163065458e0d13d37ed09817cfef09848e24eb061537467c9cc06f75b50619f2cd179890b904d819fb5ace61c353dce51bd673ec116398329777e38ba
|
|
7
|
+
data.tar.gz: b3d96b130b6d089571bd2480a9f4a9675e423d39d5b82c55fea56f0e0e553a85a63a80c3f494f9a450d356cac718494e04ce1a3add5efea0ccfc3f8f01581b97
|
data/README.md
CHANGED
|
@@ -13,10 +13,14 @@ Currently maintained by @radar.
|
|
|
13
13
|
|
|
14
14
|
You will most commonly use this library within a Rails app.
|
|
15
15
|
|
|
16
|
+
We support Rails versions from 6.0 and up.
|
|
17
|
+
|
|
16
18
|
[See the Rails Guide](https://guides.rubyonrails.org/i18n.html) for an example of its usage.
|
|
17
19
|
|
|
18
20
|
### Ruby (without Rails)
|
|
19
21
|
|
|
22
|
+
We support Ruby versions from 3.1 and up.
|
|
23
|
+
|
|
20
24
|
If you want to use this library without Rails, you can simply add `i18n` to your `Gemfile`:
|
|
21
25
|
|
|
22
26
|
```ruby
|
|
@@ -50,6 +54,18 @@ I18n.locale = :de
|
|
|
50
54
|
I18n.t(:test) # => "Dies ist ein Test"
|
|
51
55
|
```
|
|
52
56
|
|
|
57
|
+
### Fiber-safe config updates
|
|
58
|
+
|
|
59
|
+
`Fiber[]` values are inherited by child fibers.
|
|
60
|
+
To avoid sharing mutable config objects across fibers, prefer creating updated configs and setting them with `set!`:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
I18n.config.with(locale: :ja).set!
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`set!` stores a frozen config in the current execution context.
|
|
67
|
+
This keeps inherited state safe and lets updates happen by replacing config objects.
|
|
68
|
+
|
|
53
69
|
## Features
|
|
54
70
|
|
|
55
71
|
* Translation and localization
|
data/lib/i18n/backend/base.rb
CHANGED
|
@@ -54,13 +54,16 @@ module I18n
|
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
deep_interpolation = options[:deep_interpolation]
|
|
57
|
+
skip_interpolation = options[:skip_interpolation]
|
|
57
58
|
values = Utils.except(options, *RESERVED_KEYS) unless options.empty?
|
|
58
|
-
if values
|
|
59
|
+
if !skip_interpolation && values && !values.empty?
|
|
59
60
|
entry = if deep_interpolation
|
|
60
61
|
deep_interpolate(locale, entry, values)
|
|
61
62
|
else
|
|
62
63
|
interpolate(locale, entry, values)
|
|
63
64
|
end
|
|
65
|
+
elsif entry.is_a?(String) && entry =~ I18n.reserved_keys_pattern
|
|
66
|
+
raise ReservedInterpolationKey.new($1.to_sym, entry)
|
|
64
67
|
end
|
|
65
68
|
entry
|
|
66
69
|
end
|
|
@@ -149,7 +152,14 @@ module I18n
|
|
|
149
152
|
result = catch(:exception) do
|
|
150
153
|
case subject
|
|
151
154
|
when Symbol
|
|
152
|
-
I18n.translate(
|
|
155
|
+
I18n.translate(
|
|
156
|
+
subject,
|
|
157
|
+
**options.merge(
|
|
158
|
+
:locale => locale,
|
|
159
|
+
:throw => true,
|
|
160
|
+
:skip_interpolation => true
|
|
161
|
+
)
|
|
162
|
+
)
|
|
153
163
|
when Proc
|
|
154
164
|
date_or_time = options.delete(:object) || object
|
|
155
165
|
resolve(locale, object, subject.call(date_or_time, **options))
|
|
@@ -186,8 +196,8 @@ module I18n
|
|
|
186
196
|
#
|
|
187
197
|
# if the given subject is an array then:
|
|
188
198
|
# each element of the array is recursively interpolated (until it finds a string)
|
|
189
|
-
# method interpolates ["yes, %{user}", ["maybe no, %{user}, "no, %{user}"]], :user => "bartuz"
|
|
190
|
-
# # =>
|
|
199
|
+
# method interpolates ["yes, %{user}", ["maybe no, %{user}", "no, %{user}"]], :user => "bartuz"
|
|
200
|
+
# # => ["yes, bartuz", ["maybe no, bartuz", "no, bartuz"]]
|
|
191
201
|
def interpolate(locale, subject, values = EMPTY_HASH)
|
|
192
202
|
return subject if values.empty?
|
|
193
203
|
|
|
@@ -242,7 +252,7 @@ module I18n
|
|
|
242
252
|
# Loads a plain Ruby translations file. eval'ing the file must yield
|
|
243
253
|
# a Hash containing translation data with locales as toplevel keys.
|
|
244
254
|
def load_rb(filename)
|
|
245
|
-
translations = eval(IO.read(filename), binding, filename)
|
|
255
|
+
translations = eval(IO.read(filename), binding, filename.to_s)
|
|
246
256
|
[translations, false]
|
|
247
257
|
end
|
|
248
258
|
|
|
@@ -16,13 +16,21 @@ module I18n
|
|
|
16
16
|
# Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+.
|
|
17
17
|
def fallbacks
|
|
18
18
|
@@fallbacks ||= I18n::Locale::Fallbacks.new
|
|
19
|
-
|
|
19
|
+
if Fiber.respond_to?(:[])
|
|
20
|
+
Fiber[:i18n_fallbacks] || @@fallbacks
|
|
21
|
+
else
|
|
22
|
+
Thread.current[:i18n_fallbacks] || @@fallbacks
|
|
23
|
+
end
|
|
20
24
|
end
|
|
21
25
|
|
|
22
26
|
# Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.
|
|
23
27
|
def fallbacks=(fallbacks)
|
|
24
28
|
@@fallbacks = fallbacks.is_a?(Array) ? I18n::Locale::Fallbacks.new(fallbacks) : fallbacks
|
|
25
|
-
|
|
29
|
+
if Fiber.respond_to?(:[])
|
|
30
|
+
Fiber[:i18n_fallbacks] = @@fallbacks
|
|
31
|
+
else
|
|
32
|
+
Thread.current[:i18n_fallbacks] = @@fallbacks
|
|
33
|
+
end
|
|
26
34
|
end
|
|
27
35
|
end
|
|
28
36
|
|
|
@@ -71,7 +79,11 @@ module I18n
|
|
|
71
79
|
|
|
72
80
|
case subject
|
|
73
81
|
when Symbol
|
|
74
|
-
I18n.translate(subject, **options.merge(
|
|
82
|
+
I18n.translate(subject, **options.merge(
|
|
83
|
+
:locale => options[:fallback_original_locale],
|
|
84
|
+
:throw => true,
|
|
85
|
+
:skip_interpolation => true
|
|
86
|
+
))
|
|
75
87
|
when Proc
|
|
76
88
|
date_or_time = options.delete(:object) || object
|
|
77
89
|
resolve_entry(options[:fallback_original_locale], object, subject.call(date_or_time, **options))
|
|
@@ -95,7 +107,7 @@ module I18n
|
|
|
95
107
|
return super unless options.fetch(:fallback, true)
|
|
96
108
|
I18n.fallbacks[locale].each do |fallback|
|
|
97
109
|
begin
|
|
98
|
-
return true if super(fallback, key)
|
|
110
|
+
return true if super(fallback, key, options)
|
|
99
111
|
rescue I18n::InvalidLocale
|
|
100
112
|
# we do nothing when the locale is invalid, as this is a fallback anyways.
|
|
101
113
|
end
|
|
@@ -21,11 +21,11 @@ module I18n
|
|
|
21
21
|
module Compiler
|
|
22
22
|
extend self
|
|
23
23
|
|
|
24
|
-
TOKENIZER
|
|
25
|
-
INTERPOLATION_SYNTAX_PATTERN = /(%)?(%\{([^\}]+)\})/
|
|
24
|
+
TOKENIZER = /(%%?\{[^}]+\})/
|
|
26
25
|
|
|
27
26
|
def compile_if_an_interpolation(string)
|
|
28
27
|
if interpolated_str?(string)
|
|
28
|
+
string = +string
|
|
29
29
|
string.instance_eval <<-RUBY_EVAL, __FILE__, __LINE__
|
|
30
30
|
def i18n_interpolate(v = {})
|
|
31
31
|
"#{compiled_interpolation_body(string)}"
|
|
@@ -37,7 +37,7 @@ module I18n
|
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def interpolated_str?(str)
|
|
40
|
-
str.kind_of?(::String) && str =~
|
|
40
|
+
str.kind_of?(::String) && str =~ TOKENIZER
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
protected
|
|
@@ -48,13 +48,12 @@ module I18n
|
|
|
48
48
|
|
|
49
49
|
def compiled_interpolation_body(str)
|
|
50
50
|
tokenize(str).map do |token|
|
|
51
|
-
|
|
51
|
+
token.match(TOKENIZER) ? handle_interpolation_token(token) : escape_plain_str(token)
|
|
52
52
|
end.join
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
def handle_interpolation_token(
|
|
56
|
-
|
|
57
|
-
escaped ? pattern : compile_interpolation_token(key.to_sym)
|
|
55
|
+
def handle_interpolation_token(token)
|
|
56
|
+
token.start_with?('%%') ? token[1..] : compile_interpolation_token(token[2..-2])
|
|
58
57
|
end
|
|
59
58
|
|
|
60
59
|
def compile_interpolation_token(key)
|
data/lib/i18n/backend/simple.rb
CHANGED
|
@@ -10,14 +10,14 @@ module I18n
|
|
|
10
10
|
# The implementation is provided by a Implementation module allowing to easily
|
|
11
11
|
# extend Simple backend's behavior by including modules. E.g.:
|
|
12
12
|
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
13
|
+
# module I18n::Backend::Pluralization
|
|
14
|
+
# def pluralize(*args)
|
|
15
|
+
# # extended pluralization logic
|
|
16
|
+
# super
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# I18n::Backend::Simple.include(I18n::Backend::Pluralization)
|
|
21
21
|
class Simple
|
|
22
22
|
module Implementation
|
|
23
23
|
include Base
|
|
@@ -42,7 +42,7 @@ module I18n
|
|
|
42
42
|
locale = locale.to_sym
|
|
43
43
|
translations[locale] ||= Concurrent::Hash.new
|
|
44
44
|
data = Utils.deep_symbolize_keys(data) unless options.fetch(:skip_symbolize_keys, false)
|
|
45
|
-
Utils.deep_merge!(translations[locale], data)
|
|
45
|
+
MUTEX.synchronize { Utils.deep_merge!(translations[locale], data) }
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
# Get available locales from the translations hash
|
|
@@ -68,7 +68,8 @@ module I18n
|
|
|
68
68
|
"ŧ"=>"t", "Ũ"=>"U", "ũ"=>"u", "Ū"=>"U", "ū"=>"u", "Ŭ"=>"U", "ŭ"=>"u",
|
|
69
69
|
"Ů"=>"U", "ů"=>"u", "Ű"=>"U", "ű"=>"u", "Ų"=>"U", "ų"=>"u", "Ŵ"=>"W",
|
|
70
70
|
"ŵ"=>"w", "Ŷ"=>"Y", "ŷ"=>"y", "Ÿ"=>"Y", "Ź"=>"Z", "ź"=>"z", "Ż"=>"Z",
|
|
71
|
-
"ż"=>"z", "Ž"=>"Z", "ž"=>"z"
|
|
71
|
+
"ż"=>"z", "Ž"=>"Z", "ž"=>"z", "Ǫ"=>"O", "ǫ"=>"o", "Ǭ"=>"O",
|
|
72
|
+
"ǭ"=>"o"
|
|
72
73
|
}.freeze
|
|
73
74
|
|
|
74
75
|
def initialize(rule = nil)
|
|
@@ -80,7 +81,7 @@ module I18n
|
|
|
80
81
|
def transliterate(string, replacement = nil)
|
|
81
82
|
replacement ||= DEFAULT_REPLACEMENT_CHAR
|
|
82
83
|
string.gsub(/[^\x00-\x7f]/u) do |char|
|
|
83
|
-
approximations[char] || replacement
|
|
84
|
+
approximations[char] || (replacement == :none ? char : replacement)
|
|
84
85
|
end
|
|
85
86
|
end
|
|
86
87
|
|
data/lib/i18n/config.rb
CHANGED
|
@@ -10,7 +10,41 @@ module I18n
|
|
|
10
10
|
defined?(@locale) && @locale != nil ? @locale : default_locale
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
def initialize
|
|
14
|
+
@owner = Fiber.current
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def owned_by?(fiber)
|
|
18
|
+
@owner == fiber
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def owner=(fiber)
|
|
22
|
+
@owner = fiber
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize_copy(other)
|
|
26
|
+
@owner = Fiber.current
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Returns a copied configuration with the provided attributes set.
|
|
30
|
+
def with(**attrs)
|
|
31
|
+
dup.tap do |copy|
|
|
32
|
+
attrs.each do |name, value|
|
|
33
|
+
copy.public_send("#{name}=", value)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Sets this configuration as the current one for the active execution context.
|
|
39
|
+
# The stored configuration is frozen to avoid sharing mutable state between fibers.
|
|
40
|
+
def set!
|
|
41
|
+
self.owner = Fiber.current unless frozen?
|
|
42
|
+
freeze
|
|
43
|
+
I18n.config = self
|
|
44
|
+
self
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Sets the current locale pseudo-globally, i.e. in the Thread.current or Fiber local hash.
|
|
14
48
|
def locale=(locale)
|
|
15
49
|
I18n.enforce_available_locales!(locale)
|
|
16
50
|
@locale = locale && locale.to_sym
|
data/lib/i18n/exceptions.rb
CHANGED
|
@@ -79,14 +79,24 @@ module I18n
|
|
|
79
79
|
end
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
+
def empty?
|
|
83
|
+
@map.empty? && @defaults.empty?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def inspect
|
|
87
|
+
"#<#{self.class.name} @map=#{@map.inspect} @defaults=#{@defaults.inspect}>"
|
|
88
|
+
end
|
|
89
|
+
|
|
82
90
|
protected
|
|
83
91
|
|
|
84
92
|
def compute(tags, include_defaults = true, exclude = [])
|
|
85
|
-
result =
|
|
93
|
+
result = []
|
|
94
|
+
Array(tags).each do |tag|
|
|
86
95
|
tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - exclude
|
|
87
|
-
|
|
88
|
-
tags
|
|
96
|
+
result += tags
|
|
97
|
+
tags.each { |_tag| result += compute(@map[_tag], false, exclude + result) if @map[_tag] }
|
|
89
98
|
end
|
|
99
|
+
|
|
90
100
|
result.push(*defaults) if include_defaults
|
|
91
101
|
result.uniq!
|
|
92
102
|
result.compact!
|
data/lib/i18n/middleware.rb
CHANGED
|
@@ -10,7 +10,11 @@ module I18n
|
|
|
10
10
|
def call(env)
|
|
11
11
|
@app.call(env)
|
|
12
12
|
ensure
|
|
13
|
-
|
|
13
|
+
if Fiber.respond_to?(:[])
|
|
14
|
+
Fiber[:i18n_config] = I18n::Config.new
|
|
15
|
+
else
|
|
16
|
+
Thread.current.thread_variable_set(:i18n_config, I18n::Config.new)
|
|
17
|
+
end
|
|
14
18
|
end
|
|
15
19
|
|
|
16
20
|
end
|
data/lib/i18n/tests/defaults.rb
CHANGED
|
@@ -47,6 +47,13 @@ module I18n
|
|
|
47
47
|
I18n.backend.store_translations(:en, { :foo => { :bar => 'bar' } }, { :separator => '|' })
|
|
48
48
|
assert_equal 'bar', I18n.t(nil, :default => :'foo|bar', :separator => '|')
|
|
49
49
|
end
|
|
50
|
+
|
|
51
|
+
# Addresses issue: #599
|
|
52
|
+
test "defaults: only interpolates once when resolving defaults" do
|
|
53
|
+
I18n.backend.store_translations(:en, :greeting => 'hey %{name}')
|
|
54
|
+
assert_equal 'hey %{dont_interpolate_me}',
|
|
55
|
+
I18n.t(:does_not_exist, :name => '%{dont_interpolate_me}', default: [:greeting])
|
|
56
|
+
end
|
|
50
57
|
end
|
|
51
58
|
end
|
|
52
59
|
end
|
|
@@ -89,14 +89,14 @@ module I18n
|
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
test "interpolation: ASCII strings in the backend should be encoded to UTF8 if interpolation options are in UTF8" do
|
|
92
|
-
I18n.backend.store_translations 'en', 'encoding' => ('%{who} let me go'.force_encoding(
|
|
92
|
+
I18n.backend.store_translations 'en', 'encoding' => ('%{who} let me go'.dup.force_encoding(Encoding::US_ASCII))
|
|
93
93
|
result = I18n.t 'encoding', :who => "måmmå miå"
|
|
94
94
|
assert_equal Encoding::UTF_8, result.encoding
|
|
95
95
|
end
|
|
96
96
|
|
|
97
97
|
test "interpolation: UTF8 strings in the backend are still returned as UTF8 with ASCII interpolation" do
|
|
98
98
|
I18n.backend.store_translations 'en', 'encoding' => 'måmmå miå %{what}'
|
|
99
|
-
result = I18n.t 'encoding', :what => 'let me go'.force_encoding(
|
|
99
|
+
result = I18n.t 'encoding', :what => 'let me go'.dup.force_encoding(Encoding::US_ASCII)
|
|
100
100
|
assert_equal Encoding::UTF_8, result.encoding
|
|
101
101
|
end
|
|
102
102
|
|
|
@@ -112,6 +112,28 @@ module I18n
|
|
|
112
112
|
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{default}') }
|
|
113
113
|
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{separator}') }
|
|
114
114
|
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:foo => :bar, :default => '%{scope}') }
|
|
115
|
+
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:default => '%{scope}') }
|
|
116
|
+
|
|
117
|
+
I18n.backend.store_translations(:en, :interpolate => 'Hi %{scope}!')
|
|
118
|
+
assert_raises(I18n::ReservedInterpolationKey) { interpolate(:interpolate) }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
test "interpolation: it does not raise I18n::ReservedInterpolationKey for escaped variables" do
|
|
122
|
+
assert_nothing_raised do
|
|
123
|
+
assert_equal '%{separator}', interpolate(:foo => :bar, :default => '%%{separator}')
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Note: The two interpolations below do not remove the escape character (%) because
|
|
127
|
+
# I18n should not alter the strings when no interpolation parameters are given,
|
|
128
|
+
# see the comment at the top of this file.
|
|
129
|
+
assert_nothing_raised do
|
|
130
|
+
assert_equal '%%{scope}', interpolate(:default => '%%{scope}')
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
I18n.backend.store_translations(:en, :interpolate => 'Hi %%{scope}!')
|
|
134
|
+
assert_nothing_raised do
|
|
135
|
+
assert_equal 'Hi %%{scope}!', interpolate(:interpolate)
|
|
136
|
+
end
|
|
115
137
|
end
|
|
116
138
|
|
|
117
139
|
test "interpolation: deep interpolation for default string" do
|
|
@@ -150,7 +172,7 @@ module I18n
|
|
|
150
172
|
end
|
|
151
173
|
|
|
152
174
|
def euc_jp(string)
|
|
153
|
-
string.encode
|
|
175
|
+
string.encode(Encoding::EUC_JP)
|
|
154
176
|
end
|
|
155
177
|
|
|
156
178
|
def interpolate(*args)
|
|
@@ -34,7 +34,7 @@ module I18n
|
|
|
34
34
|
test "localize Date: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
|
|
35
35
|
setup_time_proc_translations
|
|
36
36
|
date = ::Date.new(2008, 3, 1)
|
|
37
|
-
assert_equal
|
|
37
|
+
assert_equal %|[Sat, 01 Mar 2008, #{{:foo=>"foo"}}]|, I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
test "localize DateTime: given a format that resolves to a Proc it calls the Proc with the object" do
|
|
@@ -46,7 +46,7 @@ module I18n
|
|
|
46
46
|
test "localize DateTime: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
|
|
47
47
|
setup_time_proc_translations
|
|
48
48
|
datetime = ::DateTime.new(2008, 3, 1, 6)
|
|
49
|
-
assert_equal
|
|
49
|
+
assert_equal %|[Sat, 01 Mar 2008 06:00:00 +00:00, #{{:foo=>"foo"}}]|, I18n.l(datetime, :format => :proc, :foo => 'foo', :locale => :ru)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
test "localize Time: given a format that resolves to a Proc it calls the Proc with the object" do
|
data/lib/i18n/tests/lookup.rb
CHANGED
|
@@ -76,6 +76,12 @@ module I18n
|
|
|
76
76
|
test "lookup: a resulting Hash is not frozen" do
|
|
77
77
|
assert !I18n.t(:hash).frozen?
|
|
78
78
|
end
|
|
79
|
+
|
|
80
|
+
# Addresses issue: #599
|
|
81
|
+
test "lookup: only interpolates once when resolving symbols" do
|
|
82
|
+
I18n.backend.store_translations(:en, foo: :bar, bar: '%{value}')
|
|
83
|
+
assert_equal '%{dont_interpolate_me}', I18n.t(:foo, value: '%{dont_interpolate_me}')
|
|
84
|
+
end
|
|
79
85
|
end
|
|
80
86
|
end
|
|
81
87
|
end
|
data/lib/i18n/tests/procs.rb
CHANGED
|
@@ -5,34 +5,38 @@ module I18n
|
|
|
5
5
|
module Procs
|
|
6
6
|
test "lookup: given a translation is a proc it calls the proc with the key and interpolation values" do
|
|
7
7
|
I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| I18n::Tests::Procs.filter_args(*args) })
|
|
8
|
-
assert_equal
|
|
8
|
+
assert_equal %|[:a_lambda, #{{:foo=>"foo"}}]|, I18n.t(:a_lambda, :foo => 'foo')
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
test "lookup: given a translation is a proc it passes the interpolation values as keyword arguments" do
|
|
12
12
|
I18n.backend.store_translations(:en, :a_lambda => lambda { |key, foo:, **| I18n::Tests::Procs.filter_args(key, foo: foo) })
|
|
13
|
-
assert_equal
|
|
13
|
+
assert_equal %|[:a_lambda, #{{:foo=>"foo"}}]|, I18n.t(:a_lambda, :foo => 'foo')
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
test "defaults: given a default is a Proc it calls it with the key and interpolation values" do
|
|
17
17
|
proc = lambda { |*args| I18n::Tests::Procs.filter_args(*args) }
|
|
18
|
-
assert_equal
|
|
18
|
+
assert_equal %|[nil, #{{:foo=>"foo"}}]|, I18n.t(nil, :default => proc, :foo => 'foo')
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
test "defaults: given a default is a key that resolves to a Proc it calls it with the key and interpolation values" do
|
|
22
22
|
the_lambda = lambda { |*args| I18n::Tests::Procs.filter_args(*args) }
|
|
23
23
|
I18n.backend.store_translations(:en, :a_lambda => the_lambda)
|
|
24
|
-
assert_equal
|
|
25
|
-
assert_equal
|
|
24
|
+
assert_equal %|[:a_lambda, #{{:foo=>"foo"}}]|, I18n.t(nil, :default => :a_lambda, :foo => 'foo')
|
|
25
|
+
assert_equal %|[:a_lambda, #{{:foo=>"foo"}}]|, I18n.t(nil, :default => [nil, :a_lambda], :foo => 'foo')
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
test "interpolation: given an interpolation value is a lambda it calls it with key and values before interpolating it" do
|
|
29
29
|
proc = lambda { |*args| I18n::Tests::Procs.filter_args(*args) }
|
|
30
|
-
|
|
30
|
+
if RUBY_VERSION < "3.4"
|
|
31
|
+
assert_match %r(\[\{:foo=>#<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
|
|
32
|
+
else
|
|
33
|
+
assert_match %r(\[\{foo: #<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
|
|
34
|
+
end
|
|
31
35
|
end
|
|
32
36
|
|
|
33
37
|
test "interpolation: given a key resolves to a Proc that returns a string then interpolation still works" do
|
|
34
38
|
proc = lambda { |*args| "%{foo}: " + I18n::Tests::Procs.filter_args(*args) }
|
|
35
|
-
assert_equal
|
|
39
|
+
assert_equal %|foo: [nil, #{{:foo=>"foo"}}]|, I18n.t(nil, :default => proc, :foo => 'foo')
|
|
36
40
|
end
|
|
37
41
|
|
|
38
42
|
test "pluralization: given a key resolves to a Proc that returns valid data then pluralization still works" do
|
|
@@ -57,6 +61,7 @@ module I18n
|
|
|
57
61
|
if arg.is_a?(Hash)
|
|
58
62
|
arg.delete(:fallback_in_progress)
|
|
59
63
|
arg.delete(:fallback_original_locale)
|
|
64
|
+
arg.delete(:skip_interpolation)
|
|
60
65
|
end
|
|
61
66
|
arg
|
|
62
67
|
end.inspect
|
data/lib/i18n/version.rb
CHANGED
data/lib/i18n.rb
CHANGED
|
@@ -19,6 +19,7 @@ module I18n
|
|
|
19
19
|
RESERVED_KEYS = %i[
|
|
20
20
|
cascade
|
|
21
21
|
deep_interpolation
|
|
22
|
+
skip_interpolation
|
|
22
23
|
default
|
|
23
24
|
exception_handler
|
|
24
25
|
fallback
|
|
@@ -48,18 +49,48 @@ module I18n
|
|
|
48
49
|
end
|
|
49
50
|
|
|
50
51
|
def self.reserved_keys_pattern # :nodoc:
|
|
51
|
-
@reserved_keys_pattern ||=
|
|
52
|
+
@reserved_keys_pattern ||= /(?<!%)%\{(#{RESERVED_KEYS.join("|")})\}/
|
|
52
53
|
end
|
|
53
54
|
|
|
54
55
|
module Base
|
|
55
56
|
# Gets I18n configuration object.
|
|
56
57
|
def config
|
|
57
|
-
|
|
58
|
+
if Fiber.respond_to?(:[])
|
|
59
|
+
current = Fiber[:i18n_config] || self.config = I18n::Config.new
|
|
60
|
+
if current.respond_to?(:owned_by?) && !current.owned_by?(Fiber.current)
|
|
61
|
+
current = current.dup
|
|
62
|
+
Fiber[:i18n_config] = current
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
current
|
|
66
|
+
else
|
|
67
|
+
Thread.current.thread_variable_get(:i18n_config) ||
|
|
68
|
+
Thread.current.thread_variable_set(:i18n_config, I18n::Config.new)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Gets a mutable I18n configuration object.
|
|
73
|
+
def writable_config
|
|
74
|
+
current = config
|
|
75
|
+
return current unless current.frozen?
|
|
76
|
+
|
|
77
|
+
current = current.dup
|
|
78
|
+
if Fiber.respond_to?(:[])
|
|
79
|
+
Fiber[:i18n_config] = current
|
|
80
|
+
else
|
|
81
|
+
Thread.current.thread_variable_set(:i18n_config, current)
|
|
82
|
+
end
|
|
83
|
+
current
|
|
58
84
|
end
|
|
59
85
|
|
|
60
86
|
# Sets I18n configuration object.
|
|
61
87
|
def config=(value)
|
|
62
|
-
|
|
88
|
+
if Fiber.respond_to?(:[])
|
|
89
|
+
Fiber[:i18n_config] = value
|
|
90
|
+
value.owner = Fiber.current if value.respond_to?(:owner=) && !value.frozen?
|
|
91
|
+
else
|
|
92
|
+
Thread.current.thread_variable_set(:i18n_config, value)
|
|
93
|
+
end
|
|
63
94
|
end
|
|
64
95
|
|
|
65
96
|
# Write methods which delegates to the configuration object
|
|
@@ -71,7 +102,7 @@ module I18n
|
|
|
71
102
|
end
|
|
72
103
|
|
|
73
104
|
def #{method}=(value)
|
|
74
|
-
|
|
105
|
+
writable_config.#{method} = (value)
|
|
75
106
|
end
|
|
76
107
|
DELEGATORS
|
|
77
108
|
end
|
|
@@ -161,7 +192,7 @@ module I18n
|
|
|
161
192
|
# or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
|
|
162
193
|
# I18n.t :foo, :default => [:bar, 'default']
|
|
163
194
|
#
|
|
164
|
-
#
|
|
195
|
+
# <b>BULK LOOKUP</b>
|
|
165
196
|
#
|
|
166
197
|
# This returns an array with the translations for <tt>:foo</tt> and <tt>:bar</tt>.
|
|
167
198
|
# I18n.t [:foo, :bar]
|
|
@@ -180,7 +211,7 @@ module I18n
|
|
|
180
211
|
# E.g. assuming the key <tt>:salutation</tt> resolves to:
|
|
181
212
|
# lambda { |key, options| options[:gender] == 'm' ? "Mr. #{options[:name]}" : "Mrs. #{options[:name]}" }
|
|
182
213
|
#
|
|
183
|
-
# Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith') will result in "Mrs. Smith".
|
|
214
|
+
# Then <tt>I18n.t(:salutation, :gender => 'w', :name => 'Smith')</tt> will result in "Mrs. Smith".
|
|
184
215
|
#
|
|
185
216
|
# Note that the string returned by lambda will go through string interpolation too,
|
|
186
217
|
# so the following lambda would give the same result:
|
|
@@ -192,7 +223,7 @@ module I18n
|
|
|
192
223
|
# always return the same translations/values per unique combination of argument
|
|
193
224
|
# values.
|
|
194
225
|
#
|
|
195
|
-
#
|
|
226
|
+
# <b>Ruby 2.7+ keyword arguments warning</b>
|
|
196
227
|
#
|
|
197
228
|
# This method uses keyword arguments.
|
|
198
229
|
# There is a breaking change in ruby that produces warning with ruby 2.7 and won't work as expected with ruby 3.0
|
|
@@ -231,11 +262,41 @@ module I18n
|
|
|
231
262
|
end
|
|
232
263
|
alias :t! :translate!
|
|
233
264
|
|
|
265
|
+
# Returns an array of interpolation keys for the given translation key
|
|
266
|
+
#
|
|
267
|
+
# *Examples*
|
|
268
|
+
#
|
|
269
|
+
# Suppose we have the following:
|
|
270
|
+
# I18n.t 'example.zero' == 'Zero interpolations'
|
|
271
|
+
# I18n.t 'example.one' == 'One interpolation %{foo}'
|
|
272
|
+
# I18n.t 'example.two' == 'Two interpolations %{foo} %{bar}'
|
|
273
|
+
# I18n.t 'example.three' == ['One %{foo}', 'Two %{bar}', 'Three %{baz}']
|
|
274
|
+
# I18n.t 'example.one', locale: :other == 'One interpolation %{baz}'
|
|
275
|
+
#
|
|
276
|
+
# Then we can expect the following results:
|
|
277
|
+
# I18n.interpolation_keys('example.zero') #=> []
|
|
278
|
+
# I18n.interpolation_keys('example.one') #=> ['foo']
|
|
279
|
+
# I18n.interpolation_keys('example.two') #=> ['foo', 'bar']
|
|
280
|
+
# I18n.interpolation_keys('example.three') #=> ['foo', 'bar', 'baz']
|
|
281
|
+
# I18n.interpolation_keys('one', scope: 'example', locale: :other) #=> ['baz']
|
|
282
|
+
# I18n.interpolation_keys('does-not-exist') #=> []
|
|
283
|
+
# I18n.interpolation_keys('example') #=> []
|
|
284
|
+
def interpolation_keys(key, **options)
|
|
285
|
+
raise I18n::ArgumentError if !key.is_a?(String) || key.empty?
|
|
286
|
+
|
|
287
|
+
return [] unless exists?(key, **options.slice(:locale, :scope))
|
|
288
|
+
|
|
289
|
+
translation = translate(key, **options.slice(:locale, :scope))
|
|
290
|
+
interpolation_keys_from_translation(translation)
|
|
291
|
+
.flatten.compact
|
|
292
|
+
end
|
|
293
|
+
|
|
234
294
|
# Returns true if a translation exists for a given key, otherwise returns false.
|
|
235
295
|
def exists?(key, _locale = nil, locale: _locale, **options)
|
|
236
296
|
locale ||= config.locale
|
|
237
297
|
raise Disabled.new('exists?') if locale == false
|
|
238
|
-
raise I18n::ArgumentError if key.is_a?(String) && key.empty?
|
|
298
|
+
raise I18n::ArgumentError if (key.is_a?(String) && key.empty?) || key.nil?
|
|
299
|
+
|
|
239
300
|
config.backend.exists?(locale, key, options)
|
|
240
301
|
end
|
|
241
302
|
|
|
@@ -429,6 +490,17 @@ module I18n
|
|
|
429
490
|
keys
|
|
430
491
|
end
|
|
431
492
|
end
|
|
493
|
+
|
|
494
|
+
def interpolation_keys_from_translation(translation)
|
|
495
|
+
case translation
|
|
496
|
+
when ::String
|
|
497
|
+
translation.scan(Regexp.union(I18n.config.interpolation_patterns))
|
|
498
|
+
when ::Array
|
|
499
|
+
translation.map { |element| interpolation_keys_from_translation(element) }
|
|
500
|
+
else
|
|
501
|
+
[]
|
|
502
|
+
end
|
|
503
|
+
end
|
|
432
504
|
end
|
|
433
505
|
|
|
434
506
|
extend Base
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: i18n
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.15.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sven Fuchs
|
|
@@ -10,10 +10,9 @@ authors:
|
|
|
10
10
|
- Stephan Soller
|
|
11
11
|
- Saimon Moore
|
|
12
12
|
- Ryan Bigg
|
|
13
|
-
autorequire:
|
|
14
13
|
bindir: bin
|
|
15
14
|
cert_chain: []
|
|
16
|
-
date:
|
|
15
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
17
16
|
dependencies:
|
|
18
17
|
- !ruby/object:Gem::Dependency
|
|
19
18
|
name: concurrent-ruby
|
|
@@ -91,7 +90,6 @@ metadata:
|
|
|
91
90
|
changelog_uri: https://github.com/ruby-i18n/i18n/releases
|
|
92
91
|
documentation_uri: https://guides.rubyonrails.org/i18n.html
|
|
93
92
|
source_code_uri: https://github.com/ruby-i18n/i18n
|
|
94
|
-
post_install_message:
|
|
95
93
|
rdoc_options: []
|
|
96
94
|
require_paths:
|
|
97
95
|
- lib
|
|
@@ -99,15 +97,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
99
97
|
requirements:
|
|
100
98
|
- - ">="
|
|
101
99
|
- !ruby/object:Gem::Version
|
|
102
|
-
version:
|
|
100
|
+
version: '3.1'
|
|
103
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
102
|
requirements:
|
|
105
103
|
- - ">="
|
|
106
104
|
- !ruby/object:Gem::Version
|
|
107
105
|
version: 1.3.5
|
|
108
106
|
requirements: []
|
|
109
|
-
rubygems_version: 3.
|
|
110
|
-
signing_key:
|
|
107
|
+
rubygems_version: 3.6.9
|
|
111
108
|
specification_version: 4
|
|
112
109
|
summary: New wave Internationalization support for Ruby
|
|
113
110
|
test_files: []
|