mirror of
https://github.com/Kugelschieber/schnittfest.git
synced 2026-01-18 10:20:27 +00:00
Added basic template rendering.
This commit is contained in:
39
util/lang.go
Normal file
39
util/lang.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
iso6391 "github.com/emvi/iso-639-1"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
supportedLangCodes = map[string]bool{"de": true}
|
||||
defaultLangCode = "de"
|
||||
)
|
||||
|
||||
func getLangCodeFromHeader(r *http.Request) string {
|
||||
header := r.Header.Get("Accept-Language")
|
||||
parts := strings.Split(header, ";")
|
||||
|
||||
if len(parts) == 0 || len(parts[0]) < 2 {
|
||||
return defaultLangCode
|
||||
}
|
||||
|
||||
code := strings.ToLower(parts[0][:2])
|
||||
|
||||
if iso6391.ValidCode(code) {
|
||||
return code
|
||||
}
|
||||
|
||||
return defaultLangCode
|
||||
}
|
||||
|
||||
func GetLangCode(r *http.Request) string {
|
||||
langCode := getLangCodeFromHeader(r)
|
||||
|
||||
if _, ok := supportedLangCodes[langCode]; ok {
|
||||
return langCode
|
||||
}
|
||||
|
||||
return defaultLangCode
|
||||
}
|
||||
Reference in New Issue
Block a user