From 4d4f3ec41b15d43f5802e310360407a85a25743d Mon Sep 17 00:00:00 2001 From: ssut Date: Tue, 21 Jul 2015 00:58:55 +0900 Subject: [PATCH] Update: for python 2.x compatibility refs #3 --- googletrans/translator.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/googletrans/translator.py b/googletrans/translator.py index 9a0a941..e831b81 100644 --- a/googletrans/translator.py +++ b/googletrans/translator.py @@ -6,6 +6,7 @@ You can translate text using this module. """ import re import requests +import sys from collections import namedtuple from future.moves.urllib.parse import quote @@ -19,6 +20,7 @@ user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/537.36 ( EXCLUDES = ['en', 'ca', 'fr'] RE_SRC = re.compile(',\[\["([\w]{2})"\]') +PY3 = sys.version_info > (3, 1) __agent = None __headers = { @@ -112,9 +114,17 @@ Advanced usage: try: pron = data[0][1][-1] except: pass + if not PY3 and isinstance(pron, unicode) and isinstance(origin, str): + origin = origin.decode('utf-8') if dest in EXCLUDES and pron == origin: pron = translated + # for python 2.x compatbillity + if not PY3: + if isinstance(src, str): src = src.decode('utf-8') + if isinstance(dest, str): dest = dest.decode('utf-8') + if isinstance(translated, str): translated = translated.decode('utf-8') + # put final values into new Translated object result = Translated(src=src, dest=dest, origin=origin, text=translated, pronunciation=pron)