[PYTHON] I made a CUI-based translation script (2)

Introduction

This is a sequel to I made a CUI-based translation script. If you haven't read it, please read it!


What to implement this time

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.

Implementation details

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.

Description

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 ().

Summary

In this article, I didn't use an external module, but you may implement it using click etc.

Recommended Posts

I made a CUI-based translation script (2)
I made a CUI-based translation script
I made a script to display emoji
〇✕ I made a game
I made a python text
I made a discord bot
I made a script to put a snippet in README.md
I made a C ++ learning site
I made a Line-bot using Python!
I made a wikipedia gacha bot
I made a fortune with Python.
I made a daemon with Python
I made a script to say hello at my Koshien
I installed Taiga.IO on CentOS7 (I made a script while I was there)
I made a new AWS S3 bucket
I made a dash docset for Holoviews
I made a payroll program in Python!
I touched "Orator" so I made a note
I made a character counter with Python
Beginner: I made a launcher using dictionary
I made a conversation partner like Siri
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a browser automatic stamping tool.
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a WEB application with Django
I made a neuron simulator with Python
I made a script in python to convert .md files to Scrapbox format
I made a stamp substitute bot with line
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
I made a crazy thing called typed tuple
[Python] I made a Youtube Downloader with Tkinter.
I made a router config collection tool Config Collecor
I made a simple Bitcoin wallet with pycoin
I made a downloader for word distributed expression
I made a LINE Bot with Serverless Framework!
I made a tool to compile Hy natively
I wrote a script to upload a WordPress plugin
I made a tool to get new articles
I made a random number graph with Numpy
I made a peeping prevention product for telework.
I made a simple RSS reader ~ C edition ~
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I made a script to record the active window using win32gui of Python
[AWS] I made a reminder BOT with LINE WORKS
I made a Twitter BOT with GAE (python) (with a reference)
I made a prime number generation program in Python