From 0e1c9d1d52f3c5c8ad71de9e295ecbb81670b833 Mon Sep 17 00:00:00 2001 From: Terry Zhuo Date: Wed, 29 Jul 2020 19:46:29 +1000 Subject: [PATCH] feature: format detection outputs (#207) * feature: format detection output * feature: format detection outputs * feature: format detection outputs --- googletrans/client.py | 8 ++++++-- tests/test_client.py | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/googletrans/client.py b/googletrans/client.py index 1ef1e24..2453b0d 100644 --- a/googletrans/client.py +++ b/googletrans/client.py @@ -259,8 +259,12 @@ class Translator: src = '' confidence = 0.0 try: - src = ''.join(data[8][0]) - confidence = data[8][-2][0] + if len(data[8][0]) > 1: + src = data[8][0] + confidence = data[8][-2] + else: + src = ''.join(data[8][0]) + confidence = data[8][-2][0] except Exception: # pragma: nocover pass result = Detected(lang=src, confidence=confidence) diff --git a/tests/test_client.py b/tests/test_client.py index e1e7109..89dad6e 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -84,18 +84,21 @@ def test_translate_list(translator): def test_detect_language(translator): ko = translator.detect(u'한국어') en = translator.detect('English') - + rubg = translator.detect('тест') + assert ko.lang == 'ko' assert en.lang == 'en' + assert rubg.lang == ['ru', 'bg'] def test_detect_list(translator): - items = [u'한국어', ' English'] + items = [u'한국어', ' English', 'тест'] result = translator.detect(items) assert result[0].lang == 'ko' assert result[1].lang == 'en' + assert result[2].lang == ['ru', 'bg'] def test_src_in_special_cases(translator):