-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1966 from SirRomey/master
Python: Translate String
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import argparse | ||
from translate import Translator | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--from_lang", help="Language to translate from.", type=str) | ||
parser.add_argument( | ||
"--to_lang", help="Language to translate to. (defaults to English)", | ||
type=str) | ||
text = input("Enter text to translate: ") | ||
args = parser.parse_args() | ||
if args.from_lang: | ||
translator = Translator( | ||
to_lang=args.to_lang if args.to_lang else "English", | ||
from_lang=args.from_lang | ||
) | ||
else: | ||
translator = Translator( | ||
to_lang=args.to_lang if args.to_lang else "English") | ||
try: | ||
translation = translator.translate(text) | ||
except Exception: | ||
print("Translation Error. Returning...") | ||
return None | ||
print(translation) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |