Skip to content

Commit

Permalink
Fix translation of plural HTML keys #193
Browse files Browse the repository at this point in the history
Now recognizes them as HTML correctly
  • Loading branch information
glebm committed Mar 19, 2016
1 parent 2b34355 commit e8db4a1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/tasks/google_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def google_translate_list(list, opts)
# copy reference keys as is, instead of translating
reference_key_vals = list.select { |_k, v| v.is_a? Symbol } || []
list -= reference_key_vals
result = list.group_by { |k_v| HtmlKeys.html_key? k_v[0] }.map { |is_html, list_slice|
result = list.group_by { |k_v| html_key? k_v[0], opts[:from] }.map { |is_html, list_slice|
fetch_google_translations list_slice, opts.merge(is_html ? {html: true} : {format: 'text'})
}.reduce(:+) || []
result.concat(reference_key_vals)
Expand Down
10 changes: 6 additions & 4 deletions lib/i18n/tasks/html_keys.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true
module I18n::Tasks
module HtmlKeys
extend self
HTML_KEY_PATTERN = /[.\-_]html\z/.freeze
HTML_KEY_PATTERN = /[.\-_]html\z/
MAYBE_PLURAL_HTML_KEY_PATTERN = /[.\-_]html\.[^.]+\z/

def html_key?(full_key)
!!(full_key =~ HTML_KEY_PATTERN)
def html_key?(full_key, locale)
!!(full_key =~ HTML_KEY_PATTERN ||
full_key =~ MAYBE_PLURAL_HTML_KEY_PATTERN &&
depluralize_key(split_key(full_key, 2)[1], locale) =~ HTML_KEY_PATTERN)
end
end
end
104 changes: 48 additions & 56 deletions spec/google_translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,61 @@
require 'i18n/tasks/commands'

RSpec.describe 'Google Translation' do
include I18n::Tasks::GoogleTranslation

tests = [
nil_value_test = ['nil-value-key', nil, nil],
text_test = ['key', "Hello - %{user} O'Neill!", "Hola - %{user} O'Neill!"],
html_test = ['html-key.html', "Hello - <b>%{user} O'neill</b>", "Hola - <b>%{user} O'neill</b>"],
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!", "Hola - %{user} O'Neill!"]
html_test = ['html-key.html', "Hello - <b>%{user} O'neill</b>", "Hola - <b>%{user} O'neill</b>"]
html_test_plrl = ['html-key.html.one', '<b>Hello %{count}</b>', '<b>Hola %{count}</b>']
array_test = ['array-key', ['Hello.', nil, '', 'Goodbye.'], ['Hola.', nil, '', 'Adiós.']]
fixnum_test = ['numeric-key', 1, 1]
ref_key_test = ['ref-key', :reference, :reference]

if ENV['GOOGLE_TRANSLATE_API_KEY']
describe 'real world test' do
delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase
describe 'real world test' do
delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase

context '#google_translate_list' do
it "works with #{tests.map(&:first)}" do
# Just one test with all the cases to lower the Google bill
translations = google_translate_list(
tests.map { |t| t[0..1] }, from: :en, to: :es, key: ENV['GOOGLE_TRANSLATE_API_KEY'])
expect(translations).to eq(tests.map { |t| [t[0], t[2]] })
end
end

before do
TestCodebase.setup('config/locales/en.yml' => '', 'config/locales/es.yml' => '')
end
before do
TestCodebase.setup('config/locales/en.yml' => '', 'config/locales/es.yml' => '')
end

after do
TestCodebase.teardown
end
after do
TestCodebase.teardown
end

context 'command' do
let(:task) { i18n_task }
context 'command' do
let(:task) { i18n_task }

it 'works' do
in_test_app_dir do
task.data[:en] = build_tree('en' => {
'common' => {
'a' => 'λ',
'hello' => text_test[1],
'hello_html' => html_test[1],
'array_key' => array_test[1],
'nil-value-key' => nil_value_test[1],
'fixnum-key' => fixnum_test[1],
'ref-key' => ref_key_test[1]
}
})
task.data[:es] = build_tree('es' => {
'common' => {
'a' => 'λ',
}
})
it 'works' do
skip 'GOOGLE_TRANSLATE_API_KEY env var not set' unless ENV['GOOGLE_TRANSLATE_API_KEY']
in_test_app_dir do
task.data[:en] = build_tree('en' => {
'common' => {
'a' => 'λ',
'hello' => text_test[1],
'hello_html' => html_test[1],
'hello_plural_html' => {
'one' => html_test_plrl[1]
},
'array_key' => array_test[1],
'nil-value-key' => nil_value_test[1],
'fixnum-key' => fixnum_test[1],
'ref-key' => ref_key_test[1]
}
})
task.data[:es] = build_tree('es' => {
'common' => {
'a' => 'λ',
}
})

run_cmd 'translate-missing'
expect(task.t('common.hello', 'es')).to eq(text_test[2])
expect(task.t('common.hello_html', 'es')).to eq(html_test[2])
expect(task.t('common.array_key', 'es')).to eq(array_test[2])
expect(task.t('common.nil-value-key', 'es')).to eq(nil_value_test[2])
expect(task.t('common.fixnum-key', 'es')).to eq(fixnum_test[2])
expect(task.t('common.ref-key', 'es')).to eq(ref_key_test[2])
expect(task.t('common.a', 'es')).to eq('λ')
end
run_cmd 'translate-missing'
expect(task.t('common.hello', 'es')).to eq(text_test[2])
expect(task.t('common.hello_html', 'es')).to eq(html_test[2])
expect(task.t('common.hello_plural_html.one', 'es')).to eq(html_test_plrl[2])
expect(task.t('common.array_key', 'es')).to eq(array_test[2])
expect(task.t('common.nil-value-key', 'es')).to eq(nil_value_test[2])
expect(task.t('common.fixnum-key', 'es')).to eq(fixnum_test[2])
expect(task.t('common.ref-key', 'es')).to eq(ref_key_test[2])
expect(task.t('common.a', 'es')).to eq('λ')
end
end
end
Expand Down

0 comments on commit e8db4a1

Please sign in to comment.