-
Notifications
You must be signed in to change notification settings - Fork 5
/
translations.go
212 lines (183 loc) · 5.54 KB
/
translations.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
package t
import (
"io/fs"
"sort"
"github.com/youthlin/t/locale"
)
// DefaultDomain 默认的文本域
const DefaultDomain = "default"
// DefaultSourceCodeLocale 默认的源代码语言
const DefaultSourceCodeLocale = "en_US"
// Translations holds several translation domains
// ts. [翻译集]包含多个翻译,每个翻译分别属于一个文本域
type Translations struct {
locale string
domain string
domains map[string]*Translation // key is domain
// sourceCodeLocale 源代码中的语言, 通常应该使用英文
sourceCodeLocale string
}
// NewTranslations create a new Translations 新建翻译集
func NewTranslations() *Translations {
return &Translations{
locale: locale.GetDefault(),
domain: DefaultDomain,
domains: make(map[string]*Translation),
sourceCodeLocale: DefaultSourceCodeLocale,
}
}
// clone clones a Translations
func (ts *Translations) clone() *Translations {
return &Translations{
locale: ts.locale,
domain: ts.domain,
domains: func() map[string]*Translation {
m := make(map[string]*Translation)
for d, tr := range ts.domains {
m[d] = tr
}
return m
}(),
sourceCodeLocale: ts.sourceCodeLocale,
}
}
// BindFS load a Translation form file system and bind to a domain
// 从文件系统绑定翻译域
func (ts *Translations) BindFS(domain string, fsys fs.FS) {
tr := NewTranslation(domain)
if tr.LoadFS(fsys) {
ts.domains[domain] = tr
}
}
// Domain return current domain 返回当前使用的文本域
func (ts *Translations) Domain() string {
return ts.domain
}
// SetDomain set current domain 设置要使用的文本域
func (ts *Translations) SetDomain(domain string) {
ts.domain = domain
}
// HasDomain return true if ts has the specified domain
func (ts *Translations) HasDomain(domain string) bool {
for d := range ts.domains {
if d == domain {
return true
}
}
return false
}
// Domains return all domains
func (ts *Translations) Domains() (domains []string) {
for domain := range ts.domains {
domains = append(domains, domain)
}
return
}
// Locale return current locale 返回设置的希望使用的语言
func (ts *Translations) Locale() string {
return ts.locale
}
// Locales return all supported locales of domain 返回文本域中支持的所有语言
func (ts *Translations) Locales() (locales []string) {
tr := ts.GetOrNoop(ts.domain)
m := make(map[string]struct{}, len(tr.langs)+1)
m[ts.sourceCodeLocale] = struct{}{}
locales = append(locales, ts.sourceCodeLocale)
for lang := range tr.langs {
lang = locale.Normalize(lang)
if _, ok := m[lang]; !ok {
m[lang] = struct{}{}
locales = append(locales, lang)
}
}
sort.Strings(locales)
return
}
// MostMatchLocale return the most match language 返回最匹配的语言
func (ts *Translations) MostMatchLocale() string {
var supported = Locales()
_, index, _ := Match(supported, []string{Locale()})
return supported[index]
}
// UsedLocale return the locale that actually used
func (ts *Translations) UsedLocale() string {
tr, ok := ts.Get(ts.domain)
if !ok {
return ts.sourceCodeLocale
}
_, ok = tr.Get(ts.locale)
if !ok {
return ts.sourceCodeLocale
}
return ts.locale
}
// SetLocale set current locale 设置要使用的语言
func (ts *Translations) SetLocale(lang string) {
if lang == "" {
lang = locale.GetDefault()
} else {
lang = locale.Normalize(lang)
}
ts.locale = lang
}
// SourceCodeLocale 设置源代码语言
func (ts *Translations) SourceCodeLocale() string { return ts.sourceCodeLocale }
// SetSourceCodeLocale 设置源代码语言
func (ts *Translations) SetSourceCodeLocale(lang string) {
lang = locale.Normalize(lang)
ts.sourceCodeLocale = lang
}
// Get return the Translation of the specified domain
// 获取指定的的翻译域
func (ts *Translations) Get(domain string) (*Translation, bool) {
tr, ok := ts.domains[domain]
return tr, ok
}
// GetOrNoop return the Translation of the specified domain
// 获取指定的的翻译域
func (ts *Translations) GetOrNoop(domain string) *Translation {
if tr, ok := ts.domains[domain]; ok {
return tr
}
return trNoop
}
// D return a new Translations with domain
func (ts *Translations) D(domain string) *Translations {
result := ts.clone()
result.SetDomain(domain)
return result
}
// L return a new Translations with locale
func (ts *Translations) L(locale string) *Translations {
result := ts.clone()
result.SetLocale(locale)
return result
}
// T is a short name of gettext
func (ts *Translations) T(msgID string, args ...interface{}) string {
return ts.X("", msgID, args...)
}
// N is a short name of nettext
func (ts *Translations) N(msgID, msgIDPlural string, n int, args ...interface{}) string {
return ts.XN64("", msgID, msgIDPlural, int64(n), args...)
}
// N64 is a short name of nettext
func (ts *Translations) N64(msgID, msgIDPlural string, n int64, args ...interface{}) string {
return ts.XN64("", msgID, msgIDPlural, n, args...)
}
// X is a short name of pgettext
func (ts *Translations) X(msgCtxt, msgID string, args ...interface{}) string {
tr := ts.GetOrNoop(ts.domain)
tor := tr.GetOrNoop(ts.locale)
return tor.X(msgCtxt, msgID, args...)
}
// XN is a short name of npgettext
func (ts *Translations) XN(msgCtxt, msgID, msgIDPlural string, n int, args ...interface{}) string {
return ts.XN64(msgCtxt, msgID, msgIDPlural, int64(n), args...)
}
// XN64 is a short name of npgettext
func (ts *Translations) XN64(msgCtxt, msgID, msgIDPlural string, n int64, args ...interface{}) string {
tr := ts.GetOrNoop(ts.domain)
tor := tr.GetOrNoop(ts.locale)
return tor.XN64(msgCtxt, msgID, msgIDPlural, n, args...)
}