startpage/text.go
2022-09-07 11:27:35 +02:00

77 lines
1.7 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[string]Text{
"en": {
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",
"Bookmark size": "Size",
},
},
"de": {
Strings: map[string]string{
"Homepage": "Homepage",
"Search": "Suche",
"Search engine": "Suchmaschine",
"Settings": "Einstellungen",
"Background color": "Hintergrundfarbe",
"Foreground color": "Textfarbe",
"Border radius": "Randradius",
"Bookmark size": "Größe",
},
},
"ja": {
Strings: map[string]string{
"Homepage": "ホームページ",
"Search": "検索",
"Search engine": "検索エンジン",
"Settings": "設定",
"Background color": "背景色",
"Foreground color": "描画色",
"Border radius": "境界半径",
"Bookmark size": "大小",
},
},
}
)
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"))
base, _ := lang.Base()
text := Languages[base.String()]
text.Language = base.String()
return &text
}