-
Notifications
You must be signed in to change notification settings - Fork 3
/
locale.go
76 lines (66 loc) · 1.67 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package address
import (
"strings"
"unicode"
"unicode/utf8"
)
// Locale represents a Unicode locale identifier.
type Locale struct {
Language string
Script string
Territory string
}
// NewLocale creates a new Locale from its string representation.
func NewLocale(id string) Locale {
// Normalize the ID ("SR_rs_LATN" => "sr-Latn-RS").
id = strings.ToLower(id)
id = strings.ReplaceAll(id, "_", "-")
locale := Locale{}
for i, part := range strings.Split(id, "-") {
if i == 0 {
locale.Language = part
continue
}
partLen := len(part)
if partLen == 4 {
// Uppercase the first letter in a UTF8-safe manner.
r, size := utf8.DecodeRuneInString(part)
locale.Script = string(unicode.ToTitle(r)) + part[size:]
continue
}
if partLen == 2 || partLen == 3 {
locale.Territory = strings.ToUpper(part)
continue
}
}
return locale
}
// String returns the string representation of l.
func (l Locale) String() string {
b := strings.Builder{}
b.WriteString(l.Language)
if l.Script != "" {
b.WriteString("-")
b.WriteString(l.Script)
}
if l.Territory != "" {
b.WriteString("-")
b.WriteString(l.Territory)
}
return b.String()
}
// MarshalText implements the encoding.TextMarshaler interface.
func (l Locale) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (l *Locale) UnmarshalText(b []byte) error {
*l = NewLocale(string(b))
return nil
}
// IsEmpty returns whether l is empty.
func (l Locale) IsEmpty() bool {
return l.Language == "" && l.Script == "" && l.Territory == ""
}