From 5d837ca1e41187888c9c2b8518cae1f0cff1e50d Mon Sep 17 00:00:00 2001 From: Aaron Madlon-Kay Date: Tue, 12 Jan 2021 14:08:49 +0900 Subject: [PATCH] Find translation results with case-insensitive comparison --- lib/bing_translator.rb | 3 ++- spec/bing_translator_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/bing_translator.rb b/lib/bing_translator.rb index bce5541..3b17e35 100755 --- a/lib/bing_translator.rb +++ b/lib/bing_translator.rb @@ -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 diff --git a/spec/bing_translator_spec.rb b/spec/bing_translator_spec.rb index 5c466cc..1a869a1 100644 --- a/spec/bing_translator_spec.rb +++ b/spec/bing_translator_spec.rb @@ -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