mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 21:15:56 +00:00
i18n: expose the LoaderConfig from the main i18n instance and add an example
Signed-off-by: Gerasimos (Makis) Maropoulos <kataras2006@hotmail.com>
This commit is contained in:
2
_examples/i18n/i18n-template/locales/el-GR/welcome.yml
Normal file
2
_examples/i18n/i18n-template/locales/el-GR/welcome.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
Dog: "σκυλί"
|
||||
HiDogs: Γειά {{plural (tr .locale "Dog") .count }}
|
||||
2
_examples/i18n/i18n-template/locales/en-US/welcome.yml
Normal file
2
_examples/i18n/i18n-template/locales/en-US/welcome.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
Dog: "dog"
|
||||
HiDogs: Hi {{plural (tr .locale "Dog") .count }}
|
||||
42
_examples/i18n/i18n-template/main.go
Normal file
42
_examples/i18n/i18n-template/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
|
||||
// go get -u github.com/gertd/go-pluralize
|
||||
"github.com/gertd/go-pluralize"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
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)
|
||||
},
|
||||
}
|
||||
app.I18n.Load("./locales/*/*.yml", "en-US", "el-GR")
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
text := ctx.Tr("HiDogs", iris.Map{
|
||||
"locale": ctx.GetLocale(),
|
||||
"count": 2,
|
||||
}) // prints "Hi 2 dogs".
|
||||
ctx.WriteString(text)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
17
_examples/i18n/i18n-template/main_test.go
Normal file
17
_examples/i18n/i18n-template/main_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
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("/").WithHeader("Accept-Language", "el").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("Γειά 2 σκυλί")
|
||||
}
|
||||
@@ -16,7 +16,8 @@ func main() {
|
||||
|
||||
ac := accesslog.File("./access.log")
|
||||
ac.TimeFormat = "2006-01-02 15:04:05"
|
||||
ac.Async = true
|
||||
// Optionally run logging after response has sent:
|
||||
// ac.Async = true
|
||||
broker := ac.Broker() // <- IMPORTANT
|
||||
|
||||
app := iris.New()
|
||||
@@ -25,7 +26,11 @@ func main() {
|
||||
app.Get("/", indexHandler)
|
||||
app.Get("/profile/{username}", profileHandler)
|
||||
app.Post("/read_body", readBodyHandler)
|
||||
app.Get("/logs", logsHandler(broker))
|
||||
|
||||
// register the /logs route,
|
||||
// registers a listener and prints the incoming logs.
|
||||
// Optionally, skip logging this handler.
|
||||
app.Get("/logs", accesslog.SkipHandler, logsHandler(broker))
|
||||
|
||||
// http://localhost:8080/logs to see the logs at real-time.
|
||||
app.Listen(":8080")
|
||||
@@ -52,8 +57,7 @@ func readBodyHandler(ctx iris.Context) {
|
||||
|
||||
func logsHandler(b *accesslog.Broker) iris.Handler {
|
||||
return func(ctx iris.Context) {
|
||||
accesslog.Skip(ctx) // skip logging this handler, optionally.
|
||||
|
||||
// accesslog.Skip(ctx) // or inline skip.
|
||||
logs := b.NewListener() // <- IMPORTANT
|
||||
|
||||
ctx.Header("Transfer-Encoding", "chunked")
|
||||
|
||||
Reference in New Issue
Block a user