mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 13:05:56 +00:00
New: i18n pluralization and variables support and more...
fixes: #1649, #1648, #1641, #1650 relative to: #1597
This commit is contained in:
10
_examples/i18n/template/locales/el-GR/other.ini
Normal file
10
_examples/i18n/template/locales/el-GR/other.ini
Normal file
@@ -0,0 +1,10 @@
|
||||
[nav]
|
||||
User = Λογαριασμός
|
||||
|
||||
[debug]
|
||||
Title = Μενού προγραμματιστή
|
||||
AccessLog = Πρόσβαση στο αρχείο καταγραφής
|
||||
AccessLogClear = Καθαρισμός {{tr "debug.AccessLog"}}
|
||||
|
||||
[user.connections]
|
||||
Title = {{tr "nav.User"}} Συνδέσεις
|
||||
5
_examples/i18n/template/locales/el-GR/user.ini
Normal file
5
_examples/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") }}
|
||||
1
_examples/i18n/template/locales/el-GR/welcome.yml
Normal file
1
_examples/i18n/template/locales/el-GR/welcome.yml
Normal file
@@ -0,0 +1 @@
|
||||
HiDogs: Hi %d {{plural (tr "Dog") .count }}
|
||||
12
_examples/i18n/template/locales/en-US/other.ini
Normal file
12
_examples/i18n/template/locales/en-US/other.ini
Normal file
@@ -0,0 +1,12 @@
|
||||
# just an example of some more nested keys,
|
||||
# see /other endpoint.
|
||||
[nav]
|
||||
User = Account
|
||||
|
||||
[debug]
|
||||
Title = Developer Menu
|
||||
AccessLog = Access Log
|
||||
AccessLogClear = Clear {{tr "debug.AccessLog"}}
|
||||
|
||||
[user.connections]
|
||||
Title = {{tr "nav.User"}} Connections
|
||||
5
_examples/i18n/template/locales/en-US/user.ini
Normal file
5
_examples/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") }}
|
||||
2
_examples/i18n/template/locales/en-US/welcome.yml
Normal file
2
_examples/i18n/template/locales/en-US/welcome.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
Dog: "dog"
|
||||
HiDogs: Hi %d {{plural (tr "Dog") .count }}
|
||||
103
_examples/i18n/template/main.go
Normal file
103
_examples/i18n/template/main.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
|
||||
// go get -u golang.org/x/text/message
|
||||
|
||||
"golang.org/x/text/feature/plural"
|
||||
"golang.org/x/text/language"
|
||||
"golang.org/x/text/message"
|
||||
)
|
||||
|
||||
/*
|
||||
Iris I18n supports text/template inside the translation values.
|
||||
Follow this example to learn how to use that feature.
|
||||
|
||||
This is just an example on how to use template functions.
|
||||
See the "plurals" example for a more comprehensive pluralization support instead.
|
||||
*/
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
// set the printers after load, so they can be done by loop of available languages.
|
||||
printers := make(map[string]*message.Printer)
|
||||
|
||||
message.Set(language.Greek, "Hello %d dog",
|
||||
plural.Selectf(1, "%d",
|
||||
"one", "Γεια σου σκυλί",
|
||||
"other", "Γεια σας %[1]d σκυλιά",
|
||||
))
|
||||
|
||||
/* by variable, single word:
|
||||
message.Set(language.Greek, "Hi %d dog(s)",
|
||||
catalog.Var("dogs", plural.Selectf(1, "%d", "one", "σκυλί", "other", "σκυλιά")),
|
||||
catalog.String("Γεια %[1]d ${dogs}"))
|
||||
*/
|
||||
|
||||
// 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.
|
||||
return printers[current.Language()].Sprintf(word, count)
|
||||
},
|
||||
"uppercase": func(word string) string {
|
||||
return strings.ToUpper(word)
|
||||
},
|
||||
"concat": func(words ...string) string {
|
||||
return strings.Join(words, " ")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
err := app.I18n.Load("./locales/*/*", "en-US", "el-GR")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, tag := range app.I18n.Tags() {
|
||||
printers[tag.String()] = message.NewPrinter(tag)
|
||||
}
|
||||
|
||||
message.NewPrinter(language.Greek).Printf("Hello %d dog", 2)
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
text := ctx.Tr("HiDogs", iris.Map{
|
||||
"count": 2,
|
||||
}) // en-US: prints "Hi 2 dogs".
|
||||
ctx.WriteString(text)
|
||||
})
|
||||
|
||||
app.Get("/singular", func(ctx iris.Context) {
|
||||
text := ctx.Tr("HiDogs", iris.Map{
|
||||
"count": 1,
|
||||
}) // 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)
|
||||
})
|
||||
|
||||
// showcases the other.ini translation file.
|
||||
app.Get("/other", func(ctx iris.Context) {
|
||||
ctx.Writef(`AccessLogClear: %s
|
||||
Title: %s`, ctx.Tr("debug.AccessLogClear"), ctx.Tr("user.connections.Title"))
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
26
_examples/i18n/template/main_test.go
Normal file
26
_examples/i18n/template/main_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
)
|
||||
|
||||
func TestI18nLoaderFuncMap(t *testing.T) {
|
||||
app := newApp()
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("Hi 2 dogs")
|
||||
e.GET("/singular").Expect().Status(httptest.StatusOK).
|
||||
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).
|
||||
Body().Equal("Γειά 2 σκυλί")
|
||||
|
||||
e.GET("/other").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("AccessLogClear: Clear Access Log\nTitle: Account Connections")
|
||||
e.GET("/other").WithHeader("Accept-Language", "el").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("AccessLogClear: Καθαρισμός Πρόσβαση στο αρχείο καταγραφής\nTitle: Λογαριασμός Συνδέσεις")
|
||||
}
|
||||
Reference in New Issue
Block a user