mirror of
https://github.com/kataras/iris.git
synced 2025-12-22 04:17:03 +00:00
implement path prefx for i18n middleware, as requested at: #1369
Former-commit-id: b0d6b6e7f368e710b01faad9b70dfa4cebdd8c4d
This commit is contained in:
@@ -7,20 +7,29 @@ import (
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Logger().SetLevel("debug")
|
||||
|
||||
globalLocale := i18n.New(i18n.Config{
|
||||
Default: "en-US",
|
||||
URLParameter: "lang",
|
||||
i18nConfig := i18n.Config{
|
||||
Default: "en-US",
|
||||
URLParameter: "lang",
|
||||
PathParameter: "lang",
|
||||
Languages: map[string]string{
|
||||
"en-US": "./locales/locale_en-US.ini",
|
||||
"el-GR": "./locales/locale_el-GR.ini",
|
||||
"zh-CN": "./locales/locale_zh-CN.ini",
|
||||
},
|
||||
})
|
||||
app.Use(globalLocale)
|
||||
Alternatives: map[string]string{"greek": "el-GR"},
|
||||
}
|
||||
|
||||
// See https://github.com/kataras/iris/issues/1369
|
||||
// if you want to enable this (SEO) feature.
|
||||
app.WrapRouter(i18n.NewWrapper(i18nConfig))
|
||||
|
||||
i18nMiddleware := i18n.New(i18nConfig)
|
||||
app.Use(i18nMiddleware)
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
// it tries to find the language by:
|
||||
// Ir tries to find the language by:
|
||||
// ctx.Values().GetString("language")
|
||||
// if that was empty then
|
||||
// it tries to find from the URLParameter set on the configuration
|
||||
@@ -35,14 +44,23 @@ func newApp() *iris.Application {
|
||||
// or:
|
||||
hi := i18n.Translate(ctx, "hi", "iris")
|
||||
|
||||
// GetTranslateLanguageContextKey() == "language"
|
||||
language := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey())
|
||||
// return is form of 'en-US'
|
||||
|
||||
// The first succeed language found saved at the cookie with name ("language"),
|
||||
// you can change that by changing the value of the: iris.TranslateLanguageContextKey
|
||||
// you can change that by changing the value of the: iris.TranslateLanguageContextKey
|
||||
ctx.Writef("From the language %s translated output: %s", language, hi)
|
||||
})
|
||||
|
||||
app.Get("/some-path", func(ctx iris.Context) {
|
||||
ctx.Writef("%s", ctx.Translate("hi", "iris"))
|
||||
})
|
||||
|
||||
app.Get("/sitemap.xml", func(ctx iris.Context) {
|
||||
ctx.WriteString("sitemap")
|
||||
})
|
||||
|
||||
multiLocale := i18n.New(i18n.Config{
|
||||
Default: "en-US",
|
||||
URLParameter: "lang",
|
||||
@@ -68,7 +86,8 @@ func newApp() *iris.Application {
|
||||
|
||||
app.Get("/templates", func(ctx iris.Context) {
|
||||
ctx.View("index.html", iris.Map{
|
||||
"tr": ctx.Translate,
|
||||
"tr": ctx.Translate, // word, arguments...
|
||||
"trLang": ctx.TranslateLang, // locale, word, arguments...
|
||||
})
|
||||
// it will return "hello, iris"
|
||||
// when {{call .tr "hi" "iris"}}
|
||||
@@ -81,7 +100,9 @@ func newApp() *iris.Application {
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// go to http://localhost:8080/?lang=el-GR
|
||||
// go to http://localhost:8080/el-GR/some-path
|
||||
// or http://localhost:8080/zh-cn/templates
|
||||
// or http://localhost:8080/some-path?lang=el-GR
|
||||
// or http://localhost:8080 (default is en-US)
|
||||
// or http://localhost:8080/?lang=zh-CN
|
||||
//
|
||||
@@ -90,5 +111,6 @@ func main() {
|
||||
// or http://localhost:8080/multi?lang=en-US
|
||||
//
|
||||
// or use cookies to set the language.
|
||||
//
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user