-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.go
55 lines (44 loc) · 1.21 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
package i18n
import (
"path"
"strings"
"github.com/cloudfoundry/cli/cf/resources"
"github.com/nicksnyder/go-i18n/i18n/language"
)
func SupportedLocales() []string {
languages := supportedLanguages()
localeNames := make([]string, len(languages))
for i, l := range languages {
localeParts := strings.Split(l.String(), "-")
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.String()
}
}
return localeNames
}
func IsSupportedLocale(locale string) bool {
for _, supportedLanguage := range supportedLanguages() {
for _, l := range language.Parse(locale) {
if supportedLanguage.String() == l.String() {
return true
}
}
}
return false
}
func supportedLanguages() []*language.Language {
assetNames := resources.AssetNames()
languages := []*language.Language{}
for _, assetName := range assetNames {
assetLocale := strings.TrimSuffix(path.Base(assetName), resourceSuffix)
languages = append(languages, language.Parse(assetLocale)...)
}
return languages
}