startpage/text.go

59 lines
997 B
Go
Raw Normal View History

2022-05-08 14:32:13 +02:00
package main
import (
"net/http"
"golang.org/x/text/language"
)
type Text struct {
Language string
Strings map[string]string
}
func (t *Text) Translate(str string) string {
if tr, ok := t.Strings[str]; ok {
return tr
}
return str
}
var (
Languages = map[language.Tag]Text{
language.English: {
Strings: map[string]string{
"Homepage": "Homepage",
"Search": "Search",
},
},
language.German: {
Strings: map[string]string{
"Homepage": "Homepage",
"Search": "Suche",
},
},
language.Japanese: {
Strings: map[string]string{
"Homepage": "ホームページ",
"Search": "検索",
},
},
}
)
var languageMatcher = language.NewMatcher([]language.Tag{
language.English,
language.German,
language.Japanese,
})
func GetText(r *http.Request) *Text {
lang, _ := language.MatchStrings(languageMatcher, r.Header.Get("Accept-Language"))
text := Languages[lang]
base, _ := lang.Base()
text.Language = base.String()
return &text
}