1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-25 05:47:07 +00:00

implement #1593 - Read HISTORY.md

updated example: https://github.com/kataras/iris/blob/master/_examples/i18n/main.go#L28-L50
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-18 08:05:51 +03:00
parent 1192e6f787
commit a491cdf7ef
9 changed files with 170 additions and 40 deletions

View File

@@ -49,6 +49,8 @@ type ConfigurationReadOnly interface {
GetLocaleContextKey() string
// GetLanguageContextKey returns the LanguageContextKey field.
GetLanguageContextKey() string
// GetLanguageInputContextKey returns the LanguageInputContextKey field.
GetLanguageInputContextKey() string
// GetVersionContextKey returns the VersionContextKey field.
GetVersionContextKey() string

View File

@@ -1203,6 +1203,7 @@ func (ctx *Context) SetLanguage(langCode string) {
}
// GetLocale returns the current request's `Locale` found by i18n middleware.
// It always fallbacks to the default one.
// See `Tr` too.
func (ctx *Context) GetLocale() Locale {
// Cache the Locale itself for multiple calls of `Tr` method.
@@ -1225,11 +1226,13 @@ func (ctx *Context) GetLocale() Locale {
// See `GetLocale` too.
//
// Example: https://github.com/kataras/iris/tree/master/_examples/i18n
func (ctx *Context) Tr(message string, values ...interface{}) string { // other name could be: Localize.
if locale := ctx.GetLocale(); locale != nil { // TODO: here... I need to change the logic, if not found then call the i18n's get locale and set the value in order to be fastest on routes that are not using (no need to reigster a middleware.)
return locale.GetMessage(message, values...)
func (ctx *Context) Tr(message string, values ...interface{}) string {
if locale := ctx.GetLocale(); locale != nil {
return locale.GetMessageContext(ctx, message, values...)
}
// This should never happen as the locale fallbacks to
// the default.
return message
}

View File

@@ -10,8 +10,8 @@ type I18nReadOnly interface {
Tr(lang string, format string, args ...interface{}) string
}
// Locale is the interface which returns from a `Localizer.GetLocale` metod.
// It serves the transltions based on "key" or format. See `GetMessage`.
// Locale is the interface which returns from a `Localizer.GetLocale` method.
// It serves the translations based on "key" or format. See `GetMessage`.
type Locale interface {
// Index returns the current locale index from the languages list.
Index() int
@@ -23,6 +23,12 @@ type Locale interface {
//
// Same as `Tag().String()` but it's static.
Language() string
// GetMessage should return translated text based n the given "key".
// GetMessage should return translated text based on the given "key".
GetMessage(key string, args ...interface{}) string
// GetMessageContext same as GetMessage
// but it accepts the Context as its first input.
// If DefaultMessageFunc was not nil then this Context
// will provide the real language input instead of the locale's which
// may be the default language one.
GetMessageContext(ctx *Context, key string, args ...interface{}) string
}