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

Stop modifying locale before sending to Google Translate #558

Merged
merged 1 commit into from
Mar 30, 2024
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
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 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 @@

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
Loading