forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
167 lines (136 loc) · 3.38 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
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
package i18n
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/i18n/detection"
resources "github.com/cloudfoundry/cli/cf/resources"
go_i18n "github.com/nicksnyder/go-i18n/i18n"
)
const (
DEFAULT_LOCALE = "en_US"
DEFAULT_LANGUAGE = "en"
)
var T go_i18n.TranslateFunc
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", - Will add support for Korean when nicksnyder/go-i18n supports Korean
"pt": "pt_BR",
//"ru": "ru_RU", - Will add support for Russian when nicksnyder/go-i18n supports Russian
"zh": "zh_Hans",
}
var Resources_path = filepath.Join("cf", "i18n", "resources")
func GetResourcesPath() string {
return Resources_path
}
func Init(config core_config.ReadWriter, detector detection.Detector) go_i18n.TranslateFunc {
var T go_i18n.TranslateFunc
var err error
locale := config.Locale()
if locale != "" {
err = loadFromAsset(locale)
if err == nil {
T, err = go_i18n.Tfunc(config.Locale(), DEFAULT_LOCALE)
}
} else {
var userLocale string
userLocale, err = initWithUserLocale(detector)
if err != nil {
userLocale = mustLoadDefaultLocale()
}
T, err = go_i18n.Tfunc(userLocale, DEFAULT_LOCALE)
}
if err != nil {
panic(err)
}
return T
}
func initWithUserLocale(detector detection.Detector) (string, error) {
userLocale, err := detector.DetectIETF()
if err != nil {
userLocale = DEFAULT_LOCALE
}
language, err := detector.DetectLanguage()
if err != nil {
language = DEFAULT_LANGUAGE
}
userLocale = strings.Replace(userLocale, "-", "_", 1)
if strings.HasPrefix(userLocale, "zh_TW") || strings.HasPrefix(userLocale, "zh_HK") {
userLocale = "zh_Hant"
language = "zh"
}
err = loadFromAsset(userLocale)
if err != nil {
locale := SUPPORTED_LOCALES[language]
if locale == "" {
userLocale = DEFAULT_LOCALE
} else {
userLocale = locale
}
err = loadFromAsset(userLocale)
}
return userLocale, err
}
func mustLoadDefaultLocale() string {
userLocale := DEFAULT_LOCALE
err := loadFromAsset(DEFAULT_LOCALE)
if err != nil {
panic("Could not load en_US language files. \n" + err.Error() + "\n\n")
}
return userLocale
}
func loadFromAsset(locale string) error {
assetName := locale + ".all.json"
assetKey := filepath.Join(GetResourcesPath(), 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))
}
_, err = os.Stat(os.TempDir())
if err != nil {
if !os.IsExist(err) {
return errors.New("Please make sure Temp dir exist - " + os.TempDir())
} else {
return err
}
}
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
}