feat: Can translate from en to zh_TW
This commit is contained in:
parent
218b6b31c7
commit
214d710ca6
@ -60,10 +60,3 @@ class GoogleTranslator():
|
|||||||
data = eval(data[0][2])
|
data = eval(data[0][2])
|
||||||
return data[1][0][0][5][0][0]
|
return data[1][0][0][5][0][0]
|
||||||
|
|
||||||
translator = GoogleTranslator("en", 'zh_TW')
|
|
||||||
|
|
||||||
n = ""
|
|
||||||
while n!="!exit":
|
|
||||||
n = input("English: ")
|
|
||||||
if n!="" or n!="!exit":
|
|
||||||
print(translator.translate(n))
|
|
||||||
4
main.py
Normal file
4
main.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from tui import TranslatorApp
|
||||||
|
|
||||||
|
app = TranslatorApp()
|
||||||
|
app.run()
|
||||||
103
tui.py
103
tui.py
@ -1,30 +1,66 @@
|
|||||||
import npyscreen
|
import npyscreen
|
||||||
import curses
|
import curses
|
||||||
|
from GoogleTranslator import GoogleTranslator
|
||||||
|
|
||||||
class MyTestApp(npyscreen.NPSAppManaged):
|
class TranslatorApp(npyscreen.NPSAppManaged):
|
||||||
def onStart(self):
|
def onStart(self):
|
||||||
|
npyscreen.setTheme(npyscreen.Themes.ColorfulTheme)
|
||||||
self.addForm("MAIN", MainForm, name="Google Translator - TUI")
|
self.addForm("MAIN", MainForm, name="Google Translator - TUI")
|
||||||
|
|
||||||
class TestBox(npyscreen.BoxTitle):
|
class Box(npyscreen.BoxTitle):
|
||||||
_contained_widget = npyscreen.Textfield
|
_contained_widget = npyscreen.MultiLineEdit
|
||||||
|
|
||||||
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 = {
|
||||||
# ALT + ENTER -> send request
|
# send request
|
||||||
curses.ascii.alt(curses.ascii.NL): self.send_text
|
curses.ascii.alt(curses.ascii.NL): self.send_text,
|
||||||
|
|
||||||
|
#exit
|
||||||
|
"^Q": self.exit_app,
|
||||||
|
|
||||||
|
# delete all input
|
||||||
|
"^D": self.remove_text,
|
||||||
}
|
}
|
||||||
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(TestBox, name="Input (from)", footer="Footer",
|
self.input = self.add(Box, name="Input (from)", footer=self.translator.fr,
|
||||||
max_width=x//2-3, max_height=y-5, relx=3, rely=3, value="{} {}".format(x, y))
|
max_width=x//2-5, max_height=y//3,
|
||||||
|
relx=3, rely=3,
|
||||||
|
value="Hello world"
|
||||||
|
)
|
||||||
|
|
||||||
self.output = self.add(TestBox, name="Output (to)", footer="Footer",
|
self.output = self.add(Box, name="Output (to)", footer=self.translator.to,
|
||||||
max_width=x//2-3, max_height=y-5, relx=x//2+1, rely=3, editable=False)
|
max_width=x//2-5, max_height=y//3,
|
||||||
|
relx=x//2+2, rely=3,
|
||||||
|
value="你好,世界", editable=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.readme = self.add(Box, name="README",
|
||||||
|
max_width=x-5, max_height=y//3*2-6,
|
||||||
|
relx=3, rely=y//3+4,
|
||||||
|
value='''
|
||||||
|
█▀▀ █▀█ █▀█ █▀▀ █░░ █▀▀ ▄▄ ▀█▀ █▀█ ▄▀█ █▄░█ █▀ █░░ ▄▀█ ▀█▀ █▀▀ ▀█▀ █░█ █
|
||||||
|
█▄█ █▄█ █▄█ █▄█ █▄▄ ██▄ ░░ ░█░ █▀▄ █▀█ █░▀█ ▄█ █▄▄ █▀█ ░█░ ██▄ ░█░ █▄█ █
|
||||||
|
|
||||||
|
This is an unofficial Google Translate client.
|
||||||
|
It use Google Translate's API(free), so it may not work when you send too many requests.
|
||||||
|
|
||||||
|
It just a practice for npyscreen, this respository may not update any more.
|
||||||
|
|
||||||
|
- ^Q : quit
|
||||||
|
- ALT + ENTER : search
|
||||||
|
- CTRL + D : delete all input
|
||||||
|
''',
|
||||||
|
editable=False
|
||||||
|
)
|
||||||
|
|
||||||
def resize(self):
|
def resize(self):
|
||||||
'''
|
'''
|
||||||
@ -35,25 +71,46 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
y, x = self.useable_space()
|
y, x = self.useable_space()
|
||||||
|
|
||||||
# change input's size
|
# change input's size
|
||||||
self.input.max_width = x//2-3
|
self.input.max_width = x//2-5
|
||||||
self.input.max_height = y//2
|
self.input.max_height = y//3
|
||||||
|
|
||||||
# change output's size & location
|
# change output's size & location
|
||||||
self.output.max_width = x//2-3
|
self.output.max_width = x//2-5
|
||||||
self.output.max_height = y//2
|
self.output.max_height = y//3
|
||||||
self.output.relx = x//2+1 # location(x)
|
|
||||||
|
|
||||||
# Output's contained widget, TextField must be move to a new position
|
self.output.relx = x//2+2 # location(x)
|
||||||
self.output.entry_widget.relx = x//2+2
|
|
||||||
|
# change README's size & location
|
||||||
|
self.readme.max_width = x-5
|
||||||
|
self.readme.max_height = y//3*2-6
|
||||||
|
self.readme.rely = y//3+4
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
def send_text(self, event):
|
def send_text(self, event):
|
||||||
|
'''
|
||||||
|
use self.translator to send the request to Google Translate
|
||||||
|
then update the response to self.output
|
||||||
|
'''
|
||||||
# When press ALT + ENTER, send request & update output's text
|
# When press ALT + ENTER, send request & update output's text
|
||||||
self.output.entry_widget.value = self.input.value
|
if self.input.value != "":
|
||||||
self.output.entry_widget.display()
|
try:
|
||||||
|
targetText = self.translator.translate(self.input.value)
|
||||||
|
except:
|
||||||
|
targetText = "This is not a true translation, there exist an error."
|
||||||
|
finally:
|
||||||
|
self.output.value = targetText
|
||||||
|
|
||||||
def main():
|
# refresh entire form
|
||||||
TA = MyTestApp()
|
# Though npyscreen's documentation mention that we should avoid using DISPLAY() function
|
||||||
TA.run()
|
# I can't display Chinese or Japanese,etc correctly when I didn't use this function.
|
||||||
|
self.DISPLAY()
|
||||||
|
|
||||||
|
def remove_text(self, event):
|
||||||
|
self.input.value = ""
|
||||||
|
self.input.update()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def exit_app(self, event):
|
||||||
main()
|
exit(0)
|
||||||
Loading…
Reference in New Issue
Block a user