mirror of
https://github.com/kataras/iris.git
synced 2026-01-07 12:07:28 +00:00
add support for the go standard embed tag for locale files
This commit is contained in:
10
_examples/i18n/template-embedded/locales/el-GR/other.ini
Normal file
10
_examples/i18n/template-embedded/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"}} Συνδέσεις
|
||||
4
_examples/i18n/template-embedded/locales/el-GR/user.ini
Normal file
4
_examples/i18n/template-embedded/locales/el-GR/user.ini
Normal file
@@ -0,0 +1,4 @@
|
||||
[forms]
|
||||
member = μέλος
|
||||
register = Γίνε {{uppercase (tr "forms.member") }}
|
||||
registered = εγγεγραμμένοι
|
||||
12
_examples/i18n/template-embedded/locales/en-US/other.ini
Normal file
12
_examples/i18n/template-embedded/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
|
||||
4
_examples/i18n/template-embedded/locales/en-US/user.ini
Normal file
4
_examples/i18n/template-embedded/locales/en-US/user.ini
Normal file
@@ -0,0 +1,4 @@
|
||||
[forms]
|
||||
member = member
|
||||
register = Become a {{uppercase (tr "forms.member") }}
|
||||
registered = registered
|
||||
50
_examples/i18n/template-embedded/main.go
Normal file
50
_examples/i18n/template-embedded/main.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
//go:embed locales/*
|
||||
var filesystem embed.FS
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
// Set custom functions per locale!
|
||||
app.I18n.Loader.Funcs = func(current iris.Locale) template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"uppercase": func(word string) string {
|
||||
return strings.ToUpper(word)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Instead of:
|
||||
// err := app.I18n.Load("./locales/*/*.ini", "en-US", "el-GR")
|
||||
// Apply the below in order to build with embedded locales inside your executable binary.
|
||||
err := app.I18n.LoadFS(filesystem, ".", "en-US", "el-GR")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
text := ctx.Tr("forms.register") // en-US: prints "Become a MEMBER".
|
||||
ctx.WriteString(text)
|
||||
})
|
||||
|
||||
app.Get("/title", func(ctx iris.Context) {
|
||||
text := ctx.Tr("user.connections.Title") // en-US: prints "Accounts Connections".
|
||||
ctx.WriteString(text)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
21
_examples/i18n/template-embedded/main_test.go
Normal file
21
_examples/i18n/template-embedded/main_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
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("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("Γίνε ΜΈΛΟΣ")
|
||||
e.GET("/title").WithHeader("Accept-Language", "el").Expect().Status(httptest.StatusOK).
|
||||
Body().Equal("Λογαριασμός Συνδέσεις")
|
||||
}
|
||||
Reference in New Issue
Block a user