Skip to content

Commit fd7bdd5

Browse files
wip
1 parent a7f149e commit fd7bdd5

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ __pycache__
55
/.vscode
66
.env.local
77
debug.log
8+
data/configs.json

core/CAIAssistant.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class CAIAssistant:
99
def __init__(self, promptsFolder=None, openai_api_key=None):
1010
if promptsFolder is None:
11-
promptsFolder = os.path.join(os.path.dirname(__file__), '../prompts')
11+
promptsFolder = os.path.join(os.path.dirname(__file__), '../data')
1212
self._promptsFolder = promptsFolder
1313
self.bindAPI(openai_api_key=openai_api_key)
1414
return
@@ -41,6 +41,7 @@ def bindAPI(self, openai_api_key):
4141
),
4242
]),
4343
)
44+
self._connected = True
4445
except Exception as e:
4546
logging.error('Failed to bind API: ' + str(e))
4647
return

core/worker.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,9 @@ def _updateLocalization(self, languageName, languageCode):
103103
res = [x for x in res if len(x) > 0]
104104
assert len(strings) == len(res)
105105
res = {k: v for k, v in zip(strings, res)}
106-
return res
106+
return res
107+
108+
def bindAPI(self, key):
109+
self._assistant.bindAPI(key)
110+
return
111+

data/languages.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"sk": "Slovak",
3+
"en": "English",
4+
"es": "Spanish",
5+
"fr": "French",
6+
"de": "German",
7+
"it": "Italian",
8+
"pt": "Portuguese",
9+
"nl": "Dutch",
10+
"sv": "Swedish",
11+
"no": "Norwegian",
12+
"da": "Danish",
13+
"fi": "Finnish",
14+
"pl": "Polish",
15+
"hu": "Hungarian",
16+
"ru": "Russian",
17+
"ar": "Arabic",
18+
"ja": "Japanese",
19+
"zh": "Chinese",
20+
"ko": "Korean",
21+
"ua": "Ukrainian",
22+
"tr": "Turkish"
23+
}
File renamed without changes.
File renamed without changes.

main.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from tkinter import ttk
55
from tkinter import simpledialog
66
import tkinter.scrolledtext as tkst
7+
import json
78
from core.worker import CWorker
89
# set up logging
910
import logging
@@ -21,16 +22,17 @@
2122
# TODO: add translation history
2223
# TODO: add simple translation diff
2324
class App(tk.Frame):
24-
def __init__(self, master, languages, currentLanguage=None):
25+
def __init__(self, master, languages, configs):
2526
super().__init__(master)
27+
self._configs = configs
2628
self._localizationMap = {}
2729
# predefine messages
2830
self._localization('Processing...')
2931
self._localization('Translation is not accurate and will be updated soon.')
3032
# set up UI
3133
self._master = master
3234
self._languages = languages
33-
self._currentLanguage = currentLanguage or self._languages.keys()[0]
35+
self._currentLanguage = self._configs.get('language') or 'en'
3436
# set global font
3537
self._master.option_add("*Font", ("Arial", 14))
3638
self._master.title("AI Enhanced Translator")
@@ -194,6 +196,7 @@ def onSelectLanguage(self, event):
194196
if code is None: return
195197

196198
self._currentLanguage = code
199+
self._configs['language'] = code
197200
self._worker.forceTranslate() # hack to force translation
198201
return
199202

@@ -211,18 +214,27 @@ def onSwitchAPIKey(self):
211214
parent=self._master
212215
)
213216
if newKey is None: return
214-
# self._worker.switchAPIKey(newKey)
217+
self._worker.bindAPI(newKey)
215218
return
216219

220+
def configs(self): return self._configs
221+
# End of class
222+
223+
def main():
224+
# load languages from data/languages.json
225+
with open('data/languages.json', 'r') as f: languages = json.load(f)
226+
# load configs
227+
configs = {}
228+
try:
229+
with open('data/configs.json', 'r') as f: configs = json.load(f)
230+
except: pass
231+
232+
app = App(master=tk.Tk(), languages=languages, configs=configs)
233+
app.mainloop()
234+
# save configs on exit
235+
configs = app.configs()
236+
with open('data/configs.json', 'w') as f: json.dump(configs, f, indent=2)
237+
return
238+
217239
if '__main__' == __name__:
218-
app = App(
219-
master=tk.Tk(),
220-
# TODO: load languages from config
221-
languages={
222-
'sk': 'Slovak',
223-
'en': 'English',
224-
'de': 'German',
225-
},
226-
currentLanguage='sk'
227-
)
228-
app.mainloop()
240+
main()

0 commit comments

Comments
 (0)