Debian Codegraph @

keyfile/locale.go

raw
1 package keyfile
2
3 import (
4 "bytes"
5 "errors"
6 "os"
7 )
8
9 // Locale represents a locale for use in parsing translatable strings.
10 type Locale struct {
11 lang string
12 country string
13 encoding string
14 modifier string
15 }
16
17 var defaultLocale *Locale
18
19 // DefaultLocale returns the locale specified by the environment.
20 func DefaultLocale() Locale {
21 if defaultLocale == nil {
22 val := os.Getenv("LANGUAGE")
23 if val == "" {
24 val = os.Getenv("LC_ALL")
25 if val == "" {
26 val = os.Getenv("LC_MESSAGES")
27 if val == "" {
28 val = os.Getenv("LANG")
29 }
30 }
31 }
32
33 l, err := ParseLocale(val)
34 if err == nil {
35 defaultLocale = &l
36 } else {
37 defaultLocale = &Locale{}
38 }
39 }
40 return *defaultLocale
41 }
42
43 // ErrBadLocaleFormat is returned by ParseLocale when the given string
44 // is not formatted properly (for example, missing the lang component).
45 var ErrBadLocaleFormat = errors.New("bad locale format")
46
47 // ParseLocale parses a locale in the format
48 //
49 // lang_COUNTRY.ENCODING@MODIFIER
50 //
51 // where "_COUNTRY", ".ENCODING", and "@MODIFIER" can be omitted. A
52 // blank string, "C", and "POSIX" are special cases that evaluate to a
53 // blank Locale.
54 func ParseLocale(s string) (Locale, error) {
55 // A blank string, "C", and "POSIX" are valid locales, they
56 // evaluate to a blank Locale.
57 if s == "" || s == "C" || s == "POSIX" {
58 return Locale{}, nil
59 }
60
61 var buf bytes.Buffer
62 var l Locale
63
64 i := 0
65
66 // lang
67 for i < len(s) && s[i] != '_' && s[i] != '.' && s[i] != '@' {
68 buf.WriteByte(s[i])
69 i++
70 }
71 l.lang = buf.String()
72 buf.Reset()
73
74 // lang is required.
75 if l.lang == "" {
76 return Locale{}, ErrBadLocaleFormat
77 }
78
79 // COUNTRY
80 if i < len(s) && s[i] == '_' {
81 i++
82 for i < len(s) && s[i] != '.' && s[i] != '@' {
83 buf.WriteByte(s[i])
84 i++
85 }
86 l.country = buf.String()
87 buf.Reset()
88 }
89
90 // ENCODING
91 if i < len(s) && s[i] == '.' {
92 i++
93 for i < len(s) && s[i] != '@' {
94 buf.WriteByte(s[i])
95 i++
96 }
97 l.encoding = buf.String()
98 buf.Reset()
99 }
100
101 // MODIFIER
102 if i < len(s) && s[i] == '@' {
103 i++
104 for i < len(s) {
105 buf.WriteByte(s[i])
106 i++
107 }
108 l.modifier = buf.String()
109 }
110
111 return l, nil
112 }
113
114 // String returns the given locale as a formatted string. The returned
115 // string is in the same format expected by ParseLocale.
116 func (l Locale) String() string {
117 var buf bytes.Buffer
118
119 buf.WriteString(l.lang)
120
121 if l.country != "" {
122 buf.WriteRune('_')
123 buf.WriteString(l.country)
124 }
125
126 if l.encoding != "" {
127 buf.WriteRune('.')
128 buf.WriteString(l.encoding)
129 }
130
131 if l.modifier != "" {
132 buf.WriteRune('@')
133 buf.WriteString(l.modifier)
134 }
135
136 return buf.String()
137 }
138
139 // Variants returns a sorted slice of Locales that should be checked for
140 // when resolving a localestring.
141 func (l Locale) Variants() []Locale {
142 variants := make([]Locale, 0, 4)
143
144 hasLang := l.lang != ""
145 hasCountry := l.country != ""
146 hasModifier := l.modifier != ""
147
148 if hasLang && hasCountry && hasModifier {
149 variants = append(variants, Locale{
150 lang: l.lang,
151 country: l.country,
152 modifier: l.modifier,
153 })
154 }
155
156 if hasLang && hasCountry {
157 variants = append(variants, Locale{
158 lang: l.lang,
159 country: l.country,
160 })
161 }
162
163 if hasLang && hasModifier {
164 variants = append(variants, Locale{
165 lang: l.lang,
166 modifier: l.modifier,
167 })
168 }
169
170 if hasLang {
171 variants = append(variants, Locale{
172 lang: l.lang,
173 })
174 }
175
176 return variants
177 }