Created
March 26, 2022 08:01
-
-
Save evrial/609a37c56f3d9f1e3109412286238ac7 to your computer and use it in GitHub Desktop.
Google translate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Users/evrial/anaconda3/envs/base/bin/python3.7 | |
# -*- coding: utf-8 -*- | |
import argparse | |
from deep_translator import GoogleTranslator | |
from deep_translator.exceptions import LanguageNotSupportedException | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Python Google Translator as a command-line tool') | |
parser.add_argument('text', help='The text you want to translate.') | |
parser.add_argument('-t', '--target', default='en', | |
help='The target language you want to translate. (Default: en)') | |
parser.add_argument('-s', '--source', default='auto', | |
help='The source language you want to translate. (Default: auto)') | |
args = parser.parse_args() | |
try: | |
translator = GoogleTranslator(source=args.source, target=args.target) | |
except LanguageNotSupportedException as e: | |
langs_list = GoogleTranslator.get_supported_languages(as_dict=True) | |
print(langs_list) | |
raise | |
result = translator.translate(args.text) | |
print(result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment