feature (languages): locale awareness for plugin

This commit is contained in:
MickaelK 2024-07-01 19:11:35 +10:00
parent d0e856fe90
commit 5099422836
2 changed files with 15 additions and 0 deletions

View file

@ -11,4 +11,5 @@ type App struct {
Share Share
Context context.Context
Authorization string
Languages []string
}

View file

@ -71,6 +71,8 @@ func SessionStart(fn HandlerFunc) HandlerFunc {
SendErrorResult(res, err)
return
}
ctx.Languages = _extractLanguages(req)
fn(ctx, res, req)
})
}
@ -327,3 +329,15 @@ func _extractSession(req *http.Request, ctx *App) (map[string]string, error) {
func _extractBackend(req *http.Request, ctx *App) (IBackend, error) {
return model.NewBackend(ctx, ctx.Session)
}
func _extractLanguages(req *http.Request) []string {
var lng = []string{}
for _, lngs := range strings.Split(req.Header.Get("Accept-Language"), ",") {
chunks := strings.Split(lngs, ";")
if len(chunks) == 0 {
continue
}
lng = append(lng, chunks[0])
}
return lng
}