Skip to content

Commit

Permalink
update to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Aug 29, 2023
1 parent 4d9bbe5 commit 32ebfd5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-fiberi18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
strategy:
matrix:
go-version:
- 1.18.x
- 1.19.x
- 1.20.x
- 1.21.x
steps:
- name: Fetch Repository
uses: actions/checkout@v3
Expand All @@ -29,4 +29,4 @@ jobs:
go-version: '${{ matrix.go-version }}'
- name: Run Test
working-directory: ./fiberi18n
run: go test -v -race ./...
run: go test -v -race ./...
10 changes: 7 additions & 3 deletions fiberi18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This middleware supports Fiber v2.

```
go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/contrib/fiberi18n
go get -u github.com/gofiber/contrib/fiberi18n/v2
```

## Signature
Expand Down Expand Up @@ -52,7 +52,7 @@ package main
import (
"log"

"github.com/gofiber/contrib/fiberi18n"
"github.com/gofiber/contrib/fiberi18n/v2"
"github.com/gofiber/fiber/v2"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
Expand All @@ -68,7 +68,11 @@ func main() {
}),
)
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(fiberi18n.MustLocalize(c, "welcome"))
localize, err := fiberi18n.Localize(c, "welcome")
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.SendString(localize)
})
app.Get("/:name", func(ctx *fiber.Ctx) error {
return ctx.SendString(fiberi18n.MustLocalize(ctx, &i18n.LocalizeConfig{
Expand Down
8 changes: 6 additions & 2 deletions fiberi18n/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"log"

"github.com/gofiber/contrib/fiberi18n"
"github.com/gofiber/contrib/fiberi18n/v2"
"github.com/gofiber/fiber/v2"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
Expand All @@ -19,7 +19,11 @@ func main() {
}),
)
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(fiberi18n.MustLocalize(c, "welcome"))
localize, err := fiberi18n.Localize(c, "welcome")
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.SendString(localize)
})
app.Get("/:name", func(ctx *fiber.Ctx) error {
return ctx.SendString(fiberi18n.MustLocalize(ctx, &i18n.LocalizeConfig{
Expand Down
4 changes: 2 additions & 2 deletions fiberi18n/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gofiber/contrib/fiberi18n
module github.com/gofiber/contrib/fiberi18n/v2

go 1.18
go 1.19

require (
github.com/gofiber/fiber/v2 v2.49.0
Expand Down
97 changes: 91 additions & 6 deletions fiberi18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io"
"net/http"
"sync"
"testing"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -31,13 +32,15 @@ func newServer() *fiber.App {

var i18nApp = newServer()

func makeRequest(lang language.Tag, name string) (*http.Response, error) {
func makeRequest(lang language.Tag, name string, app *fiber.App) (*http.Response, error) {
path := "/" + name
req, _ := http.NewRequestWithContext(context.Background(), "GET", path, nil)
req.Header.Add("Accept-Language", lang.String())
if lang != language.Und {
req.Header.Add("Accept-Language", lang.String())
}
req.Method = "GET"
req.RequestURI = path
resp, err := i18nApp.Test(req)
resp, err := app.Test(req)
return resp, err
}

Expand Down Expand Up @@ -71,7 +74,7 @@ func TestI18nEN(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeRequest(tt.args.lang, tt.args.name)
got, err := makeRequest(tt.args.lang, tt.args.name, i18nApp)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
Expand Down Expand Up @@ -109,7 +112,7 @@ func TestI18nZH(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeRequest(tt.args.lang, tt.args.name)
got, err := makeRequest(tt.args.lang, tt.args.name, i18nApp)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
Expand Down Expand Up @@ -156,11 +159,93 @@ func TestParallelI18n(t *testing.T) {
t.Parallel()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeRequest(tt.args.lang, tt.args.name)
got, err := makeRequest(tt.args.lang, tt.args.name, i18nApp)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
utils.AssertEqual(t, tt.want, string(body))
})
}
}

func TestLocalize(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Use(New())
app.Get("/", func(ctx *fiber.Ctx) error {
localize, err := Localize(ctx, "welcome?")
utils.AssertEqual(t, "", localize)
return fiber.NewError(500, err.Error())
})

app.Get("/:name", func(ctx *fiber.Ctx) error {
name := ctx.Params("name")
localize, err := Localize(ctx, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": name,
},
})
utils.AssertEqual(t, nil, err)
return ctx.SendString(localize)
})

t.Run("test localize", func(t *testing.T) {
got, err := makeRequest(language.Chinese, "", app)
utils.AssertEqual(t, 500, got.StatusCode)
utils.AssertEqual(t, nil, err)
body, _ := io.ReadAll(got.Body)
utils.AssertEqual(t, `i18n.Localize error: message "welcome?" not found in language "zh"`, string(body))

got, err = makeRequest(language.English, "name", app)
utils.AssertEqual(t, 200, got.StatusCode)
utils.AssertEqual(t, nil, err)
body, _ = io.ReadAll(got.Body)
utils.AssertEqual(t, "hello name", string(body))
})
}

func Test_defaultLangHandler(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Use(New())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(defaultLangHandler(nil, language.English.String()))
})
app.Get("/test", func(c *fiber.Ctx) error {
return c.SendString(defaultLangHandler(c, language.English.String()))
})

t.Run("test nil ctx", func(t *testing.T) {
var wg sync.WaitGroup
want := 100
wg.Add(want)
for i := 0; i < want; i++ {
go func() {
defer wg.Done()
got, err := makeRequest(language.English, "", app)
utils.AssertEqual(t, nil, err)
body, _ := io.ReadAll(got.Body)
utils.AssertEqual(t, "en", string(body))
}()
}
wg.Wait()
})

t.Run("test query and header", func(t *testing.T) {
got, err := makeRequest(language.Chinese, "test?lang=en", app)
utils.AssertEqual(t, nil, err)
body, _ := io.ReadAll(got.Body)
utils.AssertEqual(t, "en", string(body))

got, err = makeRequest(language.Chinese, "test", app)
utils.AssertEqual(t, nil, err)
body, _ = io.ReadAll(got.Body)
utils.AssertEqual(t, "zh", string(body))

got, err = makeRequest(language.Chinese, "test", app)
utils.AssertEqual(t, nil, err)
body, _ = io.ReadAll(got.Body)
utils.AssertEqual(t, "zh", string(body))
})
}

0 comments on commit 32ebfd5

Please sign in to comment.