parent
1230ace7b2
commit
28497778a1
10
translate
10
translate
@ -1,8 +1,11 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import argparse
|
import argparse
|
||||||
|
import sys
|
||||||
from googletrans import translator
|
from googletrans import translator
|
||||||
|
|
||||||
|
PY3 = sys.version_info > (3, 1)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='Python Google Translator as a command-line tool')
|
description='Python Google Translator as a command-line tool')
|
||||||
@ -25,15 +28,16 @@ def main():
|
|||||||
return
|
return
|
||||||
|
|
||||||
result = translator.translate(args.text, dest=args.dest, src=args.src)
|
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 = """
|
result = """
|
||||||
[{src}] {original}
|
[{src}] {original}
|
||||||
->
|
->
|
||||||
[{dest}] {text}
|
[{dest}] {text}
|
||||||
[pron.] {pronunciation}
|
[pron.] {pronunciation}
|
||||||
""".strip().format(src=result.src, dest=result.dest, original=result.origin,
|
""".strip().format(src=result.src, dest=result.dest, original=result.origin,
|
||||||
text=result.text.encode('utf-8', 'ignore'),
|
text=text,
|
||||||
pronunciation=result.pronunciation.encode('utf-8',
|
pronunciation=pronunciation)
|
||||||
'ignore'))
|
|
||||||
print(result)
|
print(result)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user