tater 1.2.0 → 1.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df91dea484a66a6511be668c7738e1970a588f07e3bb71f25687b0472290117d
4
- data.tar.gz: 97966c5fa1ff9db5f1c42b1ed98720892646e8dce07e49d24e058c79c9fc08a7
3
+ metadata.gz: 6fe610aeeb910795f9f07f52bdbfa529a6f10ac5b807a8252a0d0f50e24ca6a6
4
+ data.tar.gz: a4154b7803f31babbb012f2f65f36b16f4ea929a43f30a9388b0be10d717f1d7
5
5
  SHA512:
6
- metadata.gz: e636ba94fb9e1100729adfa4422fa0dc9c0c692bc38915c114a8115c9b711838b448ee49c63653ccdabbbf467c56450aa661240392b61a833061c1e78d1ddd41
7
- data.tar.gz: 60bfea0ecab01a4d99fd28789c6cfe9d7bc5f919d430d63eb1f651e58e45166384e244868ccae42246a2668ac4eb6d63964f536d6baee4a7f1b24cfd1953ef8c
6
+ metadata.gz: e478a0bb35614afcf58b79f4d2511f02a25772decbf5e1ea5f4ccbb574ea1699fb7f13bc7174aca5ee12cb6fe0130f7a5466a069cc1fca0435569a6e5db2c58d
7
+ data.tar.gz: f7f31381024e298d33cdf53ae6609218ab671d27c2fd3c0f1e4f8595a28d87f2f46b6f9d03b27e2c186aec6fc4c4a707850287ca98420ff2ef7a2444d8c7fef9
data/README.md CHANGED
@@ -56,6 +56,25 @@ i18n.translate('some.key') # => 'This here string!'
56
56
  i18n.translate('interpolated', you: 'world') # => 'Hello world!'
57
57
  ```
58
58
 
59
+
60
+ ## Array Localization
61
+
62
+ Given an array, Tater will do it's best to join the elements of the array into a
63
+ sentence based on how many elements there are.
64
+
65
+ ```yaml
66
+ en:
67
+ array:
68
+ last_word_connector: ", and "
69
+ two_words_connector: " and "
70
+ words_connector: ", "
71
+ ```
72
+
73
+ ```ruby
74
+ i18n.localize(%w[tacos enchiladas burritos]) # => "tacos, enchiladas, and burritos"
75
+ ```
76
+
77
+
59
78
  ## Numeric Localization
60
79
 
61
80
  Numeric localization (`Numeric`, `Integer`, `Float`, and `BigDecimal`) require
data/lib/tater.rb CHANGED
@@ -141,7 +141,7 @@ class Tater
141
141
  @locale = locale.to_s if available?(locale)
142
142
  end
143
143
 
144
- # Localize a Date, Time, DateTime, or Numeric object.
144
+ # Localize an Array, Date, Time, DateTime, or Numeric object.
145
145
  #
146
146
  # @param object [Date, Time, DateTime, Numeric]
147
147
  # The object to localize.
@@ -156,6 +156,13 @@ class Tater
156
156
  # The delimiter to use when localizing numberic values.
157
157
  # @option options [String] :separator
158
158
  # The separator to use when localizing numberic values.
159
+ # @option options [String] :two_words_connector
160
+ # The string used to join two array elements together e.g. " and ".
161
+ # @option options [String] :words_connector
162
+ # The string used to connect multiple array elements e.g. ", ".
163
+ # @option options [String] :last_word_connector
164
+ # The string used to connect the final element with preceding array elements
165
+ # e.g. ", and ".
159
166
  #
160
167
  # @return [String]
161
168
  # A localized version of the object passed in.
@@ -207,6 +214,27 @@ class Tater
207
214
  end
208
215
 
209
216
  object.strftime(format)
217
+ when Array
218
+ case object.length
219
+ when 0
220
+ ''
221
+ when 1
222
+ object[0]
223
+ when 2
224
+ two_words_connector = options.delete(:two_words_connector) || lookup('array.two_words_connector', locale_override)
225
+
226
+ raise(MissingLocalizationFormat, "Sentence localization connector ('array.two_words_connector') missing or not passed as option :two_words_connector") unless two_words_connector
227
+
228
+ "#{ object[0] }#{ two_words_connector }#{ object[1] }"
229
+ else
230
+ last_word_connector = options.delete(:last_word_connector) || lookup('array.last_word_connector', locale_override)
231
+ words_connector = options.delete(:words_connector) || lookup('array.words_connector', locale_override)
232
+
233
+ raise(MissingLocalizationFormat, "Sentence localization connector ('array.last_word_connector') missing or not passed as option :last_word_connector") unless last_word_connector
234
+ raise(MissingLocalizationFormat, "Sentence localization connector ('array.words_connector') missing or not passed as option :words_connector") unless words_connector
235
+
236
+ "#{ object[0...-1].join(words_connector) }#{ last_word_connector }#{ object[-1] }"
237
+ end
210
238
  else
211
239
  raise(UnLocalizableObject, "The object class #{ object.class } cannot be localized by Tater.")
212
240
  end
@@ -8,6 +8,11 @@ en:
8
8
  deep:
9
9
  key: 'This key is deeper'
10
10
 
11
+ array:
12
+ last_word_connector: ", and "
13
+ two_words_connector: " and "
14
+ words_connector: ", "
15
+
11
16
  date:
12
17
  formats:
13
18
  default: '%Y/%-m/%-d'
data/test/tater_test.rb CHANGED
@@ -184,6 +184,35 @@ describe Tater do
184
184
  Tater.new(path: File.expand_path('test/fixtures'))
185
185
  end
186
186
 
187
+ let :fr do
188
+ Tater.new(path: File.expand_path('test/fixtures'), locale: 'fr')
189
+ end
190
+
191
+ it 'localizes arrays' do
192
+ assert_equal 'tacos and burritos', i18n.localize(%w[tacos burritos])
193
+ assert_equal 'tacos', i18n.localize(%w[tacos])
194
+ assert_equal 'tacos, enchiladas, and burritos', i18n.localize(%w[tacos enchiladas burritos])
195
+
196
+ assert_equal 'tacos + enchiladas ++ burritos', fr.localize(%w[tacos enchiladas burritos], words_connector: ' + ', last_word_connector: ' ++ ')
197
+ assert_equal 'tacostwoburritos', fr.localize(%w[tacos burritos], two_words_connector: 'two')
198
+
199
+ assert_raises(Tater::MissingLocalizationFormat) do
200
+ fr.localize(%w[tacos burritos])
201
+ end
202
+
203
+ assert_raises(Tater::MissingLocalizationFormat) do
204
+ fr.localize(%w[tacos burritos], last_word_connector: 'last', words_connector: 'words')
205
+ end
206
+
207
+ assert_raises(Tater::MissingLocalizationFormat) do
208
+ fr.localize(%w[tacos burritos], last_word_connector: 'last')
209
+ end
210
+
211
+ assert_raises(Tater::MissingLocalizationFormat) do
212
+ fr.localize(%w[tacos burritos], words_connector: 'words')
213
+ end
214
+ end
215
+
187
216
  it 'localizes Dates' do
188
217
  assert_equal '1970/1/1', i18n.localize(Date.new(1970, 1, 1))
189
218
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tater
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Lecklider
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-18 00:00:00.000000000 Z
11
+ date: 2019-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest