-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.go
44 lines (37 loc) · 1.13 KB
/
code.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
package i18n
import "strings"
// Code is used to represent a language code which follows the
// ISO 639-1 standard, with sub-tags aggregated with hyphens,
// as defined in [RFC5646](https://datatracker.ietf.org/doc/html/rfc5646).
// Examples include:
//
// fr, en-US, es-419, az-Arab, x-pig-latin, man-Nkoo-GN
type Code string
// String returns the string variant of the code.
func (c Code) String() string {
return string(c)
}
// Base returns the base language code, without any subtags.
func (c Code) Base() Code {
out := strings.SplitN(c.String(), "-", 2)
if len(out) > 0 {
return Code(out[0])
}
return ""
}
// ParseAcceptLanguage provides an ordered set of codes extracted
// from an HTTP "Accept-Language" header as defined in RFC9110.
// Current implementation will ignore quality values and instead
// just assume the order of the provided codes is valid.
func ParseAcceptLanguage(txt string) []Code {
list := make([]Code, 0)
for _, s := range strings.Split(txt, ",") {
s = strings.TrimSpace(s)
// Remove any quality values.
if i := strings.Index(s, ";"); i > 0 {
s = s[:i]
}
list = append(list, Code(s))
}
return list
}