-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
does Gin has Internationalization support(i18n)? #1382
Comments
Gin does not provide i18n, you could find some pacakge here. |
@syssam is there any plan for support i18n in gin framework? |
@aceyin you should ask GIN team any plan support i18n. I have a validator package support multiple language. |
Hi. Is it possible to use go-i18n(https://github.com/nicksnyder/go-i18n) with Gin? How to use it in templates? |
Hello @nanom1t, No, Gin does not support it, however other frameworks like Iris and Beego does support it for a long time now (iris does support it for 2 years+). I will copy-paste some content here so gin contributors can see its usage and hopefully manage to do something like this for gin users as well, all gophers deserves the Iris features: ./locales/en-US.all.yaml - id: hi
translation: "hi {{.}}" a .go file import "github.com/kataras/iris/v12"
import "github.com/iris-contrib/middleware/go-i18n"
// ...
app := iris.New()
locale := i18n.New(
"./locales/en-US.all.yaml",
"./locales/en-US.errors.yaml",
"./locales/zh-CN.all.yaml",
)
app.Use(locale.Serve)
// Route, use of i18n in a line of `.Translate("key", optionalArguments)`.
app.Get("/", func(ctx iris.Context){
message := ctx.Translate("hi", "iris")
ctx.Writef("translated: %s", message )
}) Inside templates you can bind the app.Get("/", func(ctx iris.Context) {
ctx.View("index.html", iris.Map{
"tr": ctx.Translate,
})
// it will return "hello, iris"
// when {{call .tr "hi" "iris"}}
}) {{call .tr "hi" "iris"}}
|
maybe we can create new middleware in https://github.com/gin-contrib |
I'm currently using gin to build a web framework for my own use case which comes with i18n middleware that uses https://github.com/nicksnyder/go-i18n/v2/i18n underneath. Hope that helps to give some reference on how to port it into https://github.com/gin-contrib. |
please commit pr to https://github.com/gin-contrib/i18n thanks! |
middleware package middleware
import (
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/suisrc/zgo/modules/helper"
)
// I18nMiddleware 国际化
func I18nMiddleware() gin.HandlerFunc {
bundle := i18n.NewBundle(language.Chinese)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.LoadMessageFile("locales/active.zh-CN.toml")
bundle.LoadMessageFile("locales/active.en-US.toml")
return func(c *gin.Context) {
lang := c.Request.FormValue("lang")
accept := c.Request.Header.Get("Accept-Language")
localizer := i18n.NewLocalizer(bundle, lang, accept)
helper.SetI18n(c, localizer)
c.Next()
}
} helper package helper
import (
"errors"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
// 定义上下文中的键
const (
ResI18nKey = Prefix + "/res-i18n"
)
// FormatMessage fm
func FormatMessage(c *gin.Context, lc *i18n.LocalizeConfig) string {
return MustI18n(c).MustLocalize(lc)
}
// FormatText ft
func FormatText(c *gin.Context, message *i18n.Message) string {
return FormatCode(c, message, nil)
}
// FormatCode fc
func FormatCode(c *gin.Context, message *i18n.Message, args map[string]interface{}) string {
if localizer, ok := GetI18n(c); ok {
return localizer.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: message,
TemplateData: args,
})
}
// 加载i18n中间件后,不会进入该分支,简单处理未加载i18n中间件时候的处理内容
if args == nil {
return message.Other
}
text := message.Other
for key, val := range args {
text = strings.ReplaceAll(text, "{{."+key+"}}", toString(val))
}
return text
}
// MustI18n i18n
func MustI18n(c *gin.Context) *i18n.Localizer {
localizer, ok := GetI18n(c)
if !ok {
panic(errors.New("context no has i18n localizer"))
}
return localizer
}
// GetI18n i18n
func GetI18n(c *gin.Context) (*i18n.Localizer, bool) {
if v, ok := c.Get(ResI18nKey); ok {
if l, b := v.(*i18n.Localizer); b {
return l, true
}
}
return nil, false
}
// SetI18n i18n
func SetI18n(c *gin.Context, l *i18n.Localizer) {
c.Set(ResI18nKey, l)
}
// ToString to string
func toString(a interface{}) string {
switch a.(type) {
case int:
return strconv.Itoa(a.(int))
case bool:
return strconv.FormatBool(a.(bool))
case string:
return a.(string)
default:
return "conver error"
}
} 使用 message := helper.FormatText(c, &i18n.Message{ID: "service.signin.select-role-text", Other: "请选择角色"} |
我将项目中的中间件抽出, 希望可以帮助到大家, suisrc/gin-i18n |
does Gin has Internationalization support(i18n)?
where's the Document or Example.
The text was updated successfully, but these errors were encountered: