2019-12-13 22:06:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-18 07:05:51 +02:00
|
|
|
"fmt"
|
2020-09-03 12:39:42 +02:00
|
|
|
"html/template"
|
2020-08-18 07:05:51 +02:00
|
|
|
|
2019-12-13 22:06:18 +01:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
)
|
|
|
|
|
2020-09-10 05:22:53 +02:00
|
|
|
/*
|
|
|
|
See i18n-template for a more advanced translation key-values.
|
|
|
|
*/
|
|
|
|
|
2019-12-13 22:06:18 +01:00
|
|
|
func newApp() *iris.Application {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
// Configure i18n.
|
2020-08-18 07:05:51 +02:00
|
|
|
//
|
2020-08-16 06:07:36 +02:00
|
|
|
// app.I18n.Subdomain = false to disable resolve lang code from subdomain.
|
2019-12-13 22:06:18 +01:00
|
|
|
// app.I18n.LoadAssets for go-bindata.
|
|
|
|
|
|
|
|
// Default values:
|
|
|
|
// app.I18n.URLParameter = "lang"
|
|
|
|
// app.I18n.Subdomain = true
|
|
|
|
//
|
|
|
|
// Set to false to disallow path (local) redirects,
|
|
|
|
// see https://github.com/kataras/iris/issues/1369.
|
|
|
|
// app.I18n.PathRedirect = true
|
2020-05-04 19:23:15 +02:00
|
|
|
//
|
|
|
|
// See `app.I18n.ExtractFunc = func(ctx iris.Context) string` or
|
|
|
|
// `ctx.SetLanguage(langCode string)` to change the extracted language from a request.
|
2020-08-18 07:05:51 +02:00
|
|
|
//
|
|
|
|
// Use DefaultMessageFunc to customize the return value of a not found key or lang.
|
|
|
|
// All language inputs fallback to the default locale if not matched.
|
|
|
|
// This is why this one accepts both input and matched languages,
|
|
|
|
// so the caller can be more expressful knowing those.
|
|
|
|
// Defaults to nil.
|
|
|
|
app.I18n.DefaultMessageFunc = func(langInput, langMatched, key string, args ...interface{}) string {
|
|
|
|
msg := fmt.Sprintf("user language input: %s: matched as: %s: not found key: %s: args: %v", langInput, langMatched, key, args)
|
|
|
|
app.Logger().Warn(msg)
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
// Load i18n when customizations are set in place.
|
|
|
|
//
|
|
|
|
// First parameter: Glob filpath patern,
|
|
|
|
// Second variadic parameter: Optional language tags, the first one is the default/fallback one.
|
2020-11-07 11:49:14 +01:00
|
|
|
err := app.I18n.Load("./locales/*/*", "en-US", "el-GR", "zh-CN")
|
2020-08-18 07:05:51 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Get("/not-matched", func(ctx iris.Context) {
|
|
|
|
text := ctx.Tr("not_found_key", "some", "values", 42)
|
|
|
|
ctx.WriteString(text)
|
|
|
|
// user language input: en-gb: matched as: en-US: not found key: not_found_key: args: [some values 42]
|
|
|
|
})
|
2019-12-13 22:06:18 +01:00
|
|
|
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
|
|
hi := ctx.Tr("hi", "iris")
|
|
|
|
locale := ctx.GetLocale()
|
|
|
|
|
|
|
|
ctx.Writef("From the language %s translated output: %s", locale.Language(), hi)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/some-path", func(ctx iris.Context) {
|
|
|
|
ctx.Writef("%s", ctx.Tr("hi", "iris"))
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/other", func(ctx iris.Context) {
|
|
|
|
language := ctx.GetLocale().Language()
|
|
|
|
|
|
|
|
fromFirstFileValue := ctx.Tr("key1")
|
|
|
|
fromSecondFileValue := ctx.Tr("key2")
|
|
|
|
ctx.Writef("From the language: %s, translated output:\n%s=%s\n%s=%s",
|
|
|
|
language, "key1", fromFirstFileValue,
|
|
|
|
"key2", fromSecondFileValue)
|
|
|
|
})
|
|
|
|
|
|
|
|
// using in inside your views:
|
|
|
|
view := iris.HTML("./views", ".html")
|
|
|
|
app.RegisterView(view)
|
|
|
|
|
|
|
|
app.Get("/templates", func(ctx iris.Context) {
|
2022-12-13 00:37:15 +01:00
|
|
|
if err := ctx.View("index.html", iris.Map{
|
2019-12-13 22:06:18 +01:00
|
|
|
"tr": ctx.Tr, // word, arguments... {call .tr "hi" "iris"}}
|
2020-09-03 12:39:42 +02:00
|
|
|
"trUnsafe": func(message string, args ...interface{}) template.HTML {
|
|
|
|
return template.HTML(ctx.Tr(message, args...))
|
|
|
|
},
|
2022-12-13 00:37:15 +01:00
|
|
|
}); err != nil {
|
|
|
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2019-12-13 22:06:18 +01:00
|
|
|
|
|
|
|
// Note that,
|
|
|
|
// Iris automatically adds a "tr" global template function as well,
|
2019-12-16 01:00:42 +01:00
|
|
|
// the only difference is the way you call it inside your templates and
|
2019-12-13 22:06:18 +01:00
|
|
|
// that it accepts a language code as its first argument: {{ tr "el-GR" "hi" "iris"}}
|
|
|
|
})
|
|
|
|
//
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := newApp()
|
|
|
|
|
2019-12-16 01:00:42 +01:00
|
|
|
// go to http://localhost:8080/el-gr/some-path
|
|
|
|
// ^ (by path prefix)
|
|
|
|
//
|
|
|
|
// or http://el.mydomain.com8080/some-path
|
|
|
|
// ^ (by subdomain - test locally with the hosts file)
|
|
|
|
//
|
|
|
|
// or http://localhost:8080/zh-CN/templates
|
|
|
|
// ^ (by path prefix with uppercase)
|
|
|
|
//
|
|
|
|
// or http://localhost:8080/some-path?lang=el-GR
|
|
|
|
// ^ (by url parameter)
|
|
|
|
//
|
2019-12-13 22:06:18 +01:00
|
|
|
// or http://localhost:8080 (default is en-US)
|
|
|
|
// or http://localhost:8080/?lang=zh-CN
|
|
|
|
//
|
|
|
|
// go to http://localhost:8080/other?lang=el-GR
|
|
|
|
// or http://localhost:8080/other (default is en-US)
|
|
|
|
// or http://localhost:8080/other?lang=en-US
|
|
|
|
//
|
|
|
|
// or use cookies to set the language.
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080", iris.WithSitemap("http://localhost:8080"))
|
2019-12-13 22:06:18 +01:00
|
|
|
}
|