mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 13:05:56 +00:00
i18n: add the ability to register template funcs per locale
This commit is contained in:
@@ -170,7 +170,8 @@
|
||||
* [Client-Side](compression/client/main.go)
|
||||
* [Client-Side (using Iris)](compress/client-using-iris/main.go)
|
||||
* Localization and Internationalization
|
||||
* [i18n](i18n/main.go)
|
||||
* [i18n](i18n)
|
||||
* [i18n templates and functions](i18n/i18n-template)
|
||||
* Authentication, Authorization & Bot Detection
|
||||
* [Basic Authentication](auth/basicauth/main.go)
|
||||
* [CORS](auth/cors)
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"text/template"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
// go get -u github.com/gertd/go-pluralize
|
||||
"github.com/gertd/go-pluralize"
|
||||
)
|
||||
|
||||
/*
|
||||
Iris I18n supports text/template inside the translation values.
|
||||
Follow this tutorial to learn how to use that feature.
|
||||
*/
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Listen(":8080")
|
||||
@@ -16,18 +22,23 @@ func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
pluralize := pluralize.NewClient()
|
||||
app.I18n.Loader.FuncMap = map[string]interface{}{
|
||||
"plural": func(word string, count int) string {
|
||||
// Your own implementation or use a 3rd-party package
|
||||
// like we do here.
|
||||
//
|
||||
// Note that this is only for english,
|
||||
// but you can accept the language code
|
||||
// and use a map with dictionaries to
|
||||
// pluralize words based on the given language.
|
||||
return pluralize.Pluralize(word, count, true)
|
||||
},
|
||||
|
||||
// Set custom functions per locale!
|
||||
app.I18n.Loader.Funcs = func(current iris.Locale) template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"plural": func(word string, count int) string {
|
||||
// Your own implementation or use a 3rd-party package
|
||||
// like we do here.
|
||||
//
|
||||
// Note that this is only for english,
|
||||
// but you can accept the language code
|
||||
// and use a map with dictionaries to
|
||||
// pluralize words based on the given language.
|
||||
return pluralize.Pluralize(word, count, true)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
app.I18n.Load("./locales/*/*.yml", "en-US", "el-GR")
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
|
||||
@@ -7,6 +7,10 @@ import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
/*
|
||||
See i18n-template for a more advanced translation key-values.
|
||||
*/
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user