feat: Change Directory Architecture & load LanguageCode
This commit is contained in:
parent
fb9261e8ef
commit
d9f9f03454
@ -5,9 +5,11 @@ from urllib.parse import quote
|
|||||||
class GoogleTranslator():
|
class GoogleTranslator():
|
||||||
def __init__(self, fr, to):
|
def __init__(self, fr, to):
|
||||||
# set up
|
# set up
|
||||||
|
self.languageCode = {}
|
||||||
self.fr = fr
|
self.fr = fr
|
||||||
self.to = to
|
self.to = to
|
||||||
print("from {} to {}".format(fr, to))
|
|
||||||
|
self.loadLanguageCode()
|
||||||
|
|
||||||
self.URL = "https://translate.google.com.tw/_/TranslateWebserverUi/data/batchexecute?\
|
self.URL = "https://translate.google.com.tw/_/TranslateWebserverUi/data/batchexecute?\
|
||||||
rpcids=MkEWBc&\
|
rpcids=MkEWBc&\
|
||||||
@ -28,6 +30,15 @@ class GoogleTranslator():
|
|||||||
# can't sure whether it works for everyone, token in this URL might been blocked.
|
# can't sure whether it works for everyone, token in this URL might been blocked.
|
||||||
self.DATA = "f.req=%5B%5B%5B%22MkEWBc%22%2C%22%5B%5B%5C%22{}%5C%22%2C%5C%22{}%5C%22%2C%5C%22{}%5C%22%2Ctrue%5D%2C%5Bnull%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D&at=AD08yZn6jdbpV8qLjfergSwRT4IO%3A1618543754261&"
|
self.DATA = "f.req=%5B%5B%5B%22MkEWBc%22%2C%22%5B%5B%5C%22{}%5C%22%2C%5C%22{}%5C%22%2C%5C%22{}%5C%22%2Ctrue%5D%2C%5Bnull%5D%5D%22%2Cnull%2C%22generic%22%5D%5D%5D&at=AD08yZn6jdbpV8qLjfergSwRT4IO%3A1618543754261&"
|
||||||
|
|
||||||
|
def loadLanguageCode(self):
|
||||||
|
with open('LanguageCode.csv') as fp:
|
||||||
|
data = fp.readlines()
|
||||||
|
data = [line.replace('\n', '') for line in data]
|
||||||
|
|
||||||
|
for line in data:
|
||||||
|
country, code = line.split(',')
|
||||||
|
self.languageCode[country] = code
|
||||||
|
|
||||||
def translate(self, text):
|
def translate(self, text):
|
||||||
'''
|
'''
|
||||||
return a string which translate from self.fr to self.to
|
return a string which translate from self.fr to self.to
|
||||||
|
|||||||
109
LanguageCode.csv
Normal file
109
LanguageCode.csv
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
Afrikaans,af
|
||||||
|
Albanian,sq
|
||||||
|
Amharic,am
|
||||||
|
Arabic,ar
|
||||||
|
Armenian,hy
|
||||||
|
Azerbaijani,az
|
||||||
|
Basque,eu
|
||||||
|
Belarusian,be
|
||||||
|
Bengali,bn
|
||||||
|
Bosnian,bs
|
||||||
|
Bulgarian,bg
|
||||||
|
Catalan,ca
|
||||||
|
Cebuano,ceb
|
||||||
|
Chinese (Simplified),zh-CN
|
||||||
|
Chinese (Traditional),zh-TW
|
||||||
|
Corsican,co
|
||||||
|
Croatian,hr
|
||||||
|
Czech,cs
|
||||||
|
Danish,da
|
||||||
|
Dutch,nl
|
||||||
|
English,en
|
||||||
|
Esperanto,eo
|
||||||
|
Estonian,et
|
||||||
|
Finnish,fi
|
||||||
|
French,fr
|
||||||
|
Frisian,fy
|
||||||
|
Galician,gl
|
||||||
|
Georgian,ka
|
||||||
|
German,de
|
||||||
|
Greek,el
|
||||||
|
Gujarati,gu
|
||||||
|
Haitian Creole,ht
|
||||||
|
Hausa,ha
|
||||||
|
Hawaiian,haw
|
||||||
|
Hebrew,he
|
||||||
|
Hindi,hi
|
||||||
|
Hmong,hmn
|
||||||
|
Hungarian,hu
|
||||||
|
Icelandic,is
|
||||||
|
Igbo,ig
|
||||||
|
Indonesian,id
|
||||||
|
Irish,ga
|
||||||
|
Italian,it
|
||||||
|
Japanese,ja
|
||||||
|
Javanese,jv
|
||||||
|
Kannada,kn
|
||||||
|
Kazakh,kk
|
||||||
|
Khmer,km
|
||||||
|
Kinyarwanda,rw
|
||||||
|
Korean,ko
|
||||||
|
Kurdish,ku
|
||||||
|
Kyrgyz,ky
|
||||||
|
Lao,lo
|
||||||
|
Latin,la
|
||||||
|
Latvian,lv
|
||||||
|
Lithuanian,lt
|
||||||
|
Luxembourgish,lb
|
||||||
|
Macedonian,mk
|
||||||
|
Malagasy,mg
|
||||||
|
Malay,ms
|
||||||
|
Malayalam,ml
|
||||||
|
Maltese,mt
|
||||||
|
Maori,mi
|
||||||
|
Marathi,mr
|
||||||
|
Mongolian,mn
|
||||||
|
Myanmar(Burmese),my
|
||||||
|
Nepali,ne
|
||||||
|
Norwegian,no
|
||||||
|
Nyanja(Chichewa),ny
|
||||||
|
Odia(Oriya),or
|
||||||
|
Pashto,ps
|
||||||
|
Persian,fa
|
||||||
|
Polish,pl
|
||||||
|
Portuguese(Portugal or Brazil),pt
|
||||||
|
Punjabi,pa
|
||||||
|
Romanian,ro
|
||||||
|
Russian,ru
|
||||||
|
Samoan,sm
|
||||||
|
Scots Gaelic,gd
|
||||||
|
Serbian,sr
|
||||||
|
Sesotho,st
|
||||||
|
Shona,sn
|
||||||
|
Sindhi,sd
|
||||||
|
Sinhala(Sinhalese),si
|
||||||
|
Slovak,sk
|
||||||
|
Slovenian,sl
|
||||||
|
Somali,so
|
||||||
|
Spanish,es
|
||||||
|
Sundanese,su
|
||||||
|
Swahili,sw
|
||||||
|
Swedish,sv
|
||||||
|
Tagalog(Filipino),tl
|
||||||
|
Tajik,tg
|
||||||
|
Tamil,ta
|
||||||
|
Tatar,tt
|
||||||
|
Telugu,te
|
||||||
|
Thai,th
|
||||||
|
Turkish,tr
|
||||||
|
Turkmen,tk
|
||||||
|
Ukrainian,uk
|
||||||
|
Urdu,ur
|
||||||
|
Uyghur,ug
|
||||||
|
Uzbek,uz
|
||||||
|
Vietnamese,vi
|
||||||
|
Welsh,cy
|
||||||
|
Xhosa,xh
|
||||||
|
Yiddish,yi
|
||||||
|
Yoruba,yo
|
||||||
|
Zulu,zu
|
||||||
|
4
TUI/Box.py
Normal file
4
TUI/Box.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import npyscreen
|
||||||
|
|
||||||
|
class EditBox(npyscreen.BoxTitle):
|
||||||
|
_contained_widget = npyscreen.MultiLineEdit
|
||||||
@ -1,15 +1,8 @@
|
|||||||
import npyscreen
|
import npyscreen
|
||||||
import curses
|
import curses
|
||||||
|
from TUI.Box import EditBox
|
||||||
from GoogleTranslator import GoogleTranslator
|
from GoogleTranslator import GoogleTranslator
|
||||||
|
|
||||||
class TranslatorApp(npyscreen.NPSAppManaged):
|
|
||||||
def onStart(self):
|
|
||||||
npyscreen.setTheme(npyscreen.Themes.ColorfulTheme)
|
|
||||||
self.addForm("MAIN", MainForm, name="Google Translator - TUI")
|
|
||||||
|
|
||||||
class Box(npyscreen.BoxTitle):
|
|
||||||
_contained_widget = npyscreen.MultiLineEdit
|
|
||||||
|
|
||||||
class MainForm(npyscreen.FormBaseNew):
|
class MainForm(npyscreen.FormBaseNew):
|
||||||
def create(self):
|
def create(self):
|
||||||
# set translator
|
# set translator
|
||||||
@ -31,19 +24,20 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
# get terminal's size
|
# get terminal's size
|
||||||
y, x = self.useable_space()
|
y, x = self.useable_space()
|
||||||
|
|
||||||
self.input = self.add(Box, name="Input (from)", footer=self.translator.fr,
|
self.input = self.add(EditBox, name="Input (from)", footer=self.translator.fr,
|
||||||
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(Box, name="Output (to)", footer=self.translator.to,
|
self.output = self.add(EditBox, name="Output (to)", footer=self.translator.to,
|
||||||
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="你好,世界", editable=False
|
value="你好,世界",
|
||||||
|
editable=False
|
||||||
)
|
)
|
||||||
|
|
||||||
self.readme = self.add(Box, name="README",
|
self.readme = self.add(EditBox, name="README",
|
||||||
max_width=x-5, max_height=y//3*2-6,
|
max_width=x-5, max_height=y//3*2-6,
|
||||||
relx=3, rely=y//3+4,
|
relx=3, rely=y//3+4,
|
||||||
value='''
|
value='''
|
||||||
7
TUI/TranslatorApp.py
Normal file
7
TUI/TranslatorApp.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import npyscreen
|
||||||
|
from TUI.MainForm import MainForm
|
||||||
|
|
||||||
|
class TranslatorApp(npyscreen.NPSAppManaged):
|
||||||
|
def onStart(self):
|
||||||
|
npyscreen.setTheme(npyscreen.Themes.ColorfulTheme)
|
||||||
|
self.addForm("MAIN", MainForm, name="Google Translator - TUI")
|
||||||
Loading…
Reference in New Issue
Block a user