-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage.go
More file actions
104 lines (83 loc) · 2.71 KB
/
Copy pathlanguage.go
File metadata and controls
104 lines (83 loc) · 2.71 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
package customerror
import (
"regexp"
"strings"
"sync"
)
//////
// Consts, vars, and types.
//////
// Built-in language codes (ISO 639-1) supported out of the box.
const (
Chinese Language = "zh"
English Language = "en"
French Language = "fr"
German Language = "de"
Italian Language = "it"
Portuguese Language = "pt"
Spanish Language = "es"
)
var (
// ErrInvalidLanguageCode is returned when a language code is invalid.
ErrInvalidLanguageCode = NewInvalidError("it must be a string, two-letter lowercase ISO 639-1 code OR two-letter lowercase ISO 639-1 code followed by an optional hyphen AND a two-letter uppercase ISO 3166-1 alpha-2 country code", WithErrorCode("CE_ERR_INVALID_LANG_CODE"))
// ErrInvalidLanguageErrorMessage is returned when an error message is invalid.
ErrInvalidLanguageErrorMessage = NewInvalidError("it must be a string, at least 3 characters long", WithErrorCode("CE_ERR_INVALID_LANG_ERROR_MESSAGE"))
// ErrInvalidLanguageMessageMap is returned when a LanguageMessageMap is
// invalid.
ErrInvalidLanguageMessageMap = NewInvalidError("it must be a non-nil map of language codes to error messages", WithErrorCode("CE_ERR_INVALID_LANGUAGE_MESSAGE_MAP"))
// BuiltInLanguages is a list of built-in prefixes languages.
BuiltInLanguages = []string{
Chinese.String(),
English.String(),
French.String(),
German.String(),
Italian.String(),
Portuguese.String(),
Spanish.String(),
}
// LanguageRegex is a regular expression to validate language codes based on
// ISO 639-1 and ISO 3166-1 alpha-2. The special value "default" is also
// accepted. Every alternative is fully anchored, so partial matches such as
// "mydefault" are correctly rejected.
LanguageRegex = regexp.MustCompile("^[a-z]{2}(-[A-Z]{2})?$|^default$")
)
type (
// Language is a language code.
Language string
// LanguageMessageMap is a map of language codes to error messages.
LanguageMessageMap = *sync.Map
)
//////
// Factory.
//////
// NewLanguage creates a new Lang.
func NewLanguage(lang string) (Language, error) {
l := Language(lang)
err := l.Validate()
if err != nil {
return "", err
}
return l, nil
}
//////
// Methods.
//////
// String implements the Stringer interface.
func (l Language) String() string {
return string(l)
}
// Validate if lang follows the ISO 639-1 and ISO 3166-1 alpha-2 standard.
func (l Language) Validate() error {
if !LanguageRegex.MatchString(l.String()) {
return ErrInvalidLanguageCode
}
return nil
}
// GetRoot returns the root language code. Given "en-US", it returns "en".
func (l Language) GetRoot() string {
matches := LanguageRegex.FindStringSubmatch(l.String())
if len(matches) > 1 {
return strings.ReplaceAll(matches[0], matches[1], "")
}
return ""
}