-
Notifications
You must be signed in to change notification settings - Fork 4
/
display.go
62 lines (51 loc) · 1.46 KB
/
display.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
package localizer
import (
"strings"
"golang.org/x/text/language"
)
const (
//{0} and {1} are the tokens to be replaced in the cldr locale display patterns
displayToken0 = "{0}"
displayToken1 = "{1}"
)
//DisplayName returns the formatted display name for the provided language tag in the locale.
func (l *Locale) DisplayName(tag language.Tag) string {
base, script, region := tag.Raw()
var haveScript, haveRegion bool
lang, ok := l.Languages[base.String()]
if !ok {
return base.String()
}
if region.String() != "ZZ" {
haveRegion = true
}
if script.String() != "Zzzz" {
haveScript = true
}
if !haveRegion && !haveScript {
return lang
}
var modifier string
if haveRegion && haveScript {
modifier = strings.ReplaceAll(l.DisplayPattern.Separator, displayToken0, script.String())
modifier = strings.ReplaceAll(modifier, displayToken1, region.String())
} else {
if haveRegion {
//special case - see if there's an l.Languages entry for base_Region
baseReg := strings.ReplaceAll(tag.String(), "-", "_")
if exact, ok := l.Languages[baseReg]; ok {
return exact
}
//didn't have the special case. get the region name from territories
if regionName, ok := l.Territories[region.String()]; ok {
modifier = regionName
}
}
if haveScript {
modifier = script.String()
}
}
pattern := strings.ReplaceAll(l.DisplayPattern.Pattern, displayToken0, lang)
pattern = strings.ReplaceAll(pattern, displayToken1, modifier)
return pattern
}