From 142e32bd6ec851d2544a9d9ba7c39510dc41a62d Mon Sep 17 00:00:00 2001 From: Ting-Jun Wang Date: Sat, 24 Apr 2021 21:32:07 +0800 Subject: [PATCH] feature: Add choose language Form --- GoogleTranslator.py | 4 ++++ TUI/Box.py | 5 +++- TUI/LanguageForm.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ TUI/MainForm.py | 20 ++++++++++------ TUI/TranslatorApp.py | 7 ++++++ config.json | 4 ++++ config.py | 4 ++++ 7 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 TUI/LanguageForm.py create mode 100644 config.json create mode 100644 config.py diff --git a/GoogleTranslator.py b/GoogleTranslator.py index f9d6b23..1d5ce45 100644 --- a/GoogleTranslator.py +++ b/GoogleTranslator.py @@ -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): ''' diff --git a/TUI/Box.py b/TUI/Box.py index 86867e4..f9c687a 100644 --- a/TUI/Box.py +++ b/TUI/Box.py @@ -1,4 +1,7 @@ import npyscreen class EditBox(npyscreen.BoxTitle): - _contained_widget = npyscreen.MultiLineEdit \ No newline at end of file + _contained_widget = npyscreen.MultiLineEdit + +class SelectBox(npyscreen.BoxTitle): + _contained_widget = npyscreen.SelectOne \ No newline at end of file diff --git a/TUI/LanguageForm.py b/TUI/LanguageForm.py new file mode 100644 index 0000000..88acbe8 --- /dev/null +++ b/TUI/LanguageForm.py @@ -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") \ No newline at end of file diff --git a/TUI/MainForm.py b/TUI/MainForm.py index c275f47..cd5cb5a 100644 --- a/TUI/MainForm.py +++ b/TUI/MainForm.py @@ -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) \ No newline at end of file + exit(0) + + def change_language(self, event): + self.parentApp.switchForm("LANGUAGE") diff --git a/TUI/TranslatorApp.py b/TUI/TranslatorApp.py index 3f76bfa..ab18aa1 100644 --- a/TUI/TranslatorApp.py +++ b/TUI/TranslatorApp.py @@ -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") \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..0acfc7f --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "inputLanguage": "en", + "outputLanguage": "zh-TW" +} \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..a0526b6 --- /dev/null +++ b/config.py @@ -0,0 +1,4 @@ +import json + +with open('config.json') as fp: + config = json.load(fp)