Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse array of hashs for translators #531

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/i18n/tasks/translators/base_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def dump_value(value, opts)
when Array
# dump recursively
value.map { |v| dump_value(v, opts) }
when Hash
# dump recursively
value.values.map { |v| dump_value(v, opts) }
when String
value = CGI.escapeHTML(value) if opts[:html_escape]
replace_interpolations value unless value.empty?
Expand All @@ -85,6 +88,9 @@ def parse_value(untranslated, each_translated, opts)
when Array
# implode array
untranslated.map { |from| parse_value(from, each_translated, opts) }
when Hash
# implode hash
untranslated.transform_values { |value| parse_value(value, each_translated, opts) }
when String
if untranslated.empty?
untranslated
Expand Down
20 changes: 12 additions & 8 deletions spec/deepl_translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
require 'deepl'

RSpec.describe 'DeepL Translation' do
nil_value_test = ['nil-value-key', nil, nil]
text_test = ['key', "Hello, %{user} O'Neill! How are you?", "¡Hola, %{user} O'Neill! ¿Qué tal estás?"]
html_test_plrl = ['html-key.html.one', '<span>Hello %{count}</span>', '<span>Hola %{count}</span>']
array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']]
fixnum_test = ['numeric-key', 1, 1]
ref_key_test = ['ref-key', :reference, :reference]
nil_value_test = ['nil-value-key', nil, nil]
text_test = ['key', "Hello, %{user} O'Neill! How are you?", "¡Hola, %{user} O'Neill! ¿Qué tal estás?"]
html_test_plrl = ['html-key.html.one', '<span>Hello %{count}</span>', '<span>Hola %{count}</span>']
array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']]
array_hash_test = ['array-hash-key',
[{ 'hash_key1' => 'How are you?' }, { 'hash_key2' => nil }, { 'hash_key3' => 'Well.' }],
[{ 'hash_key1' => '¿Qué tal?' }, { 'hash_key2' => nil }, { 'hash_key3' => 'Bien.' }]]
fixnum_test = ['numeric-key', 1, 1]
ref_key_test = ['ref-key', :reference, :reference]
# this test fails atm due to moving of the bold tag => "Hola, <b>%{user} </b> gran O'neill ❤︎ "
# it could be a bug, but the api also allows to ignore certain tags and there is the new html-markup version which
# could be used too
html_test = ['html-key.html', "Hello, <b>%{user} big O'neill</b> ❤︎", "Hola, <b>%{user} gran O'neill</b> ❤︎"]
support_test = ['support', '%{model} or similar', '%{model} o similar']
html_test = ['html-key.html', "Hello, <b>%{user} big O'neill</b> ❤︎", "Hola, <b>%{user} gran O'neill</b> ❤︎"]
support_test = ['support', '%{model} or similar', '%{model} o similar']

describe 'real world test' do
delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase
Expand Down Expand Up @@ -44,6 +47,7 @@
'one' => html_test_plrl[1]
},
'array_key' => array_test[1],
'array_hash_key' => array_hash_test[1],
'nil-value-key' => nil_value_test[1],
'fixnum-key' => fixnum_test[1],
'ref-key' => ref_key_test[1],
Expand Down