Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: MustGetMessage both support param which type of i18n.LocalizeConfig #40

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ginI18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &paramValue, nil
}

msg := fmt.Sprintf("un supported localize param: %v", param)
Expand Down
59 changes: 45 additions & 14 deletions i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@
},
}))
})
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
}

// 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())

Expand All @@ -49,10 +56,10 @@
return w.Body.String()
}

func TestI18nEN(t *testing.T) {

Check failure on line 59 in i18n_test.go

View workflow job for this annotation

GitHub Actions / lint

59-145 lines are duplicate of `i18n_test.go:103-189` (dupl)
type args struct {
lng language.Tag
name string
path string
}
tests := []struct {
name string
Expand All @@ -62,33 +69,41 @@
{
name: "hello world",
args: args{
name: "",
path: "/",
lng: language.English,
},
want: "hello",
},
{
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)
}
})
}
}

func TestI18nDE(t *testing.T) {

Check failure on line 103 in i18n_test.go

View workflow job for this annotation

GitHub Actions / lint

103-189 lines are duplicate of `i18n_test.go:59-145` (dupl)
type args struct {
lng language.Tag
name string
path string
}
tests := []struct {
name string
Expand All @@ -98,33 +113,41 @@
{
name: "hallo",
args: args{
name: "",
path: "/",
lng: language.German,
},
want: "hallo",
},
{
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)
}
})
}
}

func TestI18nFR(t *testing.T) {

Check failure on line 147 in i18n_test.go

View workflow job for this annotation

GitHub Actions / lint

147-189 lines are duplicate of `i18n_test.go:59-101` (dupl)
type args struct {
lng language.Tag
name string
path string
}
tests := []struct {
name string
Expand All @@ -134,23 +157,31 @@
{
name: "bonjour",
args: args{
name: "",
path: "/",
lng: language.French,
},
want: "bonjour",
},
{
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)
}
})
Expand Down
1 change: 1 addition & 0 deletions testdata/localize/de.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
welcome: hallo
welcomeWithName: hallo {{ .name }}
welcomeWithAge: ich bin {{ .age }} Jahre alt
1 change: 1 addition & 0 deletions testdata/localize/en.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
welcome: hello
welcomeWithName: hello {{ .name }}
welcomeWithAge: I am {{ .age }} years old
3 changes: 2 additions & 1 deletion testdata/localize/fr.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
welcome: bonjour
welcomeWithName: bonjour {{ .name }}
welcomeWithName: bonjour {{ .name }}
welcomeWithAge: j'ai {{ .age }} ans
3 changes: 2 additions & 1 deletion testdata/localizeJSON/de.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"welcome": "hallo",
"welcomeWithName": "hallo {{ .name }}"
"welcomeWithName": "hallo {{ .name }}",
"welcomeWithAge": "ich bin {{ .age }} Jahre alt"
}
3 changes: 2 additions & 1 deletion testdata/localizeJSON/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"welcome": "hello",
"welcomeWithName": "hello {{ .name }}"
"welcomeWithName": "hello {{ .name }}",
"welcomeWithAge": "I am {{ .age }} years old"
}
3 changes: 2 additions & 1 deletion testdata/localizeJSON/zh.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"welcome": "你好",
"welcomeWithName": "你好 {{ .name }}"
"welcomeWithName": "你好 {{ .name }}",
"welcomeWithAge": "我今年 {{ .age }} 岁了"
}
Loading