@@ -2,11 +2,13 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/go-playground/locales/zh"
5
6
6
7
"github.com/go-playground/locales/en"
7
8
ut "github.com/go-playground/universal-translator"
8
9
"github.com/go-playground/validator/v10"
9
10
en_translations "github.com/go-playground/validator/v10/translations/en"
11
+ zh_translations "github.com/go-playground/validator/v10/translations/zh"
10
12
)
11
13
12
14
// User contains user information
@@ -38,15 +40,20 @@ func main() {
38
40
// NOTE: omitting allot of error checking for brevity
39
41
40
42
en := en .New ()
41
- uni = ut .New (en , en )
43
+ uni = ut .New (en , en , zh . New () )
42
44
43
- // this is usually know or extracted from http 'Accept-Language' header
45
+ validate = validator .New ()
46
+ // The supported locales need to be predefined.
47
+ enTrans , _ := uni .GetTranslator ("en" )
48
+ en_translations .RegisterDefaultTranslations (validate , enTrans )
49
+ zhTrans , _ := uni .GetTranslator ("zh" )
50
+ zh_translations .RegisterDefaultTranslations (validate , zhTrans )
51
+
52
+ // this is usually know or extracted from http 'Accept-Language' header e.g.
53
+ // Example values to pass: zh, en
44
54
// also see uni.FindTranslator(...)
45
55
trans , _ := uni .GetTranslator ("en" )
46
56
47
- validate = validator .New ()
48
- en_translations .RegisterDefaultTranslations (validate , trans )
49
-
50
57
translateAll (trans )
51
58
translateIndividual (trans )
52
59
translateOverride (trans ) // yep you can specify your own in whatever locale you want!
0 commit comments