Skip to content

Commit 2c8d525

Browse files
committedJan 10, 2019
Merge branch 'fix/array_nil_import_and_cloud_translation'
2 parents bdbc793 + 5f72069 commit 2c8d525

File tree

9 files changed

+222
-2
lines changed

9 files changed

+222
-2
lines changed
 

‎lib/lit/cloud_translation/providers/google.rb

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def sanitize_text(text_or_array)
9696
text_or_array.gsub(/%{(.+?)}/, '<code>__LIT__\1__LIT__</code>')
9797
when Array
9898
text_or_array.map { |s| sanitize_text(s) }
99+
when nil
100+
''
99101
else
100102
raise TypeError
101103
end

‎lib/lit/cloud_translation/providers/yandex.rb

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def sanitize_text(text_or_array)
6363
text_or_array.gsub(/%{(.+?)}/, '%{_LIT_\1_LIT_}')
6464
when Array
6565
text_or_array.map { |s| sanitize_text(s) }
66+
when nil
67+
''
6668
else
6769
raise TypeError
6870
end

‎lib/lit/import.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def concatenate_arrays(csv) # rubocop:disable Metrics/MethodLength, Metrics/AbcS
158158
accu[-1] = [
159159
row.first,
160160
*accu[-1].drop(1)
161-
.map { |x| Array.wrap(x) }
161+
.map { |x| Array.wrap(x).presence || [x] }
162162
.zip(row.drop(1)).map(&:flatten)
163163
]
164164
end

‎test/cassettes/Lit_CloudTranslation_Providers_Google/when_only_to_language_is_given/when_array_containing_nil_values_is_given/translates_array_to_target_language_converting_nil_to_.yml

+80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/cassettes/Lit_CloudTranslation_Providers_Google/when_only_to_language_is_given/when_array_of_strings_is_given/translates_array_of_strings_to_target_language.yml

+53-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/cassettes/Lit_CloudTranslation_Providers_Yandex/when_only_to_language_is_given/when_array_containing_nil_values_is_given/translates_array_to_target_language_converting_nil_to_.yml

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
key,en,pl
2+
scopes.csvarray,,
3+
scopes.csvarray,val0 en,"val0 pl"
4+
scopes.csvarray,val1 en,"val1 pl"
5+
scopes.csvarray,,"val2 pl"
6+
scopes.csvarray,val3 en,

‎test/unit/import_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ def setup
154154
end
155155
end
156156

157+
test "imports from csv preserving nil values in arrays" do
158+
input = imported_file('import.csv.array_with_nil')
159+
Lit::Import.call(input: input, format: 'csv')
160+
new_localization_key =
161+
Lit::LocalizationKey.find_by(localization_key: 'scopes.csvarray')
162+
localization = lambda do |locale|
163+
new_localization_key
164+
.localizations
165+
.joins(:locale)
166+
.find_by(lit_locales: { locale: locale })
167+
end
168+
assert_equal(
169+
localization.call('en').translated_value,
170+
[nil, 'val0 en', 'val1 en', nil, 'val3 en']
171+
)
172+
assert_equal(
173+
localization.call('pl').translated_value,
174+
[nil, 'val0 pl', 'val1 pl', 'val2 pl', nil]
175+
)
176+
177+
end
178+
157179
def imported_file(name)
158180
File.read(Lit::Engine.root.join('test', 'fixtures', 'lit', 'files', name))
159181
end

‎test/unit/lit/cloud_translation/providers/examples.rb

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ def cloud_provider_examples(described_klass)
3232
subject.length.must_equal 2
3333
end
3434
end
35+
36+
describe 'when array containing nil values is given' do
37+
let(:text) { [nil, 'awesome', nil, 'stuff'] }
38+
39+
it 'translates array to target language, converting nil to ""' do
40+
subject.first.must_equal ''
41+
subject.second.must_be :present?
42+
subject.third.must_equal ''
43+
subject.fourth.must_be :present?
44+
subject.length.must_equal 4
45+
end
46+
end
3547
end
3648

3749
describe 'when :from and :to languages are given' do

0 commit comments

Comments
 (0)