text_helpers 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/text_helpers/translation.rb +4 -1
- data/lib/text_helpers/version.rb +1 -1
- data/test/lib/text_helpers/translation_test.rb +63 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f015471aad735a53641fc7c929813edf85b002fb
|
4
|
+
data.tar.gz: be9b3642de7ac04cadbdebd43891a2ed168a2f3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd136498aad27d06f3bdbb176fb329caa4b82fe9bf329eb8fd70709c1163acae036c22080a611680425f140b46be4ea765c31ebcfa32fb753e13ba9e164cf877
|
7
|
+
data.tar.gz: 8702f859d002d2298ec3b3353e09232fc0d1026d8b23f7c55a48f80fb3f1386b36c2ff5a96b9b9d27dbed297a0d53a5dc5fc5b5103f5018f4f2520eb6f676b10
|
@@ -26,9 +26,12 @@ module TextHelpers
|
|
26
26
|
default: "!#{key}!"
|
27
27
|
}.merge(options)).strip
|
28
28
|
|
29
|
+
interpolation_options = options.dup
|
30
|
+
interpolation_options[:cascade] = true unless interpolation_options.has_key?(:cascade)
|
31
|
+
|
29
32
|
# Interpolate any keypaths (e.g., `!some.lookup.path/key!`) found in the text.
|
30
33
|
while text =~ /!([\w._\/]+)!/ do
|
31
|
-
text = text.gsub(/!([\w._\/]+)!/) { |match| I18n.t($1,
|
34
|
+
text = text.gsub(/!([\w._\/]+)!/) { |match| I18n.t($1, interpolation_options) }
|
32
35
|
end
|
33
36
|
|
34
37
|
text = smartify(text) if options.fetch(:smart, true)
|
data/lib/text_helpers/version.rb
CHANGED
@@ -4,6 +4,7 @@ describe TextHelpers::Translation do
|
|
4
4
|
before do
|
5
5
|
@helper = Object.send(:include, TextHelpers::Translation).new
|
6
6
|
end
|
7
|
+
|
7
8
|
describe "given a stored I18n lookup" do
|
8
9
|
before do
|
9
10
|
@scoped_text = "Scoped lookup"
|
@@ -17,20 +18,23 @@ describe TextHelpers::Translation do
|
|
17
18
|
|
18
19
|
@nb_scoped_text = "Scoped lookup"
|
19
20
|
|
21
|
+
I18n.exception_handler = nil
|
22
|
+
|
20
23
|
I18n.backend.store_translations :en, {
|
21
24
|
test_key: @global_text,
|
22
25
|
multiline_key: @multiline_text,
|
23
26
|
interpolated_key: "%{interpolate_with}",
|
24
27
|
test: {
|
25
|
-
email_key:
|
26
|
-
test_key:
|
27
|
-
list_key:
|
28
|
-
interpolated_key:
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
email_key: "<#{@email_address}>",
|
29
|
+
test_key: "*#{@scoped_text}*",
|
30
|
+
list_key: "* #{@scoped_text}",
|
31
|
+
interpolated_key: "Global? (!test_key!)",
|
32
|
+
interpolated_scoped_key: "Global? (!test_scoped_key!)",
|
33
|
+
interpol_arg_key: "Interpolate global? (!interpolated_key!)",
|
34
|
+
recursive_key: "Recursively !test.interpolated_key!",
|
35
|
+
quoted_key: "They're looking for \"#{@global_text}\"--#{@scoped_text}",
|
36
|
+
argument_key: "This is what %{user} said",
|
37
|
+
number_key: "120\"",
|
34
38
|
pluralized_key: {
|
35
39
|
one: "A single piece of text",
|
36
40
|
other: "%{count} pieces of text"
|
@@ -39,6 +43,10 @@ describe TextHelpers::Translation do
|
|
39
43
|
}
|
40
44
|
end
|
41
45
|
|
46
|
+
after do
|
47
|
+
I18n.backend.reload!
|
48
|
+
end
|
49
|
+
|
42
50
|
describe "for a specified scope" do
|
43
51
|
before do
|
44
52
|
@helper.define_singleton_method :translation_scope do
|
@@ -151,5 +159,51 @@ describe TextHelpers::Translation do
|
|
151
159
|
assert_equal @global_text, @helper.text(:test_key)
|
152
160
|
end
|
153
161
|
end
|
162
|
+
|
163
|
+
describe "when a scope is given as an option" do
|
164
|
+
before do
|
165
|
+
@helper.define_singleton_method :translation_scope do
|
166
|
+
'test'
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
it "shows translation missing if an interpolated key isn't found at the same scope" do
|
171
|
+
expected = "Global? (translation missing: en.test.test_scoped_key)"
|
172
|
+
assert_equal expected, @helper.text(:interpolated_scoped_key, scope: "test")
|
173
|
+
end
|
174
|
+
|
175
|
+
it "interpolates the key if one is found at the same scope" do
|
176
|
+
I18n.backend.store_translations(:en, {
|
177
|
+
test: {test_scoped_key: "a translation"}})
|
178
|
+
|
179
|
+
assert_equal "Global? (a translation)", @helper.text(:interpolated_scoped_key, scope: "test")
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "with the Cascade backend in place" do
|
183
|
+
before do
|
184
|
+
@original_backend = I18n.backend
|
185
|
+
new_backend = @original_backend.dup
|
186
|
+
new_backend.extend(I18n::Backend::Cascade)
|
187
|
+
I18n.backend = new_backend
|
188
|
+
end
|
189
|
+
|
190
|
+
after do
|
191
|
+
I18n.backend = @original_backend
|
192
|
+
end
|
193
|
+
|
194
|
+
it "cascades the interpolated key by default" do
|
195
|
+
I18n.backend.store_translations(:en, {test_scoped_key: "a translation"})
|
196
|
+
|
197
|
+
assert_equal "Global? (a translation)", @helper.text(:interpolated_scoped_key, scope: "test")
|
198
|
+
end
|
199
|
+
|
200
|
+
it "doesn't cascade if cascade: false is passed" do
|
201
|
+
I18n.backend.store_translations(:en, {test_scoped_key: "a translation"})
|
202
|
+
|
203
|
+
expected = "Global? (translation missing: en.test.test_scoped_key)"
|
204
|
+
assert_equal expected, @helper.text(:interpolated_scoped_key, scope: "test", cascade: false)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
154
208
|
end
|
155
209
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: text_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Horner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|