-
Notifications
You must be signed in to change notification settings - Fork 19
/
language.go
43 lines (34 loc) · 1006 Bytes
/
language.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
package utils
import (
"fmt"
"golang.org/x/text/language"
)
// Language is our internal representation of a language
type Language string
// NilLanguage represents our nil, or unknown language
var NilLanguage = Language("")
// ParseLanguage returns a new Language for the passed in language string, or an error if not found
func ParseLanguage(lang string) (Language, error) {
if len(lang) != 3 {
return NilLanguage, fmt.Errorf("iso-639-3 codes must be 3 characters, got: %s", lang)
}
base, err := language.ParseBase(lang)
if err != nil {
return NilLanguage, err
}
return Language(base.ISO3()), nil
}
// LanguageList is a list of languages
type LanguageList []Language
// RemoveDuplicates returns a new language list with duplicates removed
func (ll LanguageList) RemoveDuplicates() LanguageList {
result := LanguageList{}
seen := map[Language]bool{}
for _, val := range ll {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = true
}
}
return result
}