Echo i18n Middleware is a middleware package for the Echo web framework that provides internationalization (i18n) support.
- Seamless integration with Echo web framework.
- Support for loading message bundles in various formats such as YAML.
- Flexible language negotiation using HTTP Accept-Language header or query parameter.
- Panic-free message localization with error handling.
go get github.com/itpey/echoi18n
package main
import (
"net/http"
"github.com/itpey/echoi18n"
"github.com/labstack/echo/v4"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
func main() {
e := echo.New()
e.Use(
echoi18n.NewMiddleware(&echoi18n.Config{
RootPath: "./localize",
AcceptLanguages: []language.Tag{language.Chinese, language.English},
DefaultLanguage: language.Chinese,
}),
)
e.GET("/", func(c echo.Context) error {
localize, err := echoi18n.Localize(c, "welcome")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, localize)
})
e.GET("/:name", func(c echo.Context) error {
return c.String(http.StatusOK, echoi18n.MustLocalize(c, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": c.QueryParam("name"),
},
}))
})
e.Logger.Fatal(e.Start(":1323"))
}
If you encounter any issues or have suggestions for improvement, please open an issue on GitHub.
We welcome contributions! Fork the repository, make your changes, and submit a pull request.
echoi18n is open-source software released under the MIT License. You can find a copy of the license in the LICENSE file.
echoi18n was created by itpey