-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranslation.go
More file actions
140 lines (125 loc) · 3.66 KB
/
translation.go
File metadata and controls
140 lines (125 loc) · 3.66 KB
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
// 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
CreatedBy string
Impressum string
PrivacyPolicy string
ConnectionLost string
ConnectionLostNotPermanentlySavedBrackets string
ConnectedUser string
NoSave string
Save string
OneUser string
ButtonActive string
ButtonDownloadHTML string
ButtonDownloadDelta string
ButtonUploadDelta 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
}