Skip to content

Commit

Permalink
Parse array of hashs for translators
Browse files Browse the repository at this point in the history
With reference to ticket glebm#456, this change now allows you to translate lists of hashes with DeepL and other translators.
  • Loading branch information
georf committed Nov 13, 2023
1 parent e939d0e commit 5df9e85
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
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
21 changes: 13 additions & 8 deletions spec/deepl_translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
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 +48,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

0 comments on commit 5df9e85

Please sign in to comment.