-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
i18n.go
205 lines (172 loc) · 4.33 KB
/
i18n.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
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"context"
"embed"
"fmt"
"io/fs"
"testing/fstest"
"github.com/BurntSushi/toml"
"golang.org/x/text/language"
"zgo.at/errors"
"zgo.at/json"
"zgo.at/z18n"
"zgo.at/z18n/msgfile"
"zgo.at/zdb"
"zgo.at/zlog"
"zgo.at/zstd/zfs"
)
// Translations contains all translation messages.
//
//go:embed i18n/*
var translations embed.FS
// Translations gets the translation messages; a user can have a local override,
// so we need to apply that per-user.
func Translations(ctx context.Context) fs.FS {
builtin, _ := fs.Sub(translations, "i18n")
if ctx == nil || GetUser(ctx) == nil {
return builtin
}
var over OverrideTranslations
err := over.Get(ctx, false)
if err != nil {
return builtin
}
mapfs := fstest.MapFS{}
for _, o := range over {
t, _ := o.File.TOML()
mapfs[o.Name] = &fstest.MapFile{Data: []byte(t)}
}
return zfs.NewOverlayFS(builtin, mapfs)
}
var defaultBundle = func() *z18n.Bundle {
b, err := newBundle(Translations(context.TODO()))
if err != nil {
panic(err)
}
return b
}()
func DefaultLocale() *z18n.Locale {
return defaultBundle.Locale("en")
}
func GetBundle(ctx context.Context) *z18n.Bundle {
if ctx == nil || GetUser(ctx) == nil {
return defaultBundle
}
var over OverrideTranslations
err := over.Get(ctx, false)
if err != nil {
if !zdb.ErrNoRows(err) {
zlog.Error(err)
}
return defaultBundle
}
b, err := newBundle(Translations(ctx))
if err != nil {
zlog.Error(err)
return defaultBundle
}
return b
}
func newBundle(fsys fs.FS) (*z18n.Bundle, error) {
b := z18n.NewBundle(language.MustParse("en-GB"))
err := b.ReadMessagesDir(fsys, "*.toml")
return b, err
}
type OverrideTranslation struct {
Name string `json:"name"`
Updated string `json:"updated"`
File msgfile.File `json:"file"`
Diff string `json:"diff"`
}
type OverrideTranslations []OverrideTranslation
func (OverrideTranslations) Key(ctx context.Context) string {
return fmt.Sprintf("i18n-%d", MustGetUser(ctx).ID)
}
type wrap []wrapF
type wrapF struct{ Name, Updated, TOML string }
func (o OverrideTranslations) encode() (string, error) {
var w wrap
for _, oo := range o {
t, err := oo.File.TOML()
if err != nil {
return "", err
}
w = append(w, wrapF{
Name: oo.Name,
Updated: oo.Updated,
TOML: t,
})
}
j, err := json.MarshalIndent(w, "", " ")
return string(j), err
}
func (o *OverrideTranslations) Decode(data string) error {
var w wrap
err := json.Unmarshal([]byte(data), &w)
if err != nil {
return err
}
oo := make(OverrideTranslations, 0, len(w))
for _, ww := range w {
var f msgfile.File
_, err := toml.Decode(ww.TOML, &f)
if err != nil {
return err
}
oo = append(oo, OverrideTranslation{
Name: ww.Name,
Updated: ww.Updated,
File: f,
})
}
*o = oo
return nil
}
func (o *OverrideTranslations) Insert(ctx context.Context) error {
t, err := o.encode()
if err != nil {
return errors.Wrap(err, "OverrideTranslations.Insert")
}
err = zdb.Exec(ctx, `insert into store (key, value) values (?, ?)`, o.Key(ctx), t)
if err != nil {
return errors.Wrap(err, "OverrideTranslations.Insert")
}
cacheI18n(ctx).Delete(o.Key(ctx))
return nil
}
func (o *OverrideTranslations) Update(ctx context.Context) error {
t, err := o.encode()
if err != nil {
return errors.Wrap(err, "OverrideTranslations.Update")
}
err = zdb.Exec(ctx, `update store set value=? where key=?`, t, o.Key(ctx))
if err != nil {
return errors.Wrap(err, "OverrideTranslations.Update")
}
cacheI18n(ctx).Delete(o.Key(ctx))
return nil
}
func (o *OverrideTranslations) Get(ctx context.Context, insert bool) error {
if oo, ok := cacheI18n(ctx).Get(o.Key(ctx)); ok {
*o = *oo.(*OverrideTranslations)
return nil
}
var data []byte
err := zdb.Get(ctx, &data, `select value from store where key = ?`, o.Key(ctx))
if err != nil {
if insert && zdb.ErrNoRows(err) {
*o = OverrideTranslations{}
return o.Insert(ctx)
}
return errors.Wrap(err, "OverrideTranslations.Get")
}
err = o.Decode(string(data))
if err != nil {
return errors.Wrap(err, "OverrideTranslations.List")
}
cacheI18n(ctx).SetDefault(o.Key(ctx), o)
return nil
}