From 934c6ded755011cc8516dd3d51cc70b4801c062b Mon Sep 17 00:00:00 2001 From: XuShuo Date: Thu, 9 Nov 2023 18:31:51 +0800 Subject: [PATCH] MustGetMessage both support param which type of i18n.LocalizeConfig or *i18n.LocalizeConfig --- ginI18n.go | 2 ++ i18n_test.go | 59 ++++++++++++++++++++++++++--------- testdata/localize/de.yaml | 1 + testdata/localize/en.yaml | 1 + testdata/localize/fr.yaml | 3 +- testdata/localizeJSON/de.json | 3 +- testdata/localizeJSON/en.json | 3 +- testdata/localizeJSON/zh.json | 3 +- 8 files changed, 57 insertions(+), 18 deletions(-) diff --git a/ginI18n.go b/ginI18n.go index 61b23d0..e3a7173 100644 --- a/ginI18n.go +++ b/ginI18n.go @@ -132,6 +132,8 @@ func (i *ginI18nImpl) getLocalizeConfig(param interface{}) (*i18n.LocalizeConfig return localizeConfig, nil case *i18n.LocalizeConfig: return paramValue, nil + case i18n.LocalizeConfig: + return ¶mValue, nil } msg := fmt.Sprintf("un supported localize param: %v", param) diff --git a/i18n_test.go b/i18n_test.go index c77e20f..861c244 100644 --- a/i18n_test.go +++ b/i18n_test.go @@ -28,6 +28,14 @@ func newServer() *gin.Engine { }, })) }) + router.GET("/age/:age", func(context *gin.Context) { + context.String(http.StatusOK, MustGetMessage(context, i18n.LocalizeConfig{ + MessageID: "welcomeWithAge", + TemplateData: map[string]string{ + "age": context.Param("age"), + }, + })) + }) return router } @@ -35,9 +43,8 @@ func newServer() *gin.Engine { // makeRequest ... func makeRequest( lng language.Tag, - name string, + path string, ) string { - path := "/" + name req, _ := http.NewRequestWithContext(context.Background(), "GET", path, nil) req.Header.Add("Accept-Language", lng.String()) @@ -52,7 +59,7 @@ func makeRequest( func TestI18nEN(t *testing.T) { type args struct { lng language.Tag - name string + path string } tests := []struct { name string @@ -62,7 +69,7 @@ func TestI18nEN(t *testing.T) { { name: "hello world", args: args{ - name: "", + path: "/", lng: language.English, }, want: "hello", @@ -70,15 +77,23 @@ func TestI18nEN(t *testing.T) { { name: "hello alex", args: args{ - name: "alex", + path: "/alex", lng: language.English, }, want: "hello alex", }, + { + name: "18 years old", + args: args{ + path: "/age/18", + lng: language.English, + }, + want: "I am 18 years old", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := makeRequest(tt.args.lng, tt.args.name); got != tt.want { + if got := makeRequest(tt.args.lng, tt.args.path); got != tt.want { t.Errorf("makeRequest() = %v, want %v", got, tt.want) } }) @@ -88,7 +103,7 @@ func TestI18nEN(t *testing.T) { func TestI18nDE(t *testing.T) { type args struct { lng language.Tag - name string + path string } tests := []struct { name string @@ -98,7 +113,7 @@ func TestI18nDE(t *testing.T) { { name: "hallo", args: args{ - name: "", + path: "/", lng: language.German, }, want: "hallo", @@ -106,15 +121,23 @@ func TestI18nDE(t *testing.T) { { name: "hallo alex", args: args{ - name: "alex", + path: "/alex", lng: language.German, }, want: "hallo alex", }, + { + name: "18 jahre alt", + args: args{ + path: "/age/18", + lng: language.German, + }, + want: "ich bin 18 Jahre alt", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := makeRequest(tt.args.lng, tt.args.name); got != tt.want { + if got := makeRequest(tt.args.lng, tt.args.path); got != tt.want { t.Errorf("makeRequest() = %v, want %v", got, tt.want) } }) @@ -124,7 +147,7 @@ func TestI18nDE(t *testing.T) { func TestI18nFR(t *testing.T) { type args struct { lng language.Tag - name string + path string } tests := []struct { name string @@ -134,7 +157,7 @@ func TestI18nFR(t *testing.T) { { name: "bonjour", args: args{ - name: "", + path: "/", lng: language.French, }, want: "bonjour", @@ -142,15 +165,23 @@ func TestI18nFR(t *testing.T) { { name: "bonjour alex", args: args{ - name: "alex", + path: "/alex", lng: language.French, }, want: "bonjour alex", }, + { + name: "18 ans", + args: args{ + path: "/age/18", + lng: language.French, + }, + want: "j'ai 18 ans", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := makeRequest(tt.args.lng, tt.args.name); got != tt.want { + if got := makeRequest(tt.args.lng, tt.args.path); got != tt.want { t.Errorf("makeRequest() = %v, want %v", got, tt.want) } }) diff --git a/testdata/localize/de.yaml b/testdata/localize/de.yaml index 5e82077..b49ac30 100644 --- a/testdata/localize/de.yaml +++ b/testdata/localize/de.yaml @@ -1,2 +1,3 @@ welcome: hallo welcomeWithName: hallo {{ .name }} +welcomeWithAge: ich bin {{ .age }} Jahre alt \ No newline at end of file diff --git a/testdata/localize/en.yaml b/testdata/localize/en.yaml index 165dc60..933f68f 100644 --- a/testdata/localize/en.yaml +++ b/testdata/localize/en.yaml @@ -1,2 +1,3 @@ welcome: hello welcomeWithName: hello {{ .name }} +welcomeWithAge: I am {{ .age }} years old diff --git a/testdata/localize/fr.yaml b/testdata/localize/fr.yaml index 6ac0d53..3cdcca6 100644 --- a/testdata/localize/fr.yaml +++ b/testdata/localize/fr.yaml @@ -1,2 +1,3 @@ welcome: bonjour -welcomeWithName: bonjour {{ .name }} \ No newline at end of file +welcomeWithName: bonjour {{ .name }} +welcomeWithAge: j'ai {{ .age }} ans \ No newline at end of file diff --git a/testdata/localizeJSON/de.json b/testdata/localizeJSON/de.json index 71231d2..26c0e13 100644 --- a/testdata/localizeJSON/de.json +++ b/testdata/localizeJSON/de.json @@ -1,4 +1,5 @@ { "welcome": "hallo", - "welcomeWithName": "hallo {{ .name }}" + "welcomeWithName": "hallo {{ .name }}", + "welcomeWithAge": "ich bin {{ .age }} Jahre alt" } diff --git a/testdata/localizeJSON/en.json b/testdata/localizeJSON/en.json index 120a5be..1bc9ef6 100644 --- a/testdata/localizeJSON/en.json +++ b/testdata/localizeJSON/en.json @@ -1,4 +1,5 @@ { "welcome": "hello", - "welcomeWithName": "hello {{ .name }}" + "welcomeWithName": "hello {{ .name }}", + "welcomeWithAge": "I am {{ .age }} years old" } diff --git a/testdata/localizeJSON/zh.json b/testdata/localizeJSON/zh.json index f1eeefb..d953014 100644 --- a/testdata/localizeJSON/zh.json +++ b/testdata/localizeJSON/zh.json @@ -1,4 +1,5 @@ { "welcome": "你好", - "welcomeWithName": "你好 {{ .name }}" + "welcomeWithName": "你好 {{ .name }}", + "welcomeWithAge": "我今年 {{ .age }} 岁了" }