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

Commit

Permalink
Merge branch 'master' into distinguish_different_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-wovn authored Jul 28, 2021
2 parents d42cc3a + b7f3a38 commit 5671991
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/bing_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def request_new_access_token
when Net::HTTPServerError
raise UnavailableException.new("#{response.code}: Credentials server unavailable")
else
raise AuthenticationException.new('Invalid credentials')
raise Exception.new("Unsuccessful Access Token call: Code: #{response.code} (Invalid credentials?)")
end
end
end
Expand Down Expand Up @@ -168,9 +168,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 @@ -34,6 +34,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 5671991

Please sign in to comment.