Skip to content

Commit

Permalink
chore: MustGetMessage both support param which type of i18n.LocalizeC…
Browse files Browse the repository at this point in the history
…onfig (#40)
  • Loading branch information
VarusHsu authored Nov 25, 2023
1 parent 87dc629 commit 165baa2
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 18 deletions.
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 @@ 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
}

// 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 @@ -52,7 +59,7 @@ func makeRequest(
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,23 +69,31 @@ func TestI18nEN(t *testing.T) {
{
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)
}
})
Expand All @@ -88,7 +103,7 @@ func TestI18nEN(t *testing.T) {
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,23 +113,31 @@ func TestI18nDE(t *testing.T) {
{
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)
}
})
Expand All @@ -124,7 +147,7 @@ func TestI18nDE(t *testing.T) {
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 @@ func TestI18nFR(t *testing.T) {
{
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 }} 岁了"
}

0 comments on commit 165baa2

Please sign in to comment.