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

Commit

Permalink
Find translation results with case-insensitive comparison (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
amake authored Jan 12, 2021
1 parent 08300eb commit d978ef7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
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

0 comments on commit d978ef7

Please sign in to comment.