1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 13:35:59 +00:00

add a 'Context.SetLanguage', similar to pp.I18n.ExtractFunc but it can be used per-handler

relative: https://github.com/kataras/iris/issues/1502#issuecomment-623553034

Former-commit-id: 7e2fadeb60149f43edbb9e220b0274ce1160881b
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-05-04 19:13:06 +03:00
parent 68fda360db
commit 20fcfb4110
6 changed files with 65 additions and 9 deletions

View File

@@ -320,21 +320,31 @@ const acceptLanguageHeaderKey = "Accept-Language"
// GetLocale returns the found locale of a request.
// It will return the first registered language if nothing else matched.
func (i *I18n) GetLocale(ctx context.Context) context.Locale {
// if v := ctx.Values().Get(ctx.Application().ConfigurationReadOnly().GetLocaleContextKey()); v != nil {
// if locale, ok := v.(context.Locale); ok {
// return locale
// }
// }
var (
index int
ok bool
)
if contextKey := ctx.Application().ConfigurationReadOnly().GetLanguageContextKey(); contextKey != "" {
if v := ctx.Values().GetString(contextKey); v != "" {
if v == "default" {
index = 0 // no need to call `TryMatchString` and spend time.
} else {
_, index, _ = i.TryMatchString(v)
}
locale := i.localizer.GetLocale(index)
if locale == nil {
return nil
}
return locale
}
}
if !ok && i.ExtractFunc != nil {
if v := i.ExtractFunc(ctx); v != "" {
_, index, ok = i.TryMatchString(v)
}
}