versi 0.1.1 translate from file and save result to file

This commit is contained in:
Nestero 2022-01-05 14:10:36 +07:00
parent 22b9c7f804
commit d6eba3b84b
3 changed files with 33 additions and 4 deletions

View File

@ -10,7 +10,7 @@ pip install pylingva
## CLI
```shell
$ translate -h
usage: translate [-h] [-s SOURCE] [-t TARGET] [-txt TEXT] [-ll]
usage: translate [-h] [-s SOURCE] [-t TARGET] [-txt TEXT] [-ll] [-f FILE] [-o OUTPUT]
options:
-h, --help show this help message and exit
@ -22,9 +22,18 @@ options:
Text to translate
-ll, --list-languages
List Languages support
-f FILE, --file FILE Path file .txt to translate
-o OUTPUT, --output OUTPUT
Output file translation result
$ translate -s auto -t id -txt "How are You ?"
Apa kabar ?
$ translate -s auto -t id -txt "How are You ?" -o output.txt
Translation result saved in output.txt
$ translate -s auto -t id -f input.txt -o output.txt
Translation result saved in output.txt
```
## Python
### List languages

View File

@ -4,14 +4,17 @@ from pylingva import pylingva
import argparse
def translate():
translate = pylingva()
arg = argparse.ArgumentParser()
arg.add_argument("-s", "--source", type=str, help="Source Language to translate")
arg.add_argument("-t", "--target", type=str, help="Target Language to translate")
arg.add_argument("-txt", "--text", type=str, help="Text to translate")
arg.add_argument("-ll", "--list-languages", help="List Languages support", action="store_true")
arg.add_argument("-f", "--file", help="Path file .txt to translate")
arg.add_argument("-o", "--output", help="Output file translation result")
args = arg.parse_args()
translate = pylingva()
if args.list_languages:
lang = translate.languages()
print("{:<25} {:<25}".format('Name', 'Code'))
@ -19,6 +22,23 @@ def translate():
x = key
y = value
print("{:<25} {:<25}".format(x, y))
elif args.file != None and args.output == None:
with open(args.file, "r") as f:
t = f.read()
result = translate.translate(args.source, args.target, t)
print(result)
elif args.output != None and args.file == None:
result = translate.translate(args.source, args.target, args.text)
with open(args.output, "w") as o:
o.write(result)
print("Translation result saved in", args.output)
elif args.file != None and args.output != None:
with open(args.file, "r") as f:
t = f.read()
result = translate.translate(args.source, args.target, t)
with open(args.output, "w") as o:
o.write(result)
print("Translation result saved in", args.output)
else:
result = translate.translate(args.source, args.target, args.text)
print(result)

View File

@ -7,7 +7,7 @@ with open("README.md", "r") as des:
setup(
name='pylingva',
packages=find_packages(),
version='0.1.0',
version='0.1.1',
entry_points={'console_scripts': ['translate = pylingva.cli:translate']},
description='Simple translator using Lingva Translate API',
url='https://gitlab.com/nesstero/pylingva',