forked from goinggo/beego-mgo
/
bundle.go
178 lines (154 loc) · 4.29 KB
/
bundle.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 bundle manages translations for multiple locales.
package bundle
import (
"encoding/json"
"fmt"
"io/ioutil"
// "launchpad.net/goyaml"
"github.com/goinggo/beego-mgo/go-i18n/i18n/locale"
"github.com/goinggo/beego-mgo/go-i18n/i18n/translation"
"path/filepath"
)
// TranslateFunc is a copy of i18n.TranslateFunc to avoid a circular dependency.
type TranslateFunc func(translationID string, args ...interface{}) string
type Bundle struct {
translations map[string]map[string]translation.Translation
}
func New() *Bundle {
return &Bundle{
translations: make(map[string]map[string]translation.Translation),
}
}
func (b *Bundle) MustLoadTranslationFile(filename string) {
if err := b.LoadTranslationFile(filename); err != nil {
panic(err)
}
}
func (b *Bundle) LoadTranslationFile(filename string) error {
locale, err := locale.New(filename)
if err != nil {
return err
}
translations, err := parseTranslationFile(filename)
if err != nil {
return err
}
b.AddTranslation(locale, translations...)
return nil
}
func parseTranslationFile(filename string) ([]translation.Translation, error) {
var unmarshalFunc func([]byte, interface{}) error
switch format := filepath.Ext(filename); format {
case ".json":
unmarshalFunc = json.Unmarshal
/*
case ".yaml":
unmarshalFunc = goyaml.Unmarshal
*/
default:
return nil, fmt.Errorf("unsupported file extension %s", format)
}
fileBytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var translationsData []map[string]interface{}
if len(fileBytes) > 0 {
if err := unmarshalFunc(fileBytes, &translationsData); err != nil {
return nil, err
}
}
translations := make([]translation.Translation, 0, len(translationsData))
for i, translationData := range translationsData {
t, err := translation.NewTranslation(translationData)
if err != nil {
return nil, fmt.Errorf("unable to parse translation #%d in %s because %s\n%v", i, filename, err, translationData)
}
translations = append(translations, t)
}
return translations, nil
}
func (b *Bundle) AddTranslation(locale *locale.Locale, translations ...translation.Translation) {
if b.translations[locale.ID] == nil {
b.translations[locale.ID] = make(map[string]translation.Translation, len(translations))
}
currentTranslations := b.translations[locale.ID]
for _, newTranslation := range translations {
if currentTranslation := currentTranslations[newTranslation.ID()]; currentTranslation != nil {
currentTranslations[newTranslation.ID()] = currentTranslation.Merge(newTranslation)
} else {
currentTranslations[newTranslation.ID()] = newTranslation
}
}
}
func (b *Bundle) Translations() map[string]map[string]translation.Translation {
return b.translations
}
func (b *Bundle) MustTfunc(localeID string, localeIDs ...string) TranslateFunc {
tf, err := b.Tfunc(localeID, localeIDs...)
if err != nil {
panic(err)
}
return tf
}
func (b *Bundle) Tfunc(localeID string, localeIDs ...string) (tf TranslateFunc, err error) {
var l *locale.Locale
l, err = locale.New(localeID)
if err != nil {
for _, localeID := range localeIDs {
l, err = locale.New(localeID)
if err == nil {
break
}
}
}
return func(translationID string, args ...interface{}) string {
return b.translate(l, translationID, args...)
}, err
}
func (b *Bundle) translate(locale *locale.Locale, translationID string, args ...interface{}) string {
if locale == nil {
return translationID
}
translations := b.translations[locale.ID]
if translations == nil {
return translationID
}
translation := translations[translationID]
if translation == nil {
return translationID
}
var count interface{}
if len(args) > 0 && isNumber(args[0]) {
count = args[0]
args = args[1:]
}
pluralCategory, _ := locale.Language.PluralCategory(count)
template := translation.Template(pluralCategory)
if template == nil {
return translationID
}
var data map[string]interface{}
if len(args) > 0 {
data, _ = args[0].(map[string]interface{})
}
if isNumber(count) {
if data == nil {
data = map[string]interface{}{"Count": count}
} else {
data["Count"] = count
}
}
s := template.Execute(data)
if s == "" {
return translationID
}
return s
}
func isNumber(n interface{}) bool {
switch n.(type) {
case int, int8, int16, int32, int64, string:
return true
}
return false
}