startpage/text.go
2022-09-05 14:17:07 +02:00

74 lines
1.6 KiB
Go

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",
"Search engine": "Search engine",
"Settings": "Settings",
"Background color": "Background color",
"Foreground color": "Foreground color",
"Border radius": "Border radius",
},
},
language.German: {
Strings: map[string]string{
"Homepage": "Homepage",
"Search": "Suche",
"Search engine": "Suchmaschine",
"Settings": "Einstellungen",
"Background color": "Hintergrundfarbe",
"Foreground color": "Textfarbe",
"Border radius": "Randradius",
},
},
language.Japanese: {
Strings: map[string]string{
"Homepage": "ホームページ",
"Search": "検索",
"Search engine": "検索エンジン",
"Settings": "設定",
"Background color": "背景色",
"Foreground color": "描画色",
"Border radius": "境界半径",
},
},
}
)
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
}