22
22
class App (tk .Frame ):
23
23
def __init__ (self , master , languages , currentLanguage = None ):
24
24
super ().__init__ (master )
25
+ self ._localizationMap = {}
26
+ # predefine messages
27
+ self ._localization ('Processing...' )
28
+ self ._localization ('Translation is not accurate and will be updated soon.' )
29
+ # set up UI
25
30
self ._master = master
26
31
self ._languages = languages
27
32
self ._currentLanguage = currentLanguage or self ._languages .keys ()[0 ]
@@ -34,9 +39,18 @@ def __init__(self, master, languages, currentLanguage=None):
34
39
self ._worker = CWorker (self )
35
40
self ._worker .start ()
36
41
return
42
+
43
+ def _localization (self , text ):
44
+ res = self ._localizationMap .get (text )
45
+ if res is None :
46
+ self ._localizationMap [text ] = res = tk .StringVar (value = text )
47
+ return res
37
48
38
49
def _UI_inputArea (self , owner ):
39
- label = tk .Label (owner , text = "Input Text:" , justify = "left" , anchor = "w" )
50
+ label = tk .Label (
51
+ owner , justify = "left" , anchor = "w" ,
52
+ textvariable = self ._localization ("Input Text:" )
53
+ )
40
54
label .pack (side = "top" , fill = tk .X )
41
55
42
56
self ._inputText = tkst .ScrolledText (owner )
@@ -61,22 +75,28 @@ def _UI_languageSelection(self, owner):
61
75
self ._language .pack (side = "top" , anchor = "ne" , padx = 5 , pady = 5 )
62
76
self ._language .bind ("<<ComboboxSelected>>" , self .onSelectLanguage )
63
77
try :
64
- self ._language .set ('Slovak' )
78
+ self ._language .set (self . _languages [ self . _currentLanguage ] )
65
79
except tk .TclError :
66
80
pass
67
81
return
68
82
69
83
def _UI_outputArea (self , owner ):
70
84
self ._UI_languageSelection (owner )
71
85
# fast translation
72
- label = tk .Label (owner , text = self .UITextFor ("Fast Translation:" ), justify = "left" , anchor = "w" )
86
+ label = tk .Label (
87
+ owner , justify = "left" , anchor = "w" ,
88
+ textvariable = self ._localization ("Fast and inaccurate translation:" )
89
+ )
73
90
label .pack (side = "top" , fill = tk .X )
74
91
75
92
self ._fastOutputText = tkst .ScrolledText (owner , height = 15 )
76
93
self ._fastOutputText .pack (side = "top" , fill = tk .BOTH , expand = tk .YES )
77
94
78
95
# full translation
79
- label = tk .Label (owner , text = self .UITextFor ("Full Translation:" ), justify = "left" , anchor = "w" )
96
+ label = tk .Label (
97
+ owner , justify = "left" , anchor = "w" ,
98
+ textvariable = self ._localization ("Slow and improved translation:" )
99
+ )
80
100
label .pack (side = "top" , fill = tk .X )
81
101
82
102
self ._fullOutputText = tkst .ScrolledText (owner )
@@ -110,23 +130,22 @@ def startTranslate(self, force=False):
110
130
# set output text to "Processing..."
111
131
if force :
112
132
self ._fullOutputText .delete ("1.0" , tk .END )
113
- self ._fullOutputText .insert (tk .END , self .UITextFor ("Processing..." ))
133
+ self ._fullOutputText .insert (tk .END , self ._localization ("Processing..." ). get ( ))
114
134
return
115
135
116
136
def fastTranslated (self , text ):
117
137
self ._fastOutputText .delete ("1.0" , tk .END )
118
138
self ._fastOutputText .insert (tk .END , text )
119
139
return
120
140
121
- def fullTranslated (self , text ):
122
- notification = None
123
- if isinstance (text , tuple ):
124
- text , notification = text
125
-
141
+ def fullTranslated (self , text , pending ):
126
142
self ._fullOutputText .delete ("1.0" , tk .END )
127
143
self ._fullOutputText .insert (tk .END , text )
128
144
129
- if notification is not None :
145
+ if pending :
146
+ notification = self ._localization (
147
+ "Translation is not accurate and will be updated soon."
148
+ ).get ()
130
149
self ._fullOutputText .insert (tk .END , "\n ----------------\n " + notification )
131
150
return
132
151
@@ -144,9 +163,15 @@ def onSelectLanguage(self, event):
144
163
if code is None : return
145
164
146
165
self ._currentLanguage = code
147
- # TODO: translate UI?
148
166
return
149
167
168
+ def updateLocalization (self , localization ):
169
+ for k , v in localization .items ():
170
+ self ._localizationMap [k ].set (v )
171
+ return
172
+
173
+ def localizationStrings (self ): return list (self ._localizationMap .keys ())
174
+
150
175
if '__main__' == __name__ :
151
176
app = App (
152
177
master = tk .Tk (),
0 commit comments