i18n-bamboo 0.1.1 → 0.2.0

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: 3cfd6e263d8c6757068d359b5051c93e90a6ceb2
4
- data.tar.gz: 3e625ee1a048a3da2a172f16b136837ea6ad4eab
3
+ metadata.gz: e1ed871413db5574b95cef0cd50a9e140fa4b235
4
+ data.tar.gz: ef51776267eee5b61a32ef40e879f7e319f53163
5
5
  SHA512:
6
- metadata.gz: 48ae9eaca6ebcfa6145edf982ba192a658028d27fadf28c54508698b0a4cc20646f468d4be670326ca6a9118ce370f9fa9598a432973be30e8e3fc49cc156208
7
- data.tar.gz: 53313d4621b421ec4557d2842cbb85ebba71afb1375ec443af0eaa151d14e8a0b8f8a38f8719872a82383e4bf86687f83685d2f19ffeb946c0161d0faa06406a
6
+ metadata.gz: b0095145aa0daa4b811eaecaad11ee013f380c50dac20b225bc1ffdf70494172992f9dbd84d324e3d9ca012aa65d4c2411a7ca8f18fa8aed742181c9cc3b6c54
7
+ data.tar.gz: 9baade06a8872c029bd464998dc347367cde7c45ddbe5c1732d3eec55cbea20f1c5a34a036c935d3a247db3ef80197cbc5ce4abc60aa178e1c9a8f90507b614c
data/README.md CHANGED
@@ -50,6 +50,16 @@ I18n.t('bird')
50
50
  # => "Feathered Friend"
51
51
  ```
52
52
 
53
+ Additionally, you can temporarily override this new behavior of the `translate` and `localize` methods by passing in the `:force_default_behavior` option.
54
+
55
+ ```ruby
56
+ # Assume that I18n.default_locale = :en
57
+ I18n.t('car', force_default_behavior: true)
58
+ # => "Car"
59
+ I18n.t('car', force_default_behavior: false)
60
+ # => "Automobil"
61
+ ```
62
+
53
63
  ## Installation
54
64
 
55
65
  Add this line to your application's Gemfile:
@@ -8,13 +8,25 @@ module I18n
8
8
  alias_method :original_translate, :translate
9
9
  alias_method :original_localize, :localize
10
10
 
11
+ # Public: Unless overridden, translates using the longest translation from among all available locales.
12
+ #
13
+ # *args - see http://goo.gl/vSpFKl for original method argument doc
14
+ # options - The final argument can optionally be a Hash of options
15
+ # :force_default_behavior - Boolean that determines whether or not to force the default translate
16
+ # behavior
17
+ #
18
+ # Returns the longest translated String.
11
19
  def translate(*args)
20
+ # Fall back to the default translate behavior if the :force_default_behavior is true
21
+ force_current_locale = args.last.is_a?(Hash) ? args.pop.delete(:force_default_behavior) : false
22
+ return original_translate(*args) if force_current_locale
23
+
12
24
  translations = []
13
25
 
14
26
  available_locales.each do |locale|
15
27
  args_copy = args.dup
16
28
 
17
- if args_copy.last.is_a? Hash
29
+ if args_copy.last.is_a?(Hash)
18
30
  args_copy.last.merge!(locale: locale)
19
31
  else
20
32
  args_copy << {locale: locale}
@@ -26,7 +38,19 @@ module I18n
26
38
  translations.max
27
39
  end
28
40
 
41
+ # Public: Unless overridden, translates using the longest localized value from among all available locales.
42
+ #
43
+ # object - see http://goo.gl/bbXILw for original method argument doc
44
+ # options - see http://goo.gl/bbXILw for original method argument doc. This Hash can can contain the
45
+ # the following custom key:
46
+ # :force_default_behavior - Boolean that determines whether or not to force the default localize
47
+ # behavior
48
+ #
49
+ # Returns the longest localized String.
29
50
  def localize(object, options = {})
51
+ # Fall back to the default localize behavior if the :force_default_behavior is true
52
+ return original_localize(object, options) if options.delete(:force_default_behavior)
53
+
30
54
  localized_values = []
31
55
 
32
56
  available_locales.each do |locale|
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Bamboo
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -19,66 +19,202 @@ RSpec.describe 'I18n monkey patching' do
19
19
  end
20
20
 
21
21
  describe '#translate' do
22
- before do
23
- expect(I18n).to receive(:original_translate)
24
- .with(key, {locale: :en})
25
- .and_return(short_translation)
26
-
27
- expect(I18n).to receive(:original_translate)
28
- .with(key, {locale: :zh})
29
- .and_return(long_translation)
22
+ context 'when the :force_default_behavior option is passed in' do
23
+ context 'when :force_default_behavior is true' do
24
+ before do
25
+ expect(I18n).to receive(:original_translate).with(key).and_return(short_translation)
26
+ expect(I18n).not_to receive(:original_translate).with(key, locale: :en)
27
+ expect(I18n).not_to receive(:original_translate).with(key, locale: :zh)
28
+ end
29
+
30
+ it 'calls through to original_translate without modifying its arguments and returns the translation' do
31
+ expect(I18n.translate(key, force_default_behavior: true)).to eq(short_translation)
32
+ end
33
+ end
34
+
35
+ context 'when :force_default_behavior is false' do
36
+ before do
37
+ expect(I18n).not_to receive(:original_translate).with(key)
38
+ expect(I18n).to receive(:original_translate)
39
+ .with(key, locale: :en)
40
+ .and_return(short_translation)
41
+
42
+ expect(I18n).to receive(:original_translate)
43
+ .with(key, locale: :zh)
44
+ .and_return(long_translation)
45
+ end
46
+
47
+ it 'returns the longest translation from all available locales' do
48
+ expect(I18n.translate(key, force_default_behavior: false)).to eq(long_translation)
49
+ end
50
+ end
30
51
  end
31
52
 
32
- it 'returns the longest translation' do
33
- expect(I18n.translate(key)).to eq(long_translation)
53
+ context 'when the :force_default_behavior option is not passed in' do
54
+ before do
55
+ expect(I18n).not_to receive(:original_translate).with(key)
56
+ expect(I18n).to receive(:original_translate)
57
+ .with(key, locale: :en)
58
+ .and_return(short_translation)
59
+
60
+ expect(I18n).to receive(:original_translate)
61
+ .with(key, locale: :zh)
62
+ .and_return(long_translation)
63
+ end
64
+
65
+ it 'returns the longest translation from all available locales' do
66
+ expect(I18n.translate(key)).to eq(long_translation)
67
+ end
34
68
  end
35
69
  end
36
70
 
37
71
  describe '#t' do
38
- before do
39
- expect(I18n).to receive(:original_translate)
40
- .with(key, {locale: :en})
41
- .and_return(short_translation)
42
-
43
- expect(I18n).to receive(:original_translate)
44
- .with(key, {locale: :zh})
45
- .and_return(long_translation)
72
+ context 'when the :force_default_behavior option is passed in' do
73
+ context 'when :force_default_behavior is true' do
74
+ before do
75
+ expect(I18n).to receive(:original_translate).with(key).and_return(short_translation)
76
+ expect(I18n).not_to receive(:original_translate).with(key, locale: :en)
77
+ expect(I18n).not_to receive(:original_translate).with(key, locale: :zh)
78
+ end
79
+
80
+ it 'calls through to original_translate without modifying its arguments and returns the translation' do
81
+ expect(I18n.t(key, force_default_behavior: true)).to eq(short_translation)
82
+ end
83
+ end
84
+
85
+ context 'when :force_default_behavior is false' do
86
+ before do
87
+ expect(I18n).not_to receive(:original_translate).with(key)
88
+ expect(I18n).to receive(:original_translate)
89
+ .with(key, locale: :en)
90
+ .and_return(short_translation)
91
+
92
+ expect(I18n).to receive(:original_translate)
93
+ .with(key, locale: :zh)
94
+ .and_return(long_translation)
95
+ end
96
+
97
+ it 'returns the longest translation from all available locales' do
98
+ expect(I18n.t(key, force_default_behavior: false)).to eq(long_translation)
99
+ end
100
+ end
46
101
  end
47
102
 
48
- it 'returns the longest translation' do
49
- expect(I18n.t(key)).to eq(long_translation)
103
+ context 'when the :force_default_behavior option is not passed in' do
104
+ before do
105
+ expect(I18n).not_to receive(:original_translate).with(key)
106
+ expect(I18n).to receive(:original_translate)
107
+ .with(key, locale: :en)
108
+ .and_return(short_translation)
109
+
110
+ expect(I18n).to receive(:original_translate)
111
+ .with(key, locale: :zh)
112
+ .and_return(long_translation)
113
+ end
114
+
115
+ it 'returns the longest translation from all available locales' do
116
+ expect(I18n.translate(key)).to eq(long_translation)
117
+ end
50
118
  end
51
119
  end
52
120
 
53
121
  describe '#localize' do
54
- before do
55
- expect(I18n).to receive(:original_localize)
56
- .with(time, {locale: :en})
57
- .and_return(short_time)
58
-
59
- expect(I18n).to receive(:original_localize)
60
- .with(time, {locale: :zh})
61
- .and_return(long_time)
122
+ context 'when the :force_default_behavior option is passed in' do
123
+ context 'when :force_default_behavior is true' do
124
+ before do
125
+ expect(I18n).to receive(:original_localize).with(time, {}).and_return(short_time)
126
+ expect(I18n).not_to receive(:original_localize).with(time, locale: :en)
127
+ expect(I18n).not_to receive(:original_localize).with(time, locale: :zh)
128
+ end
129
+
130
+ it 'calls through to original_localize without modifying its arguments and returns the localized value' do
131
+ expect(I18n.localize(time, force_default_behavior: true)).to eq(short_time)
132
+ end
133
+ end
134
+
135
+ context 'when :force_default_behavior is false' do
136
+ before do
137
+ expect(I18n).not_to receive(:original_localize).with(time)
138
+ expect(I18n).to receive(:original_localize)
139
+ .with(time, locale: :en)
140
+ .and_return(short_time)
141
+
142
+ expect(I18n).to receive(:original_localize)
143
+ .with(time, locale: :zh)
144
+ .and_return(long_time)
145
+ end
146
+
147
+ it 'returns the longest localized value from all available locales' do
148
+ expect(I18n.localize(time, force_default_behavior: false)).to eq(long_time)
149
+ end
150
+ end
62
151
  end
63
152
 
64
- it 'returns the longest translation' do
65
- expect(I18n.localize(time)).to eq(long_time)
153
+ context 'when the :force_default_behavior option is not passed in' do
154
+ before do
155
+ expect(I18n).not_to receive(:original_localize).with(time)
156
+ expect(I18n).to receive(:original_localize)
157
+ .with(time, locale: :en)
158
+ .and_return(short_time)
159
+
160
+ expect(I18n).to receive(:original_localize)
161
+ .with(time, locale: :zh)
162
+ .and_return(long_time)
163
+ end
164
+
165
+ it 'returns the longest translation from all available locales' do
166
+ expect(I18n.localize(time)).to eq(long_time)
167
+ end
66
168
  end
67
169
  end
68
170
 
69
171
  describe '#l' do
70
- before do
71
- expect(I18n).to receive(:original_localize)
72
- .with(time, {locale: :en})
73
- .and_return(short_time)
74
-
75
- expect(I18n).to receive(:original_localize)
76
- .with(time, {locale: :zh})
77
- .and_return(long_time)
172
+ context 'when the :force_default_behavior option is passed in' do
173
+ context 'when :force_default_behavior is true' do
174
+ before do
175
+ expect(I18n).to receive(:original_localize).with(time, {}).and_return(short_time)
176
+ expect(I18n).not_to receive(:original_localize).with(time, locale: :en)
177
+ expect(I18n).not_to receive(:original_localize).with(time, locale: :zh)
178
+ end
179
+
180
+ it 'calls through to original_localize without modifying its arguments and returns the localized value' do
181
+ expect(I18n.l(time, force_default_behavior: true)).to eq(short_time)
182
+ end
183
+ end
184
+
185
+ context 'when :force_default_behavior is false' do
186
+ before do
187
+ expect(I18n).not_to receive(:original_localize).with(time)
188
+ expect(I18n).to receive(:original_localize)
189
+ .with(time, locale: :en)
190
+ .and_return(short_time)
191
+
192
+ expect(I18n).to receive(:original_localize)
193
+ .with(time, locale: :zh)
194
+ .and_return(long_time)
195
+ end
196
+
197
+ it 'returns the longest localized value from all available locales' do
198
+ expect(I18n.l(time, force_default_behavior: false)).to eq(long_time)
199
+ end
200
+ end
78
201
  end
79
202
 
80
- it 'returns the longest translation' do
81
- expect(I18n.l(time)).to eq(long_time)
203
+ context 'when the :force_default_behavior option is not passed in' do
204
+ before do
205
+ expect(I18n).not_to receive(:original_localize).with(time)
206
+ expect(I18n).to receive(:original_localize)
207
+ .with(time, locale: :en)
208
+ .and_return(short_time)
209
+
210
+ expect(I18n).to receive(:original_localize)
211
+ .with(time, locale: :zh)
212
+ .and_return(long_time)
213
+ end
214
+
215
+ it 'returns the longest translation from all available locales' do
216
+ expect(I18n.l(time)).to eq(long_time)
217
+ end
82
218
  end
83
219
  end
84
220
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-bamboo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Downey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n