-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.go
61 lines (48 loc) · 1.24 KB
/
locale.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
package i18n
import (
"path"
"strings"
"code.cloudfoundry.org/cli/i18n/resources"
"code.cloudfoundry.org/cli/util/ui"
)
const resourceSuffix = ".all.json"
func SupportedLocales() []string {
languages := supportedLanguages()
localeNames := make([]string, len(languages))
for i, l := range languages {
localeParts := strings.Split(l, "-")
lang := localeParts[0]
regionOrScript := localeParts[1]
switch len(regionOrScript) {
case 2: // Region
localeNames[i] = lang + "-" + strings.ToUpper(regionOrScript)
case 4: // Script
localeNames[i] = lang + "-" + strings.Title(regionOrScript)
default:
localeNames[i] = l
}
}
return localeNames
}
func IsSupportedLocale(locale string) bool {
sanitizedLocale, err := ui.ParseLocale(locale)
if err != nil {
return false
}
for _, supportedLanguage := range supportedLanguages() {
if supportedLanguage == sanitizedLocale {
return true
}
}
return false
}
func supportedLanguages() []string {
assetNames := resources.AssetNames()
var languages []string
for _, assetName := range assetNames {
assetLocale := strings.TrimSuffix(path.Base(assetName), resourceSuffix)
locale, _ := ui.ParseLocale(assetLocale)
languages = append(languages, locale)
}
return languages
}