-
Notifications
You must be signed in to change notification settings - Fork 2
/
iso6391.go
91 lines (75 loc) · 1.96 KB
/
iso6391.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package iso6391
// FromCode returns the language for given code.
func FromCode(code string) Language {
return Languages[code]
}
// FromName returns the language for given name.
func FromName(name string) Language {
for _, lang := range Languages {
if lang.Name == name {
return lang
}
}
return Language{}
}
// FromNativeName returns the language for given native name.
func FromNativeName(name string) Language {
for _, lang := range Languages {
if lang.NativeName == name {
return lang
}
}
return Language{}
}
// Name returns the language name in english for given code.
func Name(code string) string {
return FromCode(code).Name
}
// NativeName returns the language native name for given code.
func NativeName(code string) string {
return FromCode(code).NativeName
}
// CodeForName returns the language code for given name.
func CodeForName(name string) string {
for code, lang := range Languages {
if lang.Name == name {
return code
}
}
return ""
}
// CodeForNativeName returns the language code for given native name.
func CodeForNativeName(name string) string {
for code, lang := range Languages {
if lang.NativeName == name {
return code
}
}
return ""
}
// ValidCode returns true if the given code is a valid ISO 639-1 language code.
// The code must be passed in lowercase.
func ValidCode(code string) bool {
_, ok := Languages[code]
return ok
}
// ValidName returns true if the given name is a valid ISO 639-1 language name.
// The name must use uppercase characters (e.g. English, Hiri Motu, ...).
func ValidName(name string) bool {
for _, lang := range Languages {
if lang.Name == name {
return true
}
}
return false
}
// ValidNativeName returns true if the given code is a valid ISO 639-1 language native name.
// The name must be passed in its native form (e.g. English, 中文, ...).
func ValidNativeName(name string) bool {
for _, lang := range Languages {
if lang.NativeName == name {
return true
}
}
return false
}