mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
More localization template examples for https://github.com/kataras/iris/issues/1597#issuecomment-690186907
This commit is contained in:
parent
b17217444e
commit
bfb7b19096
5
_examples/i18n/i18n-template/locales/el-GR/user.ini
Normal file
5
_examples/i18n/i18n-template/locales/el-GR/user.ini
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[forms]
|
||||||
|
member = μέλος
|
||||||
|
register = Γίνε {{tr "forms.member" }}
|
||||||
|
registered = εγγεγραμμένοι
|
||||||
|
registered_members = Υπάρχουν {{ concat (plural (tr "forms.member") .count) (tr "forms.registered") }}
|
5
_examples/i18n/i18n-template/locales/en-US/user.ini
Normal file
5
_examples/i18n/i18n-template/locales/en-US/user.ini
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[forms]
|
||||||
|
member = member
|
||||||
|
register = Become a {{tr "forms.member" }}
|
||||||
|
registered = registered
|
||||||
|
registered_members = There are {{ concat (plural (tr "forms.member") .count) (tr "forms.registered") }}
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
|
@ -36,22 +37,35 @@ func newApp() *iris.Application {
|
||||||
// pluralize words based on the given language.
|
// pluralize words based on the given language.
|
||||||
return pluralize.Pluralize(word, count, true)
|
return pluralize.Pluralize(word, count, true)
|
||||||
},
|
},
|
||||||
|
"uppercase": func(word string) string {
|
||||||
|
return strings.ToUpper(word)
|
||||||
|
},
|
||||||
|
"concat": func(words ...string) string {
|
||||||
|
return strings.Join(words, " ")
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.I18n.Load("./locales/*/*.yml", "en-US", "el-GR")
|
app.I18n.Load("./locales/*/*", "en-US", "el-GR")
|
||||||
|
|
||||||
app.Get("/", func(ctx iris.Context) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
text := ctx.Tr("HiDogs", iris.Map{
|
text := ctx.Tr("HiDogs", iris.Map{
|
||||||
"count": 2,
|
"count": 2,
|
||||||
}) // prints "Hi 2 dogs".
|
}) // en-US: prints "Hi 2 dogs".
|
||||||
ctx.WriteString(text)
|
ctx.WriteString(text)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Get("/singular", func(ctx iris.Context) {
|
app.Get("/singular", func(ctx iris.Context) {
|
||||||
text := ctx.Tr("HiDogs", iris.Map{
|
text := ctx.Tr("HiDogs", iris.Map{
|
||||||
"count": 1,
|
"count": 1,
|
||||||
}) // prints "Hi 1 dog".
|
}) // en-US: prints "Hi 1 dog".
|
||||||
|
ctx.WriteString(text)
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Get("/members", func(ctx iris.Context) {
|
||||||
|
text := ctx.Tr("forms.registered_members", iris.Map{
|
||||||
|
"count": 42,
|
||||||
|
}) // en-US: prints "There are 42 members registered".
|
||||||
ctx.WriteString(text)
|
ctx.WriteString(text)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,8 @@ func TestI18nLoaderFuncMap(t *testing.T) {
|
||||||
Body().Equal("Hi 2 dogs")
|
Body().Equal("Hi 2 dogs")
|
||||||
e.GET("/singular").Expect().Status(httptest.StatusOK).
|
e.GET("/singular").Expect().Status(httptest.StatusOK).
|
||||||
Body().Equal("Hi 1 dog")
|
Body().Equal("Hi 1 dog")
|
||||||
|
e.GET("/members").Expect().Status(httptest.StatusOK).
|
||||||
|
Body().Equal("There are 42 members registered")
|
||||||
e.GET("/").WithHeader("Accept-Language", "el").Expect().Status(httptest.StatusOK).
|
e.GET("/").WithHeader("Accept-Language", "el").Expect().Status(httptest.StatusOK).
|
||||||
Body().Equal("Γειά 2 σκυλί")
|
Body().Equal("Γειά 2 σκυλί")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user