-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
localization.go
220 lines (208 loc) · 5.68 KB
/
localization.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
// Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
// Package i18n provides internationalization support for applications.
package i18n
import (
"bufio"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/toolbox/xio"
"github.com/richardwilkes/toolbox/xio/fs"
)
const (
// Extension is the file name extension required on localization files.
Extension = ".i18n"
)
var (
// Dir is the directory to scan for localization files. This will occur only once, the first time a call to Text()
// is made. If you do not set this prior to the first call, a directory in the same location as the executable with
// "_i18n" appended to the executable name (sans any extension) will be used.
Dir string
// Language is the language that should be used for text returned from calls to Text(). It is initialized to the
// result of calling Locale(). You may set this at runtime, forcing a particular language for all subsequent calls
// to Text().
Language = Locale()
// Languages is a slice of languages to fall back to should the one specified in the Language variable not be
// available. It is initialized to the value of the LANGUAGE environment variable.
Languages = strings.Split(os.Getenv("LANGUAGE"), ":")
altLocalizer atomic.Pointer[localizer]
once sync.Once
langMap = make(map[string]map[string]string)
hierLock sync.Mutex
hierMap = make(map[string][]string)
)
type localizer struct {
Text func(string) string
}
// SetLocalizer sets the function to use for localizing text. If this is not set or explicitly set to nil, the default
// localization mechanism will be used.
func SetLocalizer(f func(string) string) {
var trampoline *localizer
if f != nil {
trampoline = &localizer{Text: f}
}
altLocalizer.Store(trampoline)
}
// Text returns a localized version of the text if one exists, or the original text if not.
func Text(text string) string {
if f := altLocalizer.Load(); f != nil {
return f.Text(text)
}
once.Do(func() {
if Dir == "" {
path, err := os.Executable()
if err != nil {
return
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
return
}
path, err = filepath.Abs(fs.TrimExtension(path) + "_i18n")
if err != nil {
return
}
Dir = path
}
dirEntry, err := os.ReadDir(Dir)
if err != nil {
return
}
for _, one := range dirEntry {
if !one.IsDir() {
name := one.Name()
if filepath.Ext(name) == Extension {
load(name)
}
}
}
})
var result string
if result = lookup(text, Language); result != "" {
return result
}
for _, language := range Languages {
if result = lookup(text, language); result != "" {
return result
}
}
return text
}
func lookup(text, language string) string {
for _, lang := range hierarchy(language) {
if translations := langMap[lang]; translations != nil {
if str, ok := translations[text]; ok {
return str
}
}
}
return ""
}
func hierarchy(language string) []string {
lang := strings.ToLower(language)
hierLock.Lock()
defer hierLock.Unlock()
if s, ok := hierMap[lang]; ok {
return s
}
one := strings.ReplaceAll(strings.ReplaceAll(lang, "-", "_"), ".", "_")
var s []string
for {
s = append(s, one)
if i := strings.LastIndex(one, "_"); i != -1 {
one = one[:i]
} else {
break
}
}
hierMap[lang] = s
return s
}
func load(name string) {
path := filepath.Join(Dir, name)
f, err := os.Open(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return
}
errs.Log(errs.NewWithCause("i18n: unable to load", err), "path", path)
return
}
defer xio.CloseIgnoringErrors(f)
lineNum := 1
lastKeyLineStart := 1
translations := make(map[string]string)
var key, value string
var hasKey, hasValue bool
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
if strings.HasPrefix(line, "k:") {
if hasValue {
if _, exists := translations[key]; !exists {
translations[key] = value
} else {
slog.Warn("i18n: ignoring duplicate key", "line", lastKeyLineStart, "file", path)
}
hasKey = false
hasValue = false
}
var buffer string
if _, err = fmt.Sscanf(line, "k:%q", &buffer); err != nil {
slog.Warn("i18n: ignoring invalid key", "line", lineNum, "file", path)
} else {
if hasKey {
key += "\n" + buffer
} else {
key = buffer
hasKey = true
lastKeyLineStart = lineNum
}
}
} else if strings.HasPrefix(line, "v:") {
if hasKey {
var buffer string
if _, err = fmt.Sscanf(line, "v:%q", &buffer); err != nil {
slog.Warn("i18n: ignoring invalid value", "line", lineNum, "file", path)
} else {
if hasValue {
value += "\n" + buffer
} else {
value = buffer
hasValue = true
}
}
} else {
slog.Warn("i18n: ignoring value with no previous key", "line", lineNum, "file", path)
}
}
lineNum++
}
if hasKey {
if hasValue {
if _, exists := translations[key]; !exists {
translations[key] = value
} else {
slog.Warn("i18n: ignoring duplicate key", "line", lastKeyLineStart, "file", path)
}
} else {
slog.Warn("i18n: ignoring key with missing value", "line", lastKeyLineStart, "file", path)
}
}
key = strings.ToLower(name[:len(name)-len(Extension)])
key = strings.ReplaceAll(strings.ReplaceAll(key, "-", "_"), ".", "_")
langMap[key] = translations
}