This is a sequel to I made a CUI-based translation script. If you haven't read it, please read it!
Allows you to optionally specify the language to translate from the command line. To ensure portability, except for googletrans Implement without using an external module.
The previous implementation content is omitted.
opt = None
text = None
try:
opt = sys.argv[1]
text = " ".join(sys.argv[2:]) #Combine English sentences of multiple words into one sentence (separate writing))
except:
logger.critical('The format is incorrect.')
exit()
opt = opt.strip("-") #Attach to option'-'(Turn off the hyphen)
print(convert(text=text, lang=opt)) #Pass the convert function lang to opt.
try:
opt = sys.argv[1]
text = " ".join(sys.argv[2:])
except:
logger.critical('The format is incorrect.')
exit()
Use try ~ except
so that it can be terminated normally even if there is an error.
Also, since the divided English sentences are elements of separate lists, use str.join ()
to make one sentence and send it to convert ()
.
In this article, I didn't use an external module, but you may implement it using click etc.
Recommended Posts