-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
58 lines (48 loc) · 1.33 KB
/
functions.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
package localization
import (
"fmt"
"strings"
"github.com/cygy/ginamite/common/log"
"github.com/nicksnyder/go-i18n/i18n/bundle"
"github.com/nicksnyder/go-i18n/i18n/language"
"github.com/sirupsen/logrus"
)
// Initialize : initalizes the translation engine.
func (e *Engine) Initialize(path string, locales []string) {
for _, locale := range locales {
filePath := fmt.Sprintf("%s/%s.all.json", path, strings.ToLower(locale))
bundle := bundle.New()
bundle.MustLoadTranslationFile(filePath)
if translate, err := bundle.Tfunc(locale); err != nil {
log.WithFields(logrus.Fields{
"path": filePath,
"error": err.Error(),
}).Panic("unable to load translation file")
} else {
e.TranslateFuncs[locale] = translate
}
for locale, mapTranslate := range bundle.Translations() {
e.Translations[locale] = mapTranslate
}
}
}
// Translate : returns the right translate function.
func (e *Engine) Translate(locale string) bundle.TranslateFunc {
translateFunc, ok := e.TranslateFuncs[locale]
if !ok {
return nil
}
return translateFunc
}
// Source : returns the raw source string.
func (e *Engine) Source(locale, key string) string {
translationMap, ok := e.Translations[locale]
if !ok {
return ""
}
translation, ok := translationMap[key]
if !ok {
return ""
}
return translation.Template(language.Other).String()
}