/
locale.go
178 lines (148 loc) · 4.16 KB
/
locale.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
package internal
import (
"fmt"
"text/template"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/message/catalog"
)
// Locale is the default Locale.
// Created by Catalog.
// One Locale maps to one registered and loaded language.
// Stores the translation variables and most importantly, the Messages (keys and their renderers).
type Locale struct {
// The index of the language registered by the user, starting from zero.
index int
tag language.Tag
// ID is the tag.String().
ID string
// Options given by the Catalog
Options Options
// Fields set by Catalog.
FuncMap template.FuncMap
Printer *message.Printer
//
// Fields set by this Load method.
Messages map[string]Renderer
Vars []Var // shared per-locale variables.
}
// Load sets the translation messages based on the Catalog's key values.
func (loc *Locale) Load(c *Catalog, keyValues Map) error {
return loc.setMap(c, "", keyValues)
}
func (loc *Locale) setMap(c *Catalog, key string, keyValues Map) error {
// unique locals or the shared ones.
isRoot := key == ""
vars := getVars(loc, VarsKey, keyValues)
if isRoot {
loc.Vars = vars
} else {
vars = removeVarsDuplicates(append(vars, loc.Vars...))
}
for k, v := range keyValues {
form, isPlural := loc.Options.PluralFormDecoder(loc, k)
if isPlural {
k = key
} else if !isRoot {
k = key + "." + k
}
switch value := v.(type) {
case string:
if err := loc.setString(c, k, value, vars, form); err != nil {
return fmt.Errorf("%s:%s parse string: %w", loc.ID, key, err)
}
case Map:
// fmt.Printf("%s is map\n", fullKey)
if err := loc.setMap(c, k, value); err != nil {
return fmt.Errorf("%s:%s parse map: %w", loc.ID, key, err)
}
default:
return fmt.Errorf("%s:%s unexpected type of %T as value", loc.ID, key, value)
}
}
return nil
}
func (loc *Locale) setString(c *Catalog, key string, value string, vars []Var, form PluralForm) (err error) {
isPlural := form != nil
// fmt.Printf("setStringVars: %s=%s\n", key, value)
msgs, vars := makeSelectfVars(value, vars, isPlural)
msgs = append(msgs, catalog.String(value))
m := &Message{
Locale: loc,
Key: key,
Value: value,
Vars: vars,
Plural: isPlural,
}
var (
renderer, pluralRenderer Renderer = m, m
)
if stringIsTemplateValue(value, loc.Options.Left, loc.Options.Right) {
t, err := NewTemplate(c, m)
if err != nil {
return err
}
pluralRenderer = t
if !isPlural {
renderer = t
}
} else {
if isPlural {
pluralRenderer, err = newIndependentPluralRenderer(c, loc, key, msgs...)
if err != nil {
return fmt.Errorf("<%s = %s>: %w", key, value, err)
}
} else {
// let's make normal keys direct fire:
// renderer = &simpleRenderer{key, loc.Printer}
if err = c.Set(loc.tag, key, msgs...); err != nil {
return fmt.Errorf("<%s = %s>: %w", key, value, err)
}
}
}
if isPlural {
if existingMsg, ok := loc.Messages[key]; ok {
if msg, ok := existingMsg.(*Message); ok {
msg.AddPlural(form, pluralRenderer)
return
}
}
m.AddPlural(form, pluralRenderer)
}
loc.Messages[key] = renderer
return
}
// Index returns the current locale index from the languages list.
func (loc *Locale) Index() int {
return loc.index
}
// Tag returns the full language Tag attached to this Locale,
// it should be unique across different Locales.
func (loc *Locale) Tag() *language.Tag {
return &loc.tag
}
// Language should return the exact languagecode of this `Locale`
// that the user provided on `New` function.
//
// Same as `Tag().String()` but it's static.
func (loc *Locale) Language() string {
return loc.ID
}
// GetMessage should return translated text based on the given "key".
func (loc *Locale) GetMessage(key string, args ...interface{}) string {
return loc.getMessage(loc.ID, key, args...)
}
func (loc *Locale) getMessage(langInput, key string, args ...interface{}) string {
if msg, ok := loc.Messages[key]; ok {
result, err := msg.Render(args...)
if err != nil {
result = err.Error()
}
return result
}
if fn := loc.Options.DefaultMessageFunc; fn != nil {
// let langInput to be empty if that's the case.
return fn(langInput, loc.ID, key, args...)
}
return ""
}