mirror of
https://github.com/kataras/iris.git
synced 2026-01-07 12:07:28 +00:00
simplify some examples
This commit is contained in:
@@ -44,7 +44,7 @@ func newApp() *iris.Application {
|
||||
//
|
||||
// First parameter: Glob filpath patern,
|
||||
// Second variadic parameter: Optional language tags, the first one is the default/fallback one.
|
||||
err := app.I18n.Load("./locales/*/*.ini", "en-US", "el-GR", "zh-CN")
|
||||
err := app.I18n.Load("./locales/*/*", "en-US", "el-GR", "zh-CN")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
[forms]
|
||||
member = μέλος
|
||||
register = Γίνε {{tr "forms.member" }}
|
||||
registered = εγγεγραμμένοι
|
||||
registered_members = Υπάρχουν {{ concat (plural (tr "forms.member") .count) (tr "forms.registered") }}
|
||||
register = Γίνε {{uppercase (tr "forms.member") }}
|
||||
registered = εγγεγραμμένοι
|
||||
@@ -1 +0,0 @@
|
||||
HiDogs: Hi %d {{plural (tr "Dog") .count }}
|
||||
@@ -1,5 +1,4 @@
|
||||
[forms]
|
||||
member = member
|
||||
register = Become a {{tr "forms.member" }}
|
||||
registered = registered
|
||||
registered_members = There are {{ concat (plural (tr "forms.member") .count) (tr "forms.registered") }}
|
||||
register = Become a {{uppercase (tr "forms.member") }}
|
||||
registered = registered
|
||||
@@ -1,2 +0,0 @@
|
||||
Dog: "dog"
|
||||
HiDogs: Hi %d {{plural (tr "Dog") .count }}
|
||||
@@ -5,20 +5,11 @@ import (
|
||||
"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() {
|
||||
@@ -29,75 +20,29 @@ func main() {
|
||||
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")
|
||||
err := app.I18n.Load("./locales/*/*.ini", "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".
|
||||
text := ctx.Tr("forms.register") // en-US: prints "Become a MEMBER".
|
||||
ctx.WriteString(text)
|
||||
})
|
||||
|
||||
app.Get("/singular", func(ctx iris.Context) {
|
||||
text := ctx.Tr("HiDogs", iris.Map{
|
||||
"count": 1,
|
||||
}) // en-US: prints "Hi 1 dog".
|
||||
app.Get("/title", func(ctx iris.Context) {
|
||||
text := ctx.Tr("user.connections.Title") // en-US: prints "Accounts Connections".
|
||||
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
|
||||
}
|
||||
|
||||
@@ -11,16 +11,11 @@ func TestI18nLoaderFuncMap(t *testing.T) {
|
||||
|
||||
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")
|
||||
Body().Equal("Become a MEMBER")
|
||||
e.GET("/title").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("Account Connections")
|
||||
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: Λογαριασμός Συνδέσεις")
|
||||
Body().Equal("Γίνε ΜΈΛΟΣ")
|
||||
e.GET("/title").WithHeader("Accept-Language", "el").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("Λογαριασμός Συνδέσεις")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user