-
Notifications
You must be signed in to change notification settings - Fork 176
/
languages.go
249 lines (211 loc) · 6.04 KB
/
languages.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
// Copyright (c) 2018 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package languages
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"
"gopkg.in/yaml.v2"
)
const (
// for a language (e.g., `fi-FI`) to be supported
// it must have a metadata file named, e.g., `fi-FI.lang.yaml`
metadataFileSuffix = ".lang.yaml"
)
var (
stringsFileSuffixes = []string{"-irc.lang.json", "-help.lang.json", "-nickserv.lang.json", "-hostserv.lang.json", "-chanserv.lang.json"}
)
// LangData is the data contained in a language file.
type LangData struct {
Name string
Code string
Contributors string
Incomplete bool
}
// Manager manages our languages and provides translation abilities.
type Manager struct {
Languages map[string]LangData
translations map[string]map[string]string
defaultLang string
}
// NewManager returns a new Manager.
func NewManager(enabled bool, path string, defaultLang string) (lm *Manager, err error) {
lm = &Manager{
Languages: make(map[string]LangData),
translations: make(map[string]map[string]string),
defaultLang: defaultLang,
}
// make fake "en" info
lm.Languages["en"] = LangData{
Code: "en",
Name: "English",
Contributors: "Oragono contributors and the IRC community",
}
if enabled {
err = lm.loadData(path)
if err == nil {
// successful load, check that defaultLang is sane
_, ok := lm.Languages[lm.defaultLang]
if !ok {
err = fmt.Errorf("Cannot find default language [%s]", lm.defaultLang)
}
}
} else {
lm.defaultLang = "en"
}
return
}
func (lm *Manager) loadData(path string) (err error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return
}
// 1. for each language that has a ${langcode}.lang.yaml in the languages path
// 2. load ${langcode}.lang.yaml
// 3. load ${langcode}-irc.lang.json and friends as the translations
for _, f := range files {
if f.IsDir() {
continue
}
// glob up *.lang.yaml in the directory
name := f.Name()
if !strings.HasSuffix(name, metadataFileSuffix) {
continue
}
prefix := strings.TrimSuffix(name, metadataFileSuffix)
// load, e.g., `zh-CN.lang.yaml`
var data []byte
data, err = ioutil.ReadFile(filepath.Join(path, name))
if err != nil {
return
}
var langInfo LangData
err = yaml.Unmarshal(data, &langInfo)
if err != nil {
return err
}
if langInfo.Code == "en" {
return fmt.Errorf("Cannot have language file with code 'en' (this is the default language using strings inside the server code). If you're making an English variant, name it with a more specific code")
}
// check for duplicate languages
_, exists := lm.Languages[strings.ToLower(langInfo.Code)]
if exists {
return fmt.Errorf("Language code [%s] defined twice", langInfo.Code)
}
// slurp up all translation files with `prefix` into a single translation map
translations := make(map[string]string)
for _, translationSuffix := range stringsFileSuffixes {
stringsFilePath := filepath.Join(path, prefix+translationSuffix)
data, err = ioutil.ReadFile(stringsFilePath)
if err != nil {
continue // skip missing paths
}
var tlList map[string]string
err = json.Unmarshal(data, &tlList)
if err != nil {
return fmt.Errorf("invalid json for translation file %s: %s", stringsFilePath, err.Error())
}
for key, value := range tlList {
// because of how crowdin works, this is how we skip untranslated lines
if key == value || strings.TrimSpace(value) == "" {
continue
}
translations[key] = value
}
}
if len(translations) == 0 {
// skip empty translations
continue
}
// sanity check the language definition from the yaml file
if langInfo.Code == "" || langInfo.Name == "" || langInfo.Contributors == "" {
return fmt.Errorf("Code, name or contributors is empty in language file [%s]", name)
}
key := strings.ToLower(langInfo.Code)
lm.Languages[key] = langInfo
lm.translations[key] = translations
}
return nil
}
// Default returns the default languages.
func (lm *Manager) Default() []string {
return []string{lm.defaultLang}
}
// Count returns how many languages we have.
func (lm *Manager) Count() int {
return len(lm.Languages)
}
// Enabled returns whether translation is enabled.
func (lm *Manager) Enabled() bool {
return len(lm.translations) != 0
}
// Translators returns the languages we have and the translators.
func (lm *Manager) Translators() []string {
var tlist sort.StringSlice
for _, info := range lm.Languages {
if info.Code == "en" {
continue
}
tlist = append(tlist, fmt.Sprintf("%s (%s): %s", info.Name, info.Code, info.Contributors))
}
tlist.Sort()
return tlist
}
// Codes returns the proper language codes for the given casefolded language codes.
func (lm *Manager) Codes(codes []string) []string {
var newCodes []string
for _, code := range codes {
info, exists := lm.Languages[code]
if exists {
newCodes = append(newCodes, info.Code)
}
}
if len(newCodes) == 0 {
newCodes = []string{"en"}
}
return newCodes
}
// Translate returns the given string, translated into the given language.
func (lm *Manager) Translate(languages []string, originalString string) string {
// not using any special languages
if len(languages) == 0 || languages[0] == "en" || len(lm.translations) == 0 {
return originalString
}
for _, lang := range languages {
lang = strings.ToLower(lang)
if lang == "en" {
return originalString
}
translations, exists := lm.translations[lang]
if !exists {
continue
}
newString, exists := translations[originalString]
if !exists {
continue
}
// found a valid translation!
return newString
}
// didn't find any translation
return originalString
}
func (lm *Manager) CapValue() string {
langCodes := make(sort.StringSlice, len(lm.Languages)+1)
langCodes[0] = strconv.Itoa(len(lm.Languages))
i := 1
for _, info := range lm.Languages {
codeToken := info.Code
if info.Incomplete {
codeToken = "~" + info.Code
}
langCodes[i] = codeToken
i += 1
}
langCodes.Sort()
return strings.Join(langCodes, ",")
}