feature: Add choose language Form

This commit is contained in:
Ting-Jun Wang 2021-04-24 21:32:07 +08:00
parent d9f9f03454
commit 142e32bd6e
Signed by: snsd0805
GPG Key ID: 8DB0D22BC1217D33
7 changed files with 90 additions and 8 deletions

View File

@ -8,6 +8,8 @@ class GoogleTranslator():
self.languageCode = {}
self.fr = fr
self.to = to
self.inputLanguage = ""
self.outputLanguage = ""
self.loadLanguageCode()
@ -38,6 +40,8 @@ class GoogleTranslator():
for line in data:
country, code = line.split(',')
self.languageCode[country] = code
if code==self.fr: self.inputLanguage = country
if code==self.to: self.outputLanguage = country
def translate(self, text):
'''

View File

@ -1,4 +1,7 @@
import npyscreen
class EditBox(npyscreen.BoxTitle):
_contained_widget = npyscreen.MultiLineEdit
_contained_widget = npyscreen.MultiLineEdit
class SelectBox(npyscreen.BoxTitle):
_contained_widget = npyscreen.SelectOne

54
TUI/LanguageForm.py Normal file
View File

@ -0,0 +1,54 @@
import npyscreen
import curses
from TUI.Box import EditBox, SelectBox
from config import config
class LanguageForm(npyscreen.ActionForm):
def create(self):
# get language list
languages = list(self.parentApp.translator.languageCode.keys())
codes = list(self.parentApp.translator.languageCode.values())
# get terminal's size
y, x = self.useable_space()
inputDefault = languages.index(self.parentApp.translator.inputLanguage)
outputDefault = languages.index(self.parentApp.translator.outputLanguage)
self.input = self.add(SelectBox, name="Input Language",
value=inputDefault,
values=languages,
max_height=y-5, max_width = x//2-5,
relx=3, rely=3,
)
self.output = self.add(SelectBox, name="Output Language",
value=outputDefault,
values=languages,
max_height=y-5, max_width = x//2-5,
relx=x//2+3, rely=3,
)
def resize(self):
# get terminal's size
y, x = self.useable_space()
self.input.max_height = y-5
self.input_max_width = x//2-5
self.output.max_height = y-5
self.output.max_width = x//2-5
self.output.relx = x//2+3
self.output.entry_widget.relx = x//2+4
def on_cancel(self):
self.parentApp.switchFormPrevious()
def on_ok(self):
languages = list(self.parentApp.translator.languageCode.keys())
codes = list(self.parentApp.translator.languageCode.values())
self.parentApp.translator.fr = codes[self.input.value[0]]
self.parentApp.translator.to = codes[self.output.value[0]]
self.parentApp.translator.inputLanguage = languages[self.input.value[0]]
self.parentApp.translator.outputLanguage = languages[self.output.value[0]]
self.parentApp.setNextForm("MAIN")

View File

@ -1,12 +1,9 @@
import npyscreen
import curses
from TUI.Box import EditBox
from GoogleTranslator import GoogleTranslator
class MainForm(npyscreen.FormBaseNew):
def create(self):
# set translator
self.translator = GoogleTranslator('en', "zh_TW")
# set event handler
event_handlers = {
@ -18,19 +15,22 @@ class MainForm(npyscreen.FormBaseNew):
# delete all input
"^D": self.remove_text,
# select language
"^S": self.change_language,
}
self.add_handlers(event_handlers)
# get terminal's size
y, x = self.useable_space()
self.input = self.add(EditBox, name="Input (from)", footer=self.translator.fr,
self.input = self.add(EditBox, name="Input (from)", footer=self.parentApp.translator.inputLanguage,
max_width=x//2-5, max_height=y//3,
relx=3, rely=3,
value="Hello world"
)
self.output = self.add(EditBox, name="Output (to)", footer=self.translator.to,
self.output = self.add(EditBox, name="Output (to)", footer=self.parentApp.translator.outputLanguage,
max_width=x//2-5, max_height=y//3,
relx=x//2+2, rely=3,
value="你好,世界",
@ -82,6 +82,9 @@ class MainForm(npyscreen.FormBaseNew):
# Output and README's contained widget, TextField must be move to a new position
self.output.entry_widget.relx = x//2+3
self.readme.entry_widget.rely = y//3+5
self.input.footer = self.parentApp.translator.inputLanguage
self.output.footer = self.parentApp.translator.outputLanguage
def send_text(self, event):
'''
@ -91,7 +94,7 @@ class MainForm(npyscreen.FormBaseNew):
# When press ALT + ENTER, send request & update output's text
if self.input.value != "":
try:
targetText = self.translator.translate(self.input.value.replace('\n', ' '))
targetText = self.parentApp.translator.translate(self.input.value.replace('\n', ' '))
except:
targetText = ["This is not a true translation, there exist an error."]
@ -111,4 +114,7 @@ class MainForm(npyscreen.FormBaseNew):
self.input.update()
def exit_app(self, event):
exit(0)
exit(0)
def change_language(self, event):
self.parentApp.switchForm("LANGUAGE")

View File

@ -1,7 +1,14 @@
import npyscreen
from TUI.MainForm import MainForm
from TUI.LanguageForm import LanguageForm
from GoogleTranslator import GoogleTranslator
from config import config
class TranslatorApp(npyscreen.NPSAppManaged):
translator = GoogleTranslator(config['inputLanguage'], config['outputLanguage'])
def onStart(self):
npyscreen.setTheme(npyscreen.Themes.ColorfulTheme)
self.addForm("MAIN", MainForm, name="Google Translator - TUI")
self.addForm("LANGUAGE", LanguageForm, name="LANGUAGE CHOOSE")

4
config.json Normal file
View File

@ -0,0 +1,4 @@
{
"inputLanguage": "en",
"outputLanguage": "zh-TW"
}

4
config.py Normal file
View File

@ -0,0 +1,4 @@
import json
with open('config.json') as fp:
config = json.load(fp)