forked from goinggo/beego-mgo
/
messages.go
163 lines (135 loc) · 4.05 KB
/
messages.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
// The localize package provides support for handling different languages
// and cultures
package localize
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/goinggo/tracelog"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/nicksnyder/go-i18n/i18n/locale"
"github.com/nicksnyder/go-i18n/i18n/translation"
)
var (
// T is the translate function for the default locale
T i18n.TranslateFunc
)
// Init initializes the local environment
func Init(defaultLocale string) error {
tracelog.Startedf("localize", "Init", "DefaultLocal[%s]", defaultLocale)
switch defaultLocale {
case "en-US":
LoadJSON(defaultLocale, En_US)
default:
return fmt.Errorf("Unsupported Locale: %s", defaultLocale)
}
// Obtain the default translation function for use
var err error
T, err = NewTranslation(defaultLocale, defaultLocale)
if err != nil {
return err
}
tracelog.Completed("localize", "Init")
return nil
}
// NewTranslation obtains a translation function object for the
// specified locales
func NewTranslation(userLocale string, defaultLocale string) (t i18n.TranslateFunc, err error) {
t, err = i18n.Tfunc(userLocale, userLocale)
if err != nil {
return t, err
}
return t, err
}
// LoadJSON takes a json document of translations and manually
// loads them into the system
func LoadJSON(userLocale string, translationDocument string) error {
tracelog.Startedf("localize", "LoadJSON", "UserLocale[%s] Length[%d]", userLocale, len(translationDocument))
tranDocuments := []map[string]interface{}{}
err := json.Unmarshal([]byte(translationDocument), &tranDocuments)
if err != nil {
tracelog.CompletedErrorf(err, "localize", "LoadJSON", "**************>")
return err
}
for _, tranDocument := range tranDocuments {
tran, err := translation.NewTranslation(tranDocument)
if err != nil {
tracelog.CompletedError(err, "localize", "LoadJSON")
return err
}
i18n.AddTranslation(locale.MustNew(userLocale), tran)
}
tracelog.Completed("localize", "LoadJSON")
return nil
}
// LoadFiles looks for i18n folders inside the current directory and the GOPATH
// to find translation files to load
func LoadFiles(userLocale string, defaultLocal string) error {
gopath := os.Getenv("GOPATH")
pwd, err := os.Getwd()
if err != nil {
tracelog.CompletedError(err, "localize", "LoadFiles")
return err
}
tracelog.Info("localize", "LoadFiles", "PWD[%s] GOPATH[%s]", pwd, gopath)
// Load any translation files we can find
searchDirectory(pwd, pwd)
if gopath != "" {
searchDirectory(gopath, pwd)
}
// Create a translation function for use
T, err = i18n.Tfunc(userLocale, defaultLocal)
if err != nil {
return err
}
return nil
}
// searchDirectory recurses through the specified directory looking
// for i18n folders. If found it will load the translations files
func searchDirectory(directory string, pwd string) {
// Read the directory
fileInfos, err := ioutil.ReadDir(directory)
if err != nil {
tracelog.CompletedError(err, "localize", "searchDirectory")
return
}
// Look for i18n folders
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() == true {
fullPath := fmt.Sprintf("%s/%s", directory, fileInfo.Name())
// If this directory is the current directory, ignore it
if fullPath == pwd {
continue
}
// Is this an i18n folder
if fileInfo.Name() == "i18n" {
loadTranslationFiles(fullPath)
continue
}
// Look for more sub-directories
searchDirectory(fullPath, pwd)
continue
}
}
}
// loadTranslationFiles loads the found translation files into the i18n
// messaging system for use by the application
func loadTranslationFiles(directory string) {
// Read the directory
fileInfos, err := ioutil.ReadDir(directory)
if err != nil {
tracelog.CompletedError(err, "localize", "loadTranslationFiles")
return
}
// Look for JSON files
for _, fileInfo := range fileInfos {
if path.Ext(fileInfo.Name()) != ".json" {
continue
}
fileName := fmt.Sprintf("%s/%s", directory, fileInfo.Name())
tracelog.Info("localize", "loadTranslationFiles", "Loading %s", fileName)
i18n.MustLoadTranslationFile(fileName)
}
}