Skip to content

Commit

Permalink
Stop modifying locale before sending to Google Translate.
Browse files Browse the repository at this point in the history
Fixes #555
Fixes #556
Fixes #557
  • Loading branch information
jqr authored and glebm committed Mar 30, 2024
1 parent 56f378a commit 3bfe80c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
13 changes: 2 additions & 11 deletions lib/i18n/tasks/translators/google_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def translate_values(list, **options)
def options_for_translate_values(from:, to:, **options)
options.merge(
api_key: api_key,
from: to_google_translate_compatible_locale(from),
to: to_google_translate_compatible_locale(to)
from: from,
to: to,

Check failure on line 26 in lib/i18n/tasks/translators/google_translator.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call.
)
end

Expand All @@ -41,15 +41,6 @@ def no_results_error_message

private

SUPPORTED_LOCALES_WITH_REGION = %w[zh-CN zh-TW].freeze

# Convert 'es-ES' to 'es'
def to_google_translate_compatible_locale(locale)
return locale unless locale.include?('-') && !SUPPORTED_LOCALES_WITH_REGION.include?(locale)

locale.split('-', 2).first
end

def api_key
@api_key ||= begin
key = @i18n_tasks.translation_config[:google_translate_api_key]
Expand Down
48 changes: 48 additions & 0 deletions spec/google_translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,52 @@
end
end
end

describe 'chinese simplified vs traditional' do
delegate :i18n_task, :in_test_app_dir, :run_cmd, to: :TestCodebase

before do
TestCodebase.setup(
'config/locales/en.yml' => '',
'config/locales/zh.yml' => '',
'config/locales/zh-cn.yml' => '',
'config/locales/zh-hans.yml' => '',
'config/locales/zh-tw.yml' => '',
'config/locales/zh-hant.yml' => '',
'config/locales/zh-hk.yml' => '',
'config/locales/es.yml' => '',

Check failure on line 83 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/TrailingCommaInArguments: Avoid comma after the last parameter of a method call.
)
end

after do
TestCodebase.teardown
end

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

it 'should allow google to decide proper language from locale' do
skip 'temporarily disabled on JRuby due to https://github.com/jruby/jruby/issues/4802' if RUBY_ENGINE == 'jruby'
skip 'GOOGLE_TRANSLATE_API_KEY env var not set' unless ENV['GOOGLE_TRANSLATE_API_KEY']
skip 'GOOGLE_TRANSLATE_API_KEY env var is empty' if ENV['GOOGLE_TRANSLATE_API_KEY'].empty?
in_test_app_dir do
task.data[:en] = build_tree('en' => { 'common' => { 'a' => 'λ', 'horse' => 'horse' } })

# Loading translations seems to require at least one existing value.
%w(zh zh-cn zh-hans zh-tw zh-hant zh-hk).each do |locale|

Check failure on line 102 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/PercentLiteralDelimiters: %w-literals should be delimited by [ and ].
task.data[locale] = build_tree(locale => { 'common' => { 'a' => 'λ' } })
end

run_cmd 'translate-missing'

expect(task.t('common.horse', 'zh' )).to eq("马") # Simplified Chinese

Check failure on line 108 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideParens: Space inside parentheses detected.

Check failure on line 108 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
expect(task.t('common.horse', 'zh-cn' )).to eq("马") # Simplified Chinese

Check failure on line 109 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideParens: Space inside parentheses detected.

Check failure on line 109 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
expect(task.t('common.horse', 'zh-hans')).to eq("马") # Simplified Chinese

Check failure on line 110 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
expect(task.t('common.horse', 'zh-tw' )).to eq("馬") # Traditional Chinese

Check failure on line 111 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideParens: Space inside parentheses detected.

Check failure on line 111 in spec/google_translate_spec.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
expect(task.t('common.horse', 'zh-hant')).to eq("馬") # Traditional Chinese
expect(task.t('common.horse', 'zh-hk' )).to eq("馬") # Traditional Chinese
end
end
end
end
end

0 comments on commit 3bfe80c

Please sign in to comment.