-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
138 lines (111 loc) · 2.85 KB
/
init.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
package i18n
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/pivotal-cf-experimental/jibber_jabber"
resources "github.com/cloudfoundry/cli/cf/resources"
go_i18n "github.com/nicksnyder/go-i18n/i18n"
)
const (
DEFAULT_LOCALE = "en_US"
DEFAULT_LANGUAGE = "en"
)
var SUPPORTED_LOCALES = map[string]string{
"de": "de_DE",
"en": "en_US",
"es": "es_ES",
"fr": "fr_FR",
"it": "it_IT",
"ja": "ja_JA",
"ko": "ko_KO",
"pt": "pt_BR",
"ru": "ru_RU",
"zh": "zh_CN",
}
var resources_path = filepath.Join("cf", "i18n", "resources")
func GetResourcesPath() string {
return resources_path
}
func Init(packageName string, i18nDirname string) go_i18n.TranslateFunc {
userLocale, err := initWithUserLocale(packageName, i18nDirname)
if err != nil {
userLocale = mustLoadDefaultLocale(packageName, i18nDirname)
}
T, err := go_i18n.Tfunc(userLocale, DEFAULT_LOCALE)
if err != nil {
panic(err)
}
return T
}
func initWithUserLocale(packageName, i18nDirname string) (string, error) {
userLocale, err := jibber_jabber.DetectIETF()
if err != nil {
userLocale = DEFAULT_LOCALE
}
language, err := jibber_jabber.DetectLanguage()
if err != nil {
language = DEFAULT_LANGUAGE
}
userLocale = strings.Replace(userLocale, "-", "_", 1)
err = loadFromAsset(packageName, i18nDirname, userLocale, language)
if err != nil {
locale := SUPPORTED_LOCALES[language]
if locale == "" {
userLocale = DEFAULT_LOCALE
} else {
userLocale = locale
}
err = loadFromAsset(packageName, i18nDirname, userLocale, language)
}
return userLocale, err
}
func mustLoadDefaultLocale(packageName, i18nDirname string) string {
userLocale := DEFAULT_LOCALE
err := loadFromAsset(packageName, i18nDirname, DEFAULT_LOCALE, DEFAULT_LANGUAGE)
if err != nil {
panic("Could not load en_US language files. God save the queen. " + err.Error())
}
return userLocale
}
func loadFromAsset(packageName, assetPath, locale, language string) error {
assetName := locale + ".all.json"
assetKey := filepath.Join(assetPath, language, packageName, assetName)
byteArray, err := resources.Asset(assetKey)
if err != nil {
return err
}
if len(byteArray) == 0 {
return errors.New(fmt.Sprintf("Could not load i18n asset: %v", assetKey))
}
tmpDir, err := ioutil.TempDir("", "cloudfoundry_cli_i18n_res")
if err != nil {
return err
}
defer func() {
os.RemoveAll(tmpDir)
}()
fileName, err := saveLanguageFileToDisk(tmpDir, assetName, byteArray)
if err != nil {
return err
}
go_i18n.MustLoadTranslationFile(fileName)
os.RemoveAll(fileName)
return nil
}
func saveLanguageFileToDisk(tmpDir, assetName string, byteArray []byte) (fileName string, err error) {
fileName = filepath.Join(tmpDir, assetName)
file, err := os.Create(fileName)
if err != nil {
return
}
defer file.Close()
_, err = file.Write(byteArray)
if err != nil {
return
}
return
}