-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.go
64 lines (53 loc) · 1.23 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package laravalidate
import (
"strings"
"golang.org/x/text/language"
)
func lookupLanguages(languages []language.Tag) []string {
languageStrs := []string{}
outer:
for _, lang := range languages {
langStr := strings.ToLower(lang.String())
langParts := strings.SplitN(langStr, "-", 2)
if len(langParts) == 1 {
if len(languageStrs) == 0 {
languageStrs = append(languageStrs, langStr)
continue
}
for _, existingLang := range languageStrs {
if existingLang == langStr {
continue outer
}
}
languageStrs = append(languageStrs, langStr)
}
alternativeLang := langParts[0]
if len(languageStrs) == 0 {
languageStrs = append(languageStrs, langStr, alternativeLang)
continue
}
addLang := true
addAlternativeLang := true
for _, existingLang := range languageStrs {
if existingLang == langStr {
addLang = false
}
if existingLang == alternativeLang {
addAlternativeLang = false
}
if !addLang && !addAlternativeLang {
continue outer
}
}
if addLang {
languageStrs = append(languageStrs, langStr)
}
if addAlternativeLang {
languageStrs = append(languageStrs, alternativeLang)
}
}
if len(languageStrs) == 0 {
return []string{"en"}
}
return languageStrs
}