-
Notifications
You must be signed in to change notification settings - Fork 8
/
lang.go
55 lines (47 loc) · 1.48 KB
/
lang.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
package i18n
import (
"github.com/invopop/jsonschema"
"github.com/invopop/validation"
)
// To simplify language management GoBL does not support full localization
// and instead focusses on simple multi-language based on the ISO 639-1 set
// of two letter codes. For business documents, this is sufficient as they
// are generally issued in a given country context.
// Lang represents the two letter language code.
type Lang string
// LangDef serves to handle language definitions
type LangDef struct {
// Language Code
Code Lang `json:"code" jsonschema:"title=Code"`
// English name of the language
Name string `json:"name" jsonschema:"title=Name"`
}
var isLang = validation.In(validLang()...)
func validLang() []interface{} {
list := make([]interface{}, len(LangDefinitions))
for i, d := range LangDefinitions {
list[i] = string(d.Code)
}
return list
}
// Validate ensures the language code is valid according
// to the ISO 639-1 two-letter list.
func (l Lang) Validate() error {
return validation.Validate(string(l), isLang)
}
// JSONSchema provides a representation of the struct for usage in Schema.
func (Lang) JSONSchema() *jsonschema.Schema {
s := &jsonschema.Schema{
Title: "Language Code",
Type: "string",
OneOf: make([]*jsonschema.Schema, len(LangDefinitions)),
Description: "Identifies the ISO639-1 language code",
}
for i, v := range LangDefinitions {
s.OneOf[i] = &jsonschema.Schema{
Const: v.Code,
Description: v.Name,
}
}
return s
}