Merge pull request #1958 from mattermost/PLT-7-client-cookie
Plt 7 client cookie
Этот коммит содержится в:
@@ -12,10 +12,6 @@ import (
|
|||||||
"github.com/nicksnyder/go-i18n/i18n"
|
"github.com/nicksnyder/go-i18n/i18n"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
SESSION_LOCALE = "MMLOCALE"
|
|
||||||
)
|
|
||||||
|
|
||||||
var T i18n.TranslateFunc
|
var T i18n.TranslateFunc
|
||||||
var locales map[string]string = make(map[string]string)
|
var locales map[string]string = make(map[string]string)
|
||||||
|
|
||||||
@@ -71,80 +67,13 @@ func SetTranslations(locale string) i18n.TranslateFunc {
|
|||||||
return translations
|
return translations
|
||||||
}
|
}
|
||||||
|
|
||||||
// func GetTranslations(w http.ResponseWriter, r *http.Request) i18n.TranslateFunc {
|
|
||||||
// translations, _ := getTranslationsAndLocale(w, r)
|
|
||||||
// return translations
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func GetTranslationsAndLocale(w http.ResponseWriter, r *http.Request) (i18n.TranslateFunc, string) {
|
|
||||||
// return getTranslationsAndLocale(w, r)
|
|
||||||
// }
|
|
||||||
|
|
||||||
func SetLocaleCookie(w http.ResponseWriter, lang string, sessionCacheInMinutes int) {
|
|
||||||
maxAge := (sessionCacheInMinutes * 60)
|
|
||||||
cookie := &http.Cookie{
|
|
||||||
Name: SESSION_LOCALE,
|
|
||||||
Value: lang,
|
|
||||||
Path: "/",
|
|
||||||
MaxAge: maxAge,
|
|
||||||
}
|
|
||||||
|
|
||||||
http.SetCookie(w, cookie)
|
|
||||||
}
|
|
||||||
|
|
||||||
// var keyRegexp = regexp.MustCompile(`:[[:word:]]+`)
|
|
||||||
// func MaybeExpandNamedText(text string, args ...interface{}) string {
|
|
||||||
// var (
|
|
||||||
// arg = args[0]
|
|
||||||
// argval = reflect.ValueOf(arg)
|
|
||||||
// )
|
|
||||||
// if argval.Kind() == reflect.Ptr {
|
|
||||||
// argval = argval.Elem()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if argval.Kind() == reflect.Map && argval.Type().Key().Kind() == reflect.String {
|
|
||||||
// return expandNamedText(text, func(key string) reflect.Value {
|
|
||||||
// return argval.MapIndex(reflect.ValueOf(key))
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// if argval.Kind() != reflect.Struct {
|
|
||||||
// return text
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return expandNamedText(text, argval.FieldByName)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func expandNamedText(text string, keyGetter func(key string) reflect.Value) string {
|
|
||||||
// return keyRegexp.ReplaceAllStringFunc(text, func(key string) string {
|
|
||||||
// val := keyGetter(key[1:])
|
|
||||||
// if !val.IsValid() {
|
|
||||||
// return key
|
|
||||||
// }
|
|
||||||
// newVar, _ := val.Interface().(string)
|
|
||||||
// return newVar
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
func GetTranslationsAndLocale(w http.ResponseWriter, r *http.Request) (i18n.TranslateFunc, string) {
|
func GetTranslationsAndLocale(w http.ResponseWriter, r *http.Request) (i18n.TranslateFunc, string) {
|
||||||
var translations i18n.TranslateFunc
|
headerLocale := strings.Split(strings.Split(r.Header.Get("Accept-Language"), ",")[0], "-")[0]
|
||||||
var _ error
|
if locales[headerLocale] != "" {
|
||||||
localeCookie := ""
|
translations, _ := i18n.Tfunc(headerLocale)
|
||||||
if cookie, err := r.Cookie(SESSION_LOCALE); err == nil {
|
return translations, headerLocale
|
||||||
localeCookie = cookie.Value
|
|
||||||
if locales[localeCookie] != "" {
|
|
||||||
translations, _ = i18n.Tfunc(localeCookie)
|
|
||||||
return translations, localeCookie
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
localeCookie = strings.Split(strings.Split(r.Header.Get("Accept-Language"), ",")[0], "-")[0]
|
translations, _ := i18n.Tfunc(model.DEFAULT_LOCALE)
|
||||||
if locales[localeCookie] != "" {
|
|
||||||
translations, _ = i18n.Tfunc(localeCookie)
|
|
||||||
SetLocaleCookie(w, localeCookie, 10)
|
|
||||||
return translations, localeCookie
|
|
||||||
}
|
|
||||||
|
|
||||||
translations, _ = i18n.Tfunc(model.DEFAULT_LOCALE)
|
|
||||||
SetLocaleCookie(w, model.DEFAULT_LOCALE, 10)
|
|
||||||
return translations, model.DEFAULT_LOCALE
|
return translations, model.DEFAULT_LOCALE
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,16 @@
|
|||||||
if ({{.SessionTokenIndex}} >= 0) {
|
if ({{.SessionTokenIndex}} >= 0) {
|
||||||
window.mm_session_token_index = {{.SessionTokenIndex}};
|
window.mm_session_token_index = {{.SessionTokenIndex}};
|
||||||
$.ajaxSetup({
|
$.ajaxSetup({
|
||||||
headers: { 'X-MM-TokenIndex': mm_session_token_index }
|
headers: {
|
||||||
|
'X-MM-TokenIndex': mm_session_token_index,
|
||||||
|
'Accept-Language': mm_locale
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$.ajaxSetup({
|
||||||
|
headers: {
|
||||||
|
'Accept-Language': mm_locale
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user