-
Notifications
You must be signed in to change notification settings - Fork 4
/
i18n.go
271 lines (232 loc) · 7.3 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2021 Joe Chen. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package i18n
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
"golang.org/x/text/language"
"gopkg.in/ini.v1"
"unknwon.dev/i18n/internal/plural"
)
// Store contains a collection of locales and their descriptive names.
type Store struct {
langs []string
descs []string
locales map[string]*Locale
rules plural.Rules
}
// NewStore initializes and returns a new Store.
func NewStore() *Store {
return &Store{
locales: make(map[string]*Locale),
rules: plural.DefaultRules(),
}
}
// add attempts to add the given locale into the store. It returns true if it
// was successfully added, false if a locale with the same language name has
// already existed.
func (s *Store) add(l *Locale) bool {
if _, ok := s.locales[l.Lang()]; ok {
return false
}
s.langs = append(s.langs, l.Lang())
s.descs = append(s.descs, l.Description())
s.locales[l.Lang()] = l
return true
}
// AddLocale adds a locale with given language name and description that is
// loaded from the list of sources. Please refer to INI documentation regarding
// what is considered as a valid data source:
// https://ini.unknwon.io/docs/howto/load_data_sources.
func (s *Store) AddLocale(lang, desc string, source interface{}, others ...interface{}) (*Locale, error) {
tag, err := language.Parse(lang)
if err != nil {
return nil, errors.Wrap(err, "parse lang")
}
file, err := ini.LoadSources(
ini.LoadOptions{
IgnoreInlineComment: true,
UnescapeValueCommentSymbols: true,
},
source,
others...,
)
if err != nil {
return nil, errors.Wrap(err, "load sources")
}
file.BlockMode = false // We only read from the file
rule := s.rules[tag]
if rule == nil {
base, confidence := tag.Base()
if confidence != language.No {
rule = s.rules[language.MustParse(base.String())]
}
}
l, err := newLocale(tag, desc, rule, file)
if err != nil {
return nil, errors.Wrap(err, "new locale")
}
if !s.add(l) {
return nil, errors.Errorf("duplicated locales for %q", lang)
}
return l, nil
}
var ErrLocalNotFound = errors.New("locale not found")
// Locale returns the locale with the given language name.
func (s *Store) Locale(lang string) (*Locale, error) {
l, ok := s.locales[lang]
if !ok {
return nil, ErrLocalNotFound
}
return l, nil
}
type pluralPlaceholder struct {
name string
forms map[plural.Form]string
}
// Message represents a message in a locale.
type Message struct {
pluralRule *plural.Rule
format string
placeholders map[int]*pluralPlaceholder
}
// Translate translates the message with the supplied list of arguments.
func (m *Message) Translate(args ...interface{}) string {
if len(args) == 0 {
return m.format
}
if len(m.placeholders) == 0 {
return fmt.Sprintf(m.format, args...)
}
// NOTE: strings.NewReplacer makes >3x more allocations and 5x slower than strings.Replace.
// For strings.NewReplacer:
// BenchmarkLocale_Translate_Plural-16 987433 1097 ns/op 2585 B/op 10 allocs/op
// For strings.Replace:
// BenchmarkLocale_Translate_Plural-16 4316941 285.3 ns/op 80 B/op 3 allocs/op
format := m.format
for index, placeholder := range m.placeholders {
if len(args) < index {
format = strings.Replace(format, placeholder.name, fmt.Sprintf("<no arg for index %d>", index), 1)
continue
}
ops, err := plural.NewOperands(args[index-1])
if err != nil {
format = strings.Replace(format, placeholder.name, fmt.Sprintf("<%v>", err), 1)
continue
}
form := plural.Other
if m.pluralRule != nil {
form = m.pluralRule.PluralFormFunc(ops)
}
format = strings.Replace(format, placeholder.name, placeholder.forms[form], 1)
}
return fmt.Sprintf(format, args...)
}
// Locale represents a locale with target language and a collection of messages.
type Locale struct {
tag language.Tag
desc string
messages map[string]*Message
}
var placeholderRe = regexp.MustCompile(`\${([a-zA-z]+),\s*(\d+)}`) // e.g. ${file, 1} => ["file", "1"]
// newLocale creates a new Locale with given language tag, description and the
// raw locale file. The "[plurals]" section is reserved to define all plurals.
func newLocale(tag language.Tag, desc string, rule *plural.Rule, file *ini.File) (*Locale, error) {
const pluralsSection = "plurals"
s := file.Section(pluralsSection)
keys := s.Keys()
pluralForms := make(map[string]map[plural.Form]string, len(keys))
for _, k := range s.Keys() {
fields := strings.SplitN(k.Name(), ".", 2)
if len(fields) != 2 {
continue
}
noun, form := fields[0], fields[1]
p, ok := pluralForms[noun]
if !ok {
p = make(map[plural.Form]string, 6)
pluralForms[noun] = p
}
switch plural.Form(form) {
case plural.Zero, plural.One, plural.Two, plural.Few, plural.Many, plural.Other:
p[plural.Form(form)] = k.String()
}
}
messages := make(map[string]*Message)
for _, s := range file.Sections() {
if s.Name() == pluralsSection {
continue
}
for _, k := range s.Keys() {
// NOTE: Majority of messages do not need to deal with plurals, thus it makes
// sense to leave them with a nil map to save some memory space.
var placeholders map[int]*pluralPlaceholder
format := k.String()
if strings.Contains(format, "${") {
matches := placeholderRe.FindAllStringSubmatch(format, -1)
replaces := make([]string, 0, len(matches)*2)
placeholders = make(map[int]*pluralPlaceholder, len(matches))
for _, submatch := range matches {
placeholder := submatch[0]
noun := submatch[1]
index, _ := strconv.Atoi(submatch[2])
if index < 1 {
return nil, errors.Errorf("the smallest index is 1 but got %d for %q", index, placeholder)
}
forms, ok := pluralForms[noun]
if !ok {
replaces = append(replaces, placeholder, fmt.Sprintf("<no such plural: %s>", noun))
continue
}
name := fmt.Sprintf("${%d}", index)
replaces = append(replaces, placeholder, name)
placeholders[index] = &pluralPlaceholder{
name: name,
forms: forms,
}
}
format = strings.NewReplacer(replaces...).Replace(format)
}
key := strings.TrimPrefix(s.Name()+"::"+k.Name(), ini.DefaultSection+"::")
messages[key] = &Message{
pluralRule: rule,
format: format,
placeholders: placeholders,
}
}
}
return &Locale{
tag: tag,
desc: desc,
messages: messages,
}, nil
}
// Lang returns the BCP 47 language name of the locale.
func (l *Locale) Lang() string {
return l.tag.String()
}
// Description returns the descriptive name of the locale.
func (l *Locale) Description() string {
return l.desc
}
// Translate uses the locale to translate the message of the given key.
func (l *Locale) Translate(key string, args ...interface{}) string {
return l.TranslateWithFallback(nil, key, args...)
}
// TranslateWithFallback uses the locale to translate the message of the given
// key. It attempts to use the `fallback` to translate if the given key does not
// exist in the locale.
func (l *Locale) TranslateWithFallback(fallback *Locale, key string, args ...interface{}) string {
m, ok := l.messages[key]
if !ok {
if fallback != nil {
return fallback.Translate(key, args...)
}
return fmt.Sprintf("<no such key: %s>", key)
}
return m.Translate(args...)
}