From b1b77ff77e9dda21f5eb310b3ba5a74c8eda5f05 Mon Sep 17 00:00:00 2001 From: Elijah Miller Date: Fri, 29 Mar 2024 15:38:38 -0400 Subject: [PATCH] Stop modifying locale before sending to Google Translate. Fixes #555 Fixes #556 Fixes #557 --- .../tasks/translators/google_translator.rb | 13 +---- spec/google_translate_spec.rb | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/i18n/tasks/translators/google_translator.rb b/lib/i18n/tasks/translators/google_translator.rb index 90e9fe36..10437390 100644 --- a/lib/i18n/tasks/translators/google_translator.rb +++ b/lib/i18n/tasks/translators/google_translator.rb @@ -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, ) end @@ -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] diff --git a/spec/google_translate_spec.rb b/spec/google_translate_spec.rb index 939f196f..da801254 100644 --- a/spec/google_translate_spec.rb +++ b/spec/google_translate_spec.rb @@ -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' => '', + ) + 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| + task.data[locale] = build_tree(locale => { 'common' => { 'a' => 'λ' } }) + end + + run_cmd 'translate-missing' + + expect(task.t('common.horse', 'zh' )).to eq("马") # Simplified Chinese + expect(task.t('common.horse', 'zh-cn' )).to eq("马") # Simplified Chinese + expect(task.t('common.horse', 'zh-hans')).to eq("马") # Simplified Chinese + expect(task.t('common.horse', 'zh-tw' )).to eq("馬") # Traditional Chinese + 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