MCPcopy Create free account
hub / github.com/kataras/iris / newApp

Function newApp

_examples/i18n/basic/main.go:14–102  ·  view source on GitHub ↗

* See i18n-template for a more advanced translation key-values. */

()

Source from the content-addressed store, hash-verified

12*/
13
14func newApp() *iris.Application {
15 app := iris.New()
16
17 // Configure i18n.
18 //
19 // app.I18n.Subdomain = false to disable resolve lang code from subdomain.
20 // app.I18n.LoadAssets for go-bindata.
21
22 // Default values:
23 // app.I18n.URLParameter = "lang"
24 // app.I18n.Subdomain = true
25 //
26 // Set to false to disallow path (local) redirects,
27 // see https://github.com/kataras/iris/issues/1369.
28 // app.I18n.PathRedirect = true
29 //
30 // See `app.I18n.ExtractFunc = func(ctx iris.Context) string` or
31 // `ctx.SetLanguage(langCode string)` to change the extracted language from a request.
32 //
33 // Use DefaultMessageFunc to customize the return value of a not found key or lang.
34 // All language inputs fallback to the default locale if not matched.
35 // This is why this one accepts both input and matched languages,
36 // so the caller can be more expressful knowing those.
37 // Defaults to nil.
38 app.I18n.DefaultMessageFunc = func(langInput, langMatched, key string, args ...interface{}) string {
39 msg := fmt.Sprintf("user language input: %s: matched as: %s: not found key: %s: args: %v", langInput, langMatched, key, args)
40 app.Logger().Warn(msg)
41 return msg
42 }
43 // Load i18n when customizations are set in place.
44 //
45 // First parameter: Glob filpath patern,
46 // Second variadic parameter: Optional language tags, the first one is the default/fallback one.
47 err := app.I18n.Load("./locales/*/*", "en-US", "el-GR", "zh-CN")
48 if err != nil {
49 panic(err)
50 }
51
52 app.Get("/not-matched", func(ctx iris.Context) {
53 text := ctx.Tr("not_found_key", "some", "values", 42)
54 ctx.WriteString(text)
55 // user language input: en-gb: matched as: en-US: not found key: not_found_key: args: [some values 42]
56 })
57
58 app.Get("/", func(ctx iris.Context) {
59 hi := ctx.Tr("hi", "iris")
60 locale := ctx.GetLocale()
61
62 ctx.Writef("From the language %s translated output: %s", locale.Language(), hi)
63 })
64
65 app.Get("/some-path", func(ctx iris.Context) {
66 ctx.Writef("%s", ctx.Tr("hi", "iris"))
67 })
68
69 app.Get("/other", func(ctx iris.Context) {
70 language := ctx.GetLocale().Language()
71

Callers 2

TestI18nFunction · 0.70
mainFunction · 0.70

Calls 13

NewMethod · 0.80
WriteStringMethod · 0.80
WritefMethod · 0.80
LoggerMethod · 0.65
LoadMethod · 0.65
GetMethod · 0.65
TrMethod · 0.65
GetLocaleMethod · 0.65
LanguageMethod · 0.65
RegisterViewMethod · 0.65
ViewMethod · 0.65
ErrorMethod · 0.65

Tested by 1

TestI18nFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…