Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Find translation results with case-insensitive comparison #45

Merged
merged 1 commit into from
Jan 12, 2021
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
3 changes: 2 additions & 1 deletion lib/bing_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ def translation_request(texts, params)

data = texts.map { |text| { 'Text' => text } }.to_json
response_json = api_client.post('/translate', params: params, data: data)
to_lang = params[:to].to_s
response_json.map do |translation|
# There should be just one translation, but who knows...
translation['translations'].find { |result| result['to'] == params[:to].to_s }
translation['translations'].find { |result| result['to'].casecmp(to_lang).zero? }
end
end
end
5 changes: 5 additions & 0 deletions spec/bing_translator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def load_file(filename)
expect(result).to eq 'Diese Nachricht sollte übersetzt werden'
end

it 'translates text to complex language code' do
result = translator.translate message_en, from: :en, to: :'fr-ca'
expect(result).to eq 'Ce message doit être traduit'
end

it 'translates long texts (up to allowed limit)' do
result = translator.translate long_text, from: :en, to: :ru
expect(result.size).to be > 1000
Expand Down