1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 05:25:58 +00:00

implement path prefx for i18n middleware, as requested at: #1369

Former-commit-id: b0d6b6e7f368e710b01faad9b70dfa4cebdd8c4d
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-11-19 23:36:18 +02:00
parent 1faea8b2e8
commit 0844c109d9
16 changed files with 304 additions and 130 deletions

View File

@@ -68,9 +68,11 @@ type ConfigurationReadOnly interface {
GetPostMaxMemory() int64
// GetTranslateLanguageContextKey returns the configuration's TranslateFunctionContextKey value,
// used for i18n.
// used for i18n inside templates.
GetTranslateFunctionContextKey() string
// GetTranslateLangFunctionContextKey returns the configuration's TranslateLangFunctionContextKey value,
// used for i18n inside templates.
GetTranslateLangFunctionContextKey() string
// GetTranslateLanguageContextKey returns the configuration's TranslateLanguageContextKey value,
// used for i18n.
GetTranslateLanguageContextKey() string

View File

@@ -294,10 +294,16 @@ type Context interface {
Values() *memstore.Store
// Translate is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey())
// to execute the translate function and return the localized text value.
// to execute the translate function and returns the current localized text value.
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
Translate(format string, args ...interface{}) string
// TranslateLang is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateLangFunctionContextKey())
// to execute the translate function and returns the localized text value based on the "lang".
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
TranslateLang(lang, format string, args ...interface{}) string
// +------------------------------------------------------------+
// | Path, Host, Subdomain, IP, Headers etc... |
@@ -1499,17 +1505,30 @@ func (ctx *context) Values() *memstore.Store {
// Translate is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey())
// to execute the translate function and return the localized text value.
// to execute the translate function and return the current localized text value.
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
func (ctx *context) Translate(format string, args ...interface{}) string {
if cb, ok := ctx.values.Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey()).(func(format string, args ...interface{}) string); ok {
if cb, ok := ctx.values.Get(ctx.Application().ConfigurationReadOnly().GetTranslateFunctionContextKey()).(func(string, ...interface{}) string); ok {
return cb(format, args...)
}
return ""
}
// TranslateLang is the i18n (localization) middleware's function,
// it calls the Values().Get(ctx.Application().ConfigurationReadOnly().GetTranslateLangFunctionContextKey())
// to execute the translate function and returns the localized text value based on the "lang".
//
// Example: https://github.com/kataras/iris/tree/master/_examples/miscellaneous/i18n
func (ctx *context) TranslateLang(lang, format string, args ...interface{}) string {
if cb, ok := ctx.values.Get(ctx.Application().ConfigurationReadOnly().GetTranslateLangFunctionContextKey()).(func(string, string, ...interface{}) string); ok {
return cb(lang, format, args...)
}
return ""
}
// +------------------------------------------------------------+
// | Path, Host, Subdomain, IP, Headers etc... |
// +------------------------------------------------------------+