https://github.com/fishjar/gin-i18n
Simple gin i18n middleware (简单的 gin 多语言支持中间件)
https://github.com/fishjar/gin-i18n
gin gin-i18n i18n
Last synced: about 1 month ago
JSON representation
Simple gin i18n middleware (简单的 gin 多语言支持中间件)
- Host: GitHub
- URL: https://github.com/fishjar/gin-i18n
- Owner: fishjar
- License: mit
- Created: 2022-08-11T06:11:28.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-23T07:54:01.000Z (about 3 years ago)
- Last Synced: 2025-02-20T09:18:56.577Z (over 1 year ago)
- Topics: gin, gin-i18n, i18n
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple gin i18n middleware
English | [简体中文](README.zh-CN.md)
I found that other people's `gin` i18n middleware too complicated, so I wrote a simple one.
## Example
```go
// example/main.go
package main
import (
"log"
"net/http"
"path"
ginI18n "github.com/fishjar/gin-i18n"
"github.com/gin-gonic/gin"
)
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
// apply i18n middleware
router.Use(ginI18n.Localizer(&ginI18n.Options{
DefaultLang: "zh-CN", // default language
SupportLangs: "zh-CN,en-US", // list of supported languages (must include default language)
FilePath: path.Join("example", "localize"), // multilingual file directory
}))
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, ginI18n.Msg(c, "welcome"))
})
router.GET("/:name", func(c *gin.Context) {
c.String(http.StatusOK, ginI18n.Msg(c, "hello_world", c.Param("name")))
})
if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
}
```
```yaml
# example/localize/zh-CN.yml
welcome: 欢迎!
hello_world: 你好 %s!
```
```yaml
# example/localize/en-US.yml
welcome: welcome!
hello_world: hello %s!
```
```sh
# review
go run example/main.go
curl http://127.0.0.1:8080/ -H 'accept-language: zh-CN' # 欢迎!
curl http://127.0.0.1:8080/ -H 'accept-language: en-US' # welcome!
curl http://127.0.0.1:8080/ -H 'accept-language: zh-CN,en-US;q=0.9' # 欢迎!
curl http://127.0.0.1:8080/ -H 'accept-language: zh-CN;q=0.9,en-US' # welcome!
curl http://127.0.0.1:8080/ -H 'accept-language: zh' # 欢迎!
curl http://127.0.0.1:8080/ -H 'accept-language: en' # welcome!
curl http://127.0.0.1:8080/gabe -H 'accept-language: zh-CN' # 你好 gabe!
curl http://127.0.0.1:8080/gabe -H 'accept-language: en-US' # hello gabe!
```