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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d68768849991fa2e1aef1064d10c70a0c106e3e1
4
- data.tar.gz: 010c42fa301e3b113586a30ef7e6e1e2ae014059
3
+ metadata.gz: f015471aad735a53641fc7c929813edf85b002fb
4
+ data.tar.gz: be9b3642de7ac04cadbdebd43891a2ed168a2f3c
5
5
  SHA512:
6
- metadata.gz: 58bea6441da44a2a256ca9ed53a7bb1116a1c4b9d85250846f6e5e84e55d245d7ee626f5d1258556b3527d7ad1893ff8287da9a96e265a740a8d513ca3293bdd
7
- data.tar.gz: 5ff92cc80d614a94f78c2852f0e8ef9008cc1e75bdbd2728f89fa35c89eb98d30a0526192fb378b450f88fbcba65af819706482c91ec31b0d2a7d4c73f25cbeb
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, options) }
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)
@@ -1,3 +1,3 @@
1
1
  module TextHelpers
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -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: "<#{@email_address}>",
26
- test_key: "*#{@scoped_text}*",
27
- list_key: "* #{@scoped_text}",
28
- interpolated_key: "Global? (!test_key!)",
29
- interpol_arg_key: "Interpolate global? (!interpolated_key!)",
30
- recursive_key: "Recursively !test.interpolated_key!",
31
- quoted_key: "They're looking for \"#{@global_text}\"--#{@scoped_text}",
32
- argument_key: "This is what %{user} said",
33
- number_key: "120\"",
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.1
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-01-24 00:00:00.000000000 Z
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport