diff --git a/GoogleTranslator.py b/GoogleTranslator.py index 4586271..f9d6b23 100644 --- a/GoogleTranslator.py +++ b/GoogleTranslator.py @@ -5,9 +5,11 @@ from urllib.parse import quote class GoogleTranslator(): def __init__(self, fr, to): # set up + self.languageCode = {} self.fr = fr self.to = to - print("from {} to {}".format(fr, to)) + + self.loadLanguageCode() self.URL = "https://translate.google.com.tw/_/TranslateWebserverUi/data/batchexecute?\ rpcids=MkEWBc&\ @@ -28,6 +30,15 @@ class GoogleTranslator(): # 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&" + 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): ''' return a string which translate from self.fr to self.to diff --git a/LanguageCode.csv b/LanguageCode.csv new file mode 100644 index 0000000..775c33d --- /dev/null +++ b/LanguageCode.csv @@ -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 \ No newline at end of file diff --git a/TUI/Box.py b/TUI/Box.py new file mode 100644 index 0000000..86867e4 --- /dev/null +++ b/TUI/Box.py @@ -0,0 +1,4 @@ +import npyscreen + +class EditBox(npyscreen.BoxTitle): + _contained_widget = npyscreen.MultiLineEdit \ No newline at end of file diff --git a/tui.py b/TUI/MainForm.py similarity index 86% rename from tui.py rename to TUI/MainForm.py index 8559ad4..c275f47 100644 --- a/tui.py +++ b/TUI/MainForm.py @@ -1,15 +1,8 @@ import npyscreen import curses +from TUI.Box import EditBox 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): def create(self): # set translator @@ -31,19 +24,20 @@ class MainForm(npyscreen.FormBaseNew): # get terminal's size 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, relx=3, rely=3, 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, 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, relx=3, rely=y//3+4, value=''' diff --git a/TUI/TranslatorApp.py b/TUI/TranslatorApp.py new file mode 100644 index 0000000..3f76bfa --- /dev/null +++ b/TUI/TranslatorApp.py @@ -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") diff --git a/main.py b/main.py index 2679d8c..711d78d 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from tui import TranslatorApp +from TUI.TranslatorApp import TranslatorApp app = TranslatorApp() app.run() \ No newline at end of file