-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.go
36 lines (31 loc) · 971 Bytes
/
format.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
package languagecode
// Format represents a specific language code format with a specific
// serialization.
type Format int
const (
// FormatAlpha3 is an ISO-639-2 language code.
FormatAlpha3 Format = iota
// FormatAlpha3B is an ISO-639-2/B language code.
FormatAlpha3B
// FormatAlpha2 is an ISO-639-1 language code.
FormatAlpha2
formatsCount
)
// Serialize the specified Language into a language code string of the Format.
func (f Format) Serialize(language Language) string {
return codes[language.code][f]
}
// Deserialize the specified language code string of the Format into a
// Language.
func (f Format) Deserialize(languageCode string) Language {
return languages[f][languageCode]
}
var languages = func() (l [formatsCount]map[string]Language) {
for f := Format(0); f < formatsCount; f++ {
l[f] = make(map[string]Language, len(codes))
for j, languageCodes := range codes {
l[f][languageCodes[f]] = Language{code: code(j)}
}
}
return
}()