Skip to content

Commit

Permalink
Update: don't encode string if python 3 is used
Browse files Browse the repository at this point in the history
refs ssut#1
  • Loading branch information
ssut committed Jul 17, 2015
1 parent 1230ace commit 2849777
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions translate
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import sys
from googletrans import translator

PY3 = sys.version_info > (3, 1)

def main():
parser = argparse.ArgumentParser(
description='Python Google Translator as a command-line tool')
Expand All @@ -25,15 +28,16 @@ def main():
return

result = translator.translate(args.text, dest=args.dest, src=args.src)
text = result.text if PY3 else result.text.encode('utf-8', 'ignore')
pronunciation = result.pronunciation if PY3 else result.pronunciation.encode('utf-8', 'ignore')
result = """
[{src}] {original}
->
[{dest}] {text}
[pron.] {pronunciation}
""".strip().format(src=result.src, dest=result.dest, original=result.origin,
text=result.text.encode('utf-8', 'ignore'),
pronunciation=result.pronunciation.encode('utf-8',
'ignore'))
text=text,
pronunciation=pronunciation)
print(result)

if __name__ == '__main__':
Expand Down

0 comments on commit 2849777

Please sign in to comment.