These are chat archives for gin-gonic/gin

28th
May 2016
Bruce Auyeung
@bruceauyeung
May 28 2016 12:31
hi everyone, does gin support i18n?
Ousmane Traore
@otraore
May 28 2016 12:32
You can use i18n I’m not sure if there’s out of the box support for it.
But there is support for i18n in go that you can use for gin
Bruce Auyeung
@bruceauyeung
May 28 2016 12:35
it seems not to be dynamically changeable while app running?
switch en to ch, etc.
Dean Karn
@joeybloggs
May 28 2016 12:36
Also check out https://github.com/go-playground/universal-translator which has date and number formatting
Ousmane Traore
@otraore
May 28 2016 12:38
@redhatlinux10 Yeah it can be
And will do @joeybloggs
Bruce Auyeung
@bruceauyeung
May 28 2016 12:40
@otraore the key is T, _ := i18n.Tfunc("en-US")?
Ousmane Traore
@otraore
May 28 2016 12:40
yeah
so basically in your controller you could get the user defined language code
Bruce Auyeung
@bruceauyeung
May 28 2016 12:40
is this changing operation goroutine-safe?
em, it seems to be unnecesssary
changing and then reloading page is enough.
Ousmane Traore
@otraore
May 28 2016 12:45
huh?
Bruce Auyeung
@bruceauyeung
May 28 2016 12:46
i was considering whether reassign T with another instance is goroutine-safe
i was considering whether reassigning T with another instance is goroutine-safe
Ousmane Traore
@otraore
May 28 2016 12:49
I’d think you’d only need to assign it once
Why would you need to re-assign it? (Curious)
Bruce Auyeung
@bruceauyeung
May 28 2016 12:49
i wish user can change language in the login page.
manually
Ousmane Traore
@otraore
May 28 2016 12:52
Well if you’re doing a traditional web app, they can just change it and when they do you reload the page
Your login controller will see the language code and use it for the page rendering
Bruce Auyeung
@bruceauyeung
May 28 2016 12:53
yeah, i get it, goroutine-safe is not necessary.
and go-i18n does i18n at the server side, can it be done at gin's template?
Dean Karn
@joeybloggs
May 28 2016 12:54
You should set T per request, store it in a cookie and have middleware check if language set in cookie and fallback to using the Accept-Language header and then can change language on login using cookie
Ousmane Traore
@otraore
May 28 2016 12:54
@joeybloggs :+1:
@redhatlinux10 Yes, you’d pass in your templates values to the i18n value of it.
Let me type up a quick example.
Using gin’s html rendering example (https://github.com/gin-gonic/gin#html-rendering)
Dean Karn
@joeybloggs
May 28 2016 12:56
I also do a check for query param before cookie for explicit overriding for the language selection page so don't have to update cookie until they choose their language, hope that helps
Ousmane Traore
@otraore
May 28 2016 12:57
func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/*”)
    T, _ := i18n.Tfunc("en-US”)
    router.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl", gin.H{
            "title": T(“title”),
        })
    })
    router.Run(":8080")
}
Don’t know why it’s not synthax highlighting it
Bruce Auyeung
@bruceauyeung
May 28 2016 12:58
if set T per request, i have a doubt, is T instance a heavy object?
Ousmane Traore
@otraore
May 28 2016 13:00
Actually ignore what I said above
You can use T directly in your templates
Dean Karn
@joeybloggs
May 28 2016 13:02
no T is just a Struct with methods to access the translations already in memory, and most translation libraries cache them as well instead of creating as new one
Ousmane Traore
@otraore
May 28 2016 13:03
I thought so too but I was just making sure by checking out the source of go-i18n
Bruce Auyeung
@bruceauyeung
May 28 2016 13:14
thanks @otraore @joeybloggs
access the translations already in memory
do you mean all translation strings have already been loaded into memory?
it may be a huge memory.
i will try to dig out is it possible to unload translations.
Dean Karn
@joeybloggs
May 28 2016 13:20
I wouldn't worry about it too much, the translations in the end are just strings, you can store tons without much memory
Bruce Auyeung
@bruceauyeung
May 28 2016 13:22
@joeybloggs yeah, maybe i worried too much.
Bruce Auyeung
@bruceauyeung
May 28 2016 13:40
func main() { router := gin.Default() router.LoadHTMLGlob("templates/**/*") router.GET("/posts/index", func(c *gin.Context) { c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ "title": "Posts", }) }) router.GET("/users/index", func(c *gin.Context) { c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ "title": "Users", }) }) router.Run(":8080") }
func main() { router := gin.Default() router.LoadHTMLGlob("templates/**/*") router.GET("/posts/index", func(c *gin.Context) { c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ "title": "Posts", }) }) router.GET("/users/index", func(c *gin.Context) { c.HTML(http.StatusOK, "users/index.tmpl", gin.H{ "title": "Users", }) }) router.Run(":8080") }
sorry for the bad format
how can i add a funcMap to all temlates?
Bruce Auyeung
@bruceauyeung
May 28 2016 13:49
maybe func (engine *Engine) LoadHTMLGlob(pattern string) should add another param in FuncMap
maybe func (engine *Engine) LoadHTMLGlob(pattern string) should add another param : in FuncMap
so templ := template.Must(template.ParseGlob(pattern)) can be rewrited to templ := template.Must(template.ParseGlob(pattern)).Funcs(in)