forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
134 lines (112 loc) · 3.41 KB
/
i18n.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
package ui
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"
"golang.org/x/text/language"
"code.cloudfoundry.org/cli/cf/resources"
)
const (
// assetPath is the path of the translation file inside the asset loader.
assetPath = "cf/i18n/resources/%s.all.json"
// chineseBase is the language code for Chinese.
chineseBase = "zh"
// defaultLocale is the default locale used when one is not configured.
defaultLocale = "en-us"
// unspecifiedScript is what is returned by language#Script objects when the
// script cannot be determined.
unspecifiedScript = "Zzzz"
)
type LocaleReader interface {
Locale() string
}
// TranslationEntry is the expected format of the translation file.
type TranslationEntry struct {
// ID is the original English string.
ID string `json:"id"`
// Translation is the translation of the ID.
Translation string `json:"translation"`
}
// TranslateFunc returns the translation of the string identified by
// translationID.
//
// If there is no translation for translationID, then the translationID is used
// as the translation.
type TranslateFunc func(translationID string, args ...interface{}) string
// GetTranslationFunc will return back a function that can be used to translate
// strings into the currently set locale.
func GetTranslationFunc(reader LocaleReader) (TranslateFunc, error) {
locale, err := determineLocale(reader)
if err != nil {
locale = defaultLocale
}
rawTranslation, err := loadAssetFromResources(locale)
if err != nil {
rawTranslation, err = loadAssetFromResources(defaultLocale)
if err != nil {
return nil, err
}
}
return generateTranslationFunc(rawTranslation)
}
// ParseLocale will return a locale formatted as "<language code>-<region
// code>" for all non-Chinese lanagues. For Chinese, it will return
// "zh-<script>", defaulting to "hant" if script is unspecified.
func ParseLocale(locale string) (string, error) {
lang, err := language.Parse(locale)
if err != nil {
return "", err
}
base, script, region := lang.Raw()
switch base.String() {
case chineseBase:
if script.String() == unspecifiedScript {
return "zh-hant", nil
}
return strings.ToLower(fmt.Sprintf("%s-%s", base, script)), nil
default:
return strings.ToLower(fmt.Sprintf("%s-%s", base, region)), nil
}
}
func determineLocale(reader LocaleReader) (string, error) {
locale := reader.Locale()
if locale == "" {
return defaultLocale, nil
}
return ParseLocale(locale)
}
func generateTranslationFunc(rawTranslation []byte) (TranslateFunc, error) {
var entries []TranslationEntry
err := json.Unmarshal(rawTranslation, &entries)
if err != nil {
return nil, err
}
translations := map[string]string{}
for _, entry := range entries {
translations[entry.ID] = entry.Translation
}
return func(translationID string, args ...interface{}) string {
translated := translations[translationID]
if translated == "" {
translated = translationID
}
var keys interface{}
if len(args) > 0 {
keys = args[0]
}
var buffer bytes.Buffer
formattedTemplate := template.Must(template.New("Display Text").Parse(translated))
formattedTemplate.Execute(&buffer, keys)
return buffer.String()
}, nil
}
func loadAssetFromResources(locale string) ([]byte, error) {
assetName := fmt.Sprintf(assetPath, locale)
assetBytes, err := resources.Asset(assetName)
if err != nil {
err = fmt.Errorf("Could not load asset '%s': %s", assetName, err.Error())
}
return assetBytes, err
}