mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
i18n: subdomain code: set from cookie if settings allow it, so multiple subdomain redirections can set the language correctly instead of falling back to the default one
This commit is contained in:
parent
177b05b674
commit
f8ac760f69
20
i18n/i18n.go
20
i18n/i18n.go
|
@ -405,6 +405,22 @@ func (i *I18n) GetMessage(ctx *context.Context, format string, args ...interface
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *I18n) setLangWithoutContext(w http.ResponseWriter, r *http.Request, lang string) {
|
||||||
|
if i.Cookie != "" {
|
||||||
|
http.SetCookie(w, &http.Cookie{
|
||||||
|
Name: i.Cookie,
|
||||||
|
Value: lang,
|
||||||
|
// allow subdomain sharing.
|
||||||
|
Domain: context.GetDomain(context.GetHost(r)),
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
})
|
||||||
|
} else if i.URLParameter != "" {
|
||||||
|
r.URL.Query().Set(i.URLParameter, lang)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Header.Set(acceptLanguageHeaderKey, lang)
|
||||||
|
}
|
||||||
|
|
||||||
// Wrapper returns a new router wrapper.
|
// Wrapper returns a new router wrapper.
|
||||||
// The result function can be passed on `Application.WrapRouter/AddRouterWrapper`.
|
// The result function can be passed on `Application.WrapRouter/AddRouterWrapper`.
|
||||||
// It compares the path prefix for translated language and
|
// It compares the path prefix for translated language and
|
||||||
|
@ -437,7 +453,7 @@ func (i *I18n) Wrapper() router.WrapperFunc {
|
||||||
|
|
||||||
r.RequestURI = path
|
r.RequestURI = path
|
||||||
r.URL.Path = path
|
r.URL.Path = path
|
||||||
r.Header.Set(acceptLanguageHeaderKey, lang)
|
i.setLangWithoutContext(w, r, lang)
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -450,7 +466,7 @@ func (i *I18n) Wrapper() router.WrapperFunc {
|
||||||
host = host[dotIdx+1:]
|
host = host[dotIdx+1:]
|
||||||
r.URL.Host = host
|
r.URL.Host = host
|
||||||
r.Host = host
|
r.Host = host
|
||||||
r.Header.Set(acceptLanguageHeaderKey, tag.String())
|
i.setLangWithoutContext(w, r, tag.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
iris.go
2
iris.go
|
@ -531,7 +531,7 @@ func (app *Application) Build() error {
|
||||||
if app.I18n.Loaded() {
|
if app.I18n.Loaded() {
|
||||||
// {{ tr "lang" "key" arg1 arg2 }}
|
// {{ tr "lang" "key" arg1 arg2 }}
|
||||||
app.view.AddFunc("tr", app.I18n.Tr)
|
app.view.AddFunc("tr", app.I18n.Tr)
|
||||||
app.Router.AddRouterWrapper(app.I18n.Wrapper())
|
app.Router.PrependRouterWrapper(app.I18n.Wrapper())
|
||||||
}
|
}
|
||||||
|
|
||||||
if n := app.view.Len(); n > 0 {
|
if n := app.view.Len(); n > 0 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user