-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.go
254 lines (232 loc) · 9.29 KB
/
translate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// translate.go
// File generated on Wed, 07 Apr 2021 17:39:24 using Gotk3 Objects Translate v1.5 2019-20 H.F.M
/*
* This program comes with absolutely no warranty.
* See the The MIT License (MIT) for details:
* https://opensource.org/licenses/mit-license.php
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/gotk3/gotk3/gtk"
)
// initGtkObjectsText: read translations from structure and set them to objects.
func (trans *MainTranslate) initGtkObjectsText() {
trans.setTextToGtkObjects(&mainObjects.CheckbuttonAddReminder.Widget, "CheckbuttonAddReminder")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonAppendFiles.Widget, "CheckbuttonAppendFiles")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonBlake2b256.Widget, "CheckbuttonBlake2b256")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonBlake2b384.Widget, "CheckbuttonBlake2b384")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonBlake2b512.Widget, "CheckbuttonBlake2b512")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonConcurrentOp.Widget, "CheckbuttonConcurrentOp")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonCreateFile.Widget, "CheckbuttonCreateFile")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonMd4.Widget, "CheckbuttonMd4")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonMd5.Widget, "CheckbuttonMd5")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonRecursiveScan.Widget, "CheckbuttonRecursiveScan")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonSha1.Widget, "CheckbuttonSha1")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonSha256.Widget, "CheckbuttonSha256")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonSha384.Widget, "CheckbuttonSha384")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonSha512.Widget, "CheckbuttonSha512")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonSha3_256.Widget, "CheckbuttonSha3_256")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonSha3_384.Widget, "CheckbuttonSha3_384")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonSha3_512.Widget, "CheckbuttonSha3_512")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonShowFilename.Widget, "CheckbuttonShowFilename")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonShowSplash.Widget, "CheckbuttonShowSplash")
trans.setTextToGtkObjects(&mainObjects.CheckbuttonUseDecimal.Widget, "CheckbuttonUseDecimal")
trans.setTextToGtkObjects(&mainObjects.ImageAppIcon.Widget, "ImageAppIcon")
trans.setTextToGtkObjects(&mainObjects.LabelMethods.Widget, "LabelMethods")
trans.setTextToGtkObjects(&mainObjects.LabelOptions.Widget, "LabelOptions")
trans.setTextToGtkObjects(&mainObjects.LabelOutput.Widget, "LabelOutput")
trans.setTextToGtkObjects(&mainObjects.LabelTitle.Widget, "LabelTitle")
trans.setTextToGtkObjects(&mainObjects.MainButtonCancel.Widget, "MainButtonCancel")
trans.setTextToGtkObjects(&mainObjects.MainButtonDone.Widget, "MainButtonDone")
trans.setTextToGtkObjects(&mainObjects.MainStatusbar.Widget, "MainStatusbar")
trans.setTextToGtkObjects(&mainObjects.ScrolledWindowTextViewDisplay.Widget, "ScrolledWindowTextViewDisplay")
trans.setTextToGtkObjects(&mainObjects.SwitchExpand.Widget, "SwitchExpand")
trans.setTextToGtkObjects(&mainObjects.SwitchTreeView.Widget, "SwitchTreeView")
trans.setTextToGtkObjects(&mainObjects.TextViewDisplay.Widget, "TextViewDisplay")
trans.setTextToGtkObjects(&mainObjects.TreeViewDisplay.Widget, "TreeViewDisplay")
}
// Translations structure declaration. To be used in main application.
var translate = new(MainTranslate)
// sts: some sentences/words used in the application. Mostly used in Development mode.
// You must add there all sentences used in your application. Or not ...
// They'll be added to language file each time application started
// when "devMode" is set at true.
var sts = map[string]string{
`mtd`: `Hash method`,
`no`: `No`,
`yes`: `Yes`,
`cancel`: `Cancel`,
`dat`: `Data`,
}
// Translations structure with methods
type MainTranslate struct {
// Public
ProgInfos progInfo
Language language
Options parsingFlags
ObjectsCount int
Objects []object
Sentences map[string]string
// Private
objectsLoaded bool
}
// MainTranslateNew: Initialise new translation structure and assign language file content to GtkObjects.
// devModeActive, indicate that the new sentences must be added to previous language file.
func MainTranslateNew(filename string, devModeActive ...bool) (mt *MainTranslate) {
var err error
mt = new(MainTranslate)
if err = mt.read(filename); err == nil {
mt.initGtkObjectsText()
if len(devModeActive) != 0 {
if devModeActive[0] {
mt.Sentences = sts
err := mt.write(filename)
if err != nil {
fmt.Printf("%s\n%s\n", "Cannot write actual sentences to language file.", err.Error())
}
}
}
} else {
fmt.Printf("%s\n%s\n", "Error loading language file !\nNot an error when you just creating from glade Xml or GOH project file.", err.Error())
}
return
}
// readFile: language file.
func (trans *MainTranslate) read(filename string) (err error) {
var textFileBytes []byte
if textFileBytes, err = ioutil.ReadFile(filename); err == nil {
if err = json.Unmarshal(textFileBytes, &trans); err == nil {
trans.objectsLoaded = true
}
}
return
}
// Write json datas to file
func (trans *MainTranslate) write(filename string) (err error) {
var out bytes.Buffer
var jsonData []byte
if jsonData, err = json.Marshal(&trans); err == nil && trans.objectsLoaded {
if err = json.Indent(&out, jsonData, "", "\t"); err == nil {
err = ioutil.WriteFile(filename, out.Bytes(), 0644)
}
}
return
}
type parsingFlags struct {
SkipLowerCase,
SkipEmptyLabel,
SkipEmptyName,
DoBackup bool
}
type progInfo struct {
Name,
Version,
Creat,
MainObjStructName,
GladeXmlFilename,
TranslateFilename,
ProjectRootDir,
GohProjFile string
}
type language struct {
LangNameLong,
LangNameShrt,
Author,
Date,
Updated string
Contributors []string
}
type object struct {
Class,
Id,
Label,
Tooltip,
Text,
Uri,
Comment string
LabelMarkup,
LabelWrap,
TooltipMarkup bool
Idx int
}
// Define available property within objects
type propObject struct {
Class string
Label,
LabelMarkup,
LabelWrap,
Tooltip,
TooltipMarkup,
Text,
Uri bool
}
// Property that exists for Gtk3 Object ... (Used for Class capability)
var propPerObjects = []propObject{
{Class: "GtkButton", Label: true, Tooltip: true, TooltipMarkup: true},
{Class: "GtkMenuButton", Label: true, Tooltip: true, TooltipMarkup: true},
// {Class: "GtkToolButton", Label: true, Tooltip: true, TooltipMarkup: true}, // Deprecated since 3.10
// {Class: "GtkImageMenuItem", Label: true, Tooltip: true, TooltipMarkup: true}, // Deprecated since 3.10
{Class: "GtkMenuItem", Label: true, Tooltip: true, TooltipMarkup: true},
{Class: "GtkCheckMenuItem", Label: true, Tooltip: true, TooltipMarkup: true},
{Class: "GtkRadioMenuItem", Label: true, Tooltip: true, TooltipMarkup: true},
{Class: "GtkToggleButton", Label: true, Tooltip: true, TooltipMarkup: true},
{Class: "GtkLabel", Label: true, LabelMarkup: true, Tooltip: true, TooltipMarkup: true, LabelWrap: true},
{Class: "GtkSpinButton", Tooltip: true, TooltipMarkup: true},
{Class: "GtkEntry", Tooltip: true, TooltipMarkup: true},
{Class: "GtkCheckButton", Label: true, Tooltip: true, TooltipMarkup: true},
{Class: "GtkProgressBar", Tooltip: true, TooltipMarkup: true, Text: true},
{Class: "GtkSearchBar", Tooltip: true, TooltipMarkup: true},
{Class: "GtkImage", Tooltip: true, TooltipMarkup: true},
{Class: "GtkRadioButton", Label: true, LabelMarkup: false, Tooltip: true, TooltipMarkup: true},
{Class: "GtkComboBoxText", Tooltip: true, TooltipMarkup: true},
{Class: "GtkComboBox", Tooltip: true, TooltipMarkup: true},
{Class: "GtkLinkButton", Label: true, Tooltip: true, TooltipMarkup: true, Uri: true},
{Class: "GtkSwitch", Tooltip: true, TooltipMarkup: true},
{Class: "GtkTreeView", Tooltip: true, TooltipMarkup: true},
{Class: "GtkFileChooserButton", Tooltip: true, TooltipMarkup: true},
{Class: "GtkTextView", Tooltip: true, TooltipMarkup: true},
{Class: "GtkSourceView", Tooltip: true, TooltipMarkup: true},
{Class: "GtkStatusbar", Tooltip: true, TooltipMarkup: true},
{Class: "GtkScrolledWindow", Tooltip: true, TooltipMarkup: true},
}
// setTextToGtkObjects: read translations from structure and set them to object.
// like this: setTextToGtkObjects(&mainObjects.TransLabelHint.Widget, "TransLabelHint")
func (trans *MainTranslate) setTextToGtkObjects(obj *gtk.Widget, objectId string) {
for _, currObject := range trans.Objects {
if currObject.Id == objectId {
for _, props := range propPerObjects {
if currObject.Class == props.Class {
if props.Label {
obj.SetProperty("label", currObject.Label)
if props.LabelMarkup {
obj.SetProperty("use-markup", currObject.LabelMarkup)
obj.SetProperty("label", strings.ReplaceAll(currObject.Label, "&", "&"))
}
}
if props.LabelWrap {
obj.SetProperty("wrap", currObject.LabelWrap)
}
if props.Tooltip && !currObject.TooltipMarkup {
obj.SetProperty("tooltip_text", currObject.Tooltip)
}
if props.Tooltip && currObject.TooltipMarkup {
obj.SetProperty("tooltip_markup", strings.ReplaceAll(currObject.Tooltip, "&", "&"))
}
if props.Text {
obj.SetProperty("text", currObject.Text)
}
if props.Uri {
obj.SetProperty("uri", currObject.Uri)
}
break
}
}
break
}
}
}