-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation.go
198 lines (183 loc) · 5.43 KB
/
translation.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020,2022 Marcus Soll
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"embed"
"encoding/json"
"log"
"path/filepath"
"reflect"
"strings"
"sync"
)
// Translation represents an object holding all translations
type Translation struct {
Language string
WeekdayMonday string
WeekdayTuesday string
WeekdayWednesday string
WeekdayThursday string
WeekdayFriday string
WeekdaySaturday string
WeekdaySunday string
DateYes string
DateNo string
DateOnlyIfNeeded string
DateCanNotSay string
Name string
Optional string
Points string
Submit string
CreatedBy string
Impressum string
PrivacyPolicy string
NewPoll string
NormalPoll string
AppointmentPoll string
Question string
AnswerOption string
Value string
Colour string
Description string
AddOption string
Yes string
No string
Username string
Password string
AcceptPrivacyPolicy string
CreatePoll string
Time string
StartDate string
EndDate string
NoTime string
AddTime string
Participate string
SelectPollKind string
Results string
PollToLargeError string
PollNoOptions string
DeletePoll string
PollIsDeleted string
Starred string
LoadConfiguration string
Configuration string
MoreOptions string
ExportConfiguration string
Comment string
Unknown string
SelectAll string
FunctionRequiresJavaScript string
UserNotCreator string
CreateNewPollRandom string
PleaseWait string
AuthentificationFailure string
ErrorOccured string
OpinionPoll string
OpinionItem string
AddOpinionItem string
OpinionGood string
OpinionRatherGood string
OpinionNeutral string
OpinionRatherBad string
OpinionBad string
InvalidKey string
EditAnswer string
DeleteAnswer string
RememberedAs string
}
const defaultLanguage = "en"
//go:embed translation
var translationFiles embed.FS
var initialiseCurrent sync.Once
var current Translation
var rwlock sync.RWMutex
var translationPath = "./translation"
// GetTranslation returns a Translation struct of the given language.
// This function always loads translations from disk. Try to use GetDefaultTranslation where possible.
func GetTranslation(language string) (Translation, error) {
t, err := getSingleTranslation(language)
if err != nil {
return Translation{}, err
}
d, err := getSingleTranslation(defaultLanguage)
if err != nil {
return Translation{}, err
}
// Set unknown strings to default value
vp := reflect.ValueOf(&t)
dv := reflect.ValueOf(d)
v := vp.Elem()
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).CanSet() {
continue
}
if v.Field(i).Kind() != reflect.String {
continue
}
if v.Field(i).String() == "" {
v.Field(i).SetString(dv.Field(i).String())
}
}
return t, nil
}
func getSingleTranslation(language string) (Translation, error) {
if language == "" {
return GetDefaultTranslation(), nil
}
file := strings.Join([]string{language, "json"}, ".")
file = filepath.Join(translationPath, file)
b, err := translationFiles.ReadFile(file)
if err != nil {
return Translation{}, err
}
t := Translation{}
err = json.Unmarshal(b, &t)
if err != nil {
return Translation{}, err
}
return t, nil
}
// SetDefaultTranslation sets the default language to the provided one.
// Does nothing if it returns error != nil.
func SetDefaultTranslation(language string) error {
if language == "" {
return nil
}
t, err := GetTranslation(language)
rwlock.Lock()
defer rwlock.Unlock()
if err != nil {
return err
}
current = t
return nil
}
// GetDefaultTranslation returns a Translation struct of the current default language.
func GetDefaultTranslation() Translation {
initialiseCurrent.Do(func() {
rwlock.RLock()
c := current.Language
rwlock.RUnlock()
if c == "" {
err := SetDefaultTranslation(defaultLanguage)
if err != nil {
log.Printf("Can not load default language (%s): %s", defaultLanguage, err.Error())
}
}
})
rwlock.RLock()
defer rwlock.RUnlock()
return current
}