feature: Add choose language Form
This commit is contained in:
parent
d9f9f03454
commit
142e32bd6e
@ -8,6 +8,8 @@ class GoogleTranslator():
|
|||||||
self.languageCode = {}
|
self.languageCode = {}
|
||||||
self.fr = fr
|
self.fr = fr
|
||||||
self.to = to
|
self.to = to
|
||||||
|
self.inputLanguage = ""
|
||||||
|
self.outputLanguage = ""
|
||||||
|
|
||||||
self.loadLanguageCode()
|
self.loadLanguageCode()
|
||||||
|
|
||||||
@ -38,6 +40,8 @@ class GoogleTranslator():
|
|||||||
for line in data:
|
for line in data:
|
||||||
country, code = line.split(',')
|
country, code = line.split(',')
|
||||||
self.languageCode[country] = code
|
self.languageCode[country] = code
|
||||||
|
if code==self.fr: self.inputLanguage = country
|
||||||
|
if code==self.to: self.outputLanguage = country
|
||||||
|
|
||||||
def translate(self, text):
|
def translate(self, text):
|
||||||
'''
|
'''
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
import npyscreen
|
import npyscreen
|
||||||
|
|
||||||
class EditBox(npyscreen.BoxTitle):
|
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
54
TUI/LanguageForm.py
Normal 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")
|
||||||
@ -1,12 +1,9 @@
|
|||||||
import npyscreen
|
import npyscreen
|
||||||
import curses
|
import curses
|
||||||
from TUI.Box import EditBox
|
from TUI.Box import EditBox
|
||||||
from GoogleTranslator import GoogleTranslator
|
|
||||||
|
|
||||||
class MainForm(npyscreen.FormBaseNew):
|
class MainForm(npyscreen.FormBaseNew):
|
||||||
def create(self):
|
def create(self):
|
||||||
# set translator
|
|
||||||
self.translator = GoogleTranslator('en', "zh_TW")
|
|
||||||
|
|
||||||
# set event handler
|
# set event handler
|
||||||
event_handlers = {
|
event_handlers = {
|
||||||
@ -18,19 +15,22 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
|
|
||||||
# delete all input
|
# delete all input
|
||||||
"^D": self.remove_text,
|
"^D": self.remove_text,
|
||||||
|
|
||||||
|
# select language
|
||||||
|
"^S": self.change_language,
|
||||||
}
|
}
|
||||||
self.add_handlers(event_handlers)
|
self.add_handlers(event_handlers)
|
||||||
|
|
||||||
# get terminal's size
|
# get terminal's size
|
||||||
y, x = self.useable_space()
|
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,
|
max_width=x//2-5, max_height=y//3,
|
||||||
relx=3, rely=3,
|
relx=3, rely=3,
|
||||||
value="Hello world"
|
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,
|
max_width=x//2-5, max_height=y//3,
|
||||||
relx=x//2+2, rely=3,
|
relx=x//2+2, rely=3,
|
||||||
value="你好,世界",
|
value="你好,世界",
|
||||||
@ -82,6 +82,9 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
# Output and README's contained widget, TextField must be move to a new position
|
# Output and README's contained widget, TextField must be move to a new position
|
||||||
self.output.entry_widget.relx = x//2+3
|
self.output.entry_widget.relx = x//2+3
|
||||||
self.readme.entry_widget.rely = y//3+5
|
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):
|
def send_text(self, event):
|
||||||
'''
|
'''
|
||||||
@ -91,7 +94,7 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
# When press ALT + ENTER, send request & update output's text
|
# When press ALT + ENTER, send request & update output's text
|
||||||
if self.input.value != "":
|
if self.input.value != "":
|
||||||
try:
|
try:
|
||||||
targetText = self.translator.translate(self.input.value.replace('\n', ' '))
|
targetText = self.parentApp.translator.translate(self.input.value.replace('\n', ' '))
|
||||||
except:
|
except:
|
||||||
targetText = ["This is not a true translation, there exist an error."]
|
targetText = ["This is not a true translation, there exist an error."]
|
||||||
|
|
||||||
@ -111,4 +114,7 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
self.input.update()
|
self.input.update()
|
||||||
|
|
||||||
def exit_app(self, event):
|
def exit_app(self, event):
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
def change_language(self, event):
|
||||||
|
self.parentApp.switchForm("LANGUAGE")
|
||||||
|
|||||||
@ -1,7 +1,14 @@
|
|||||||
import npyscreen
|
import npyscreen
|
||||||
from TUI.MainForm import MainForm
|
from TUI.MainForm import MainForm
|
||||||
|
from TUI.LanguageForm import LanguageForm
|
||||||
|
from GoogleTranslator import GoogleTranslator
|
||||||
|
from config import config
|
||||||
|
|
||||||
class TranslatorApp(npyscreen.NPSAppManaged):
|
class TranslatorApp(npyscreen.NPSAppManaged):
|
||||||
|
|
||||||
|
translator = GoogleTranslator(config['inputLanguage'], config['outputLanguage'])
|
||||||
|
|
||||||
def onStart(self):
|
def onStart(self):
|
||||||
npyscreen.setTheme(npyscreen.Themes.ColorfulTheme)
|
npyscreen.setTheme(npyscreen.Themes.ColorfulTheme)
|
||||||
self.addForm("MAIN", MainForm, name="Google Translator - TUI")
|
self.addForm("MAIN", MainForm, name="Google Translator - TUI")
|
||||||
|
self.addForm("LANGUAGE", LanguageForm, name="LANGUAGE CHOOSE")
|
||||||
4
config.json
Normal file
4
config.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"inputLanguage": "en",
|
||||||
|
"outputLanguage": "zh-TW"
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user