2020-09-10 04:17:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-10 05:22:53 +02:00
|
|
|
"text/template"
|
2020-09-10 04:17:03 +02:00
|
|
|
|
2020-09-10 05:22:53 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2020-09-10 04:17:03 +02:00
|
|
|
// go get -u github.com/gertd/go-pluralize
|
|
|
|
"github.com/gertd/go-pluralize"
|
|
|
|
)
|
|
|
|
|
2020-09-10 05:22:53 +02:00
|
|
|
/*
|
|
|
|
Iris I18n supports text/template inside the translation values.
|
2020-09-10 09:12:06 +02:00
|
|
|
Follow this example to learn how to use that feature.
|
2020-09-10 05:22:53 +02:00
|
|
|
*/
|
|
|
|
|
2020-09-10 04:17:03 +02:00
|
|
|
func main() {
|
|
|
|
app := newApp()
|
|
|
|
app.Listen(":8080")
|
|
|
|
}
|
|
|
|
|
|
|
|
func newApp() *iris.Application {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
pluralize := pluralize.NewClient()
|
2020-09-10 05:22:53 +02:00
|
|
|
|
|
|
|
// 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,
|
2020-09-10 09:12:06 +02:00
|
|
|
// but you can use the "current" locale
|
|
|
|
// and make a map with dictionaries to
|
2020-09-10 05:22:53 +02:00
|
|
|
// pluralize words based on the given language.
|
|
|
|
return pluralize.Pluralize(word, count, true)
|
|
|
|
},
|
|
|
|
}
|
2020-09-10 04:17:03 +02:00
|
|
|
}
|
2020-09-10 05:22:53 +02:00
|
|
|
|
2020-09-10 04:17:03 +02:00
|
|
|
app.I18n.Load("./locales/*/*.yml", "en-US", "el-GR")
|
|
|
|
|
|
|
|
app.Get("/", func(ctx iris.Context) {
|
|
|
|
text := ctx.Tr("HiDogs", iris.Map{
|
2020-09-10 04:57:49 +02:00
|
|
|
"count": 2,
|
2020-09-10 04:17:03 +02:00
|
|
|
}) // prints "Hi 2 dogs".
|
|
|
|
ctx.WriteString(text)
|
|
|
|
})
|
|
|
|
|
2020-09-10 09:12:06 +02:00
|
|
|
app.Get("/singular", func(ctx iris.Context) {
|
|
|
|
text := ctx.Tr("HiDogs", iris.Map{
|
|
|
|
"count": 1,
|
|
|
|
}) // prints "Hi 1 dog".
|
|
|
|
ctx.WriteString(text)
|
|
|
|
})
|
|
|
|
|
2020-09-10 04:17:03 +02:00
|
|
|
return app
|
|
|
|
}
|