startpage/text.go

80 lines
1.8 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",
"Hide bookmark": "Hide bookmark",
},
},
"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",
"Hide bookmark": "Verstecken",
},
},
"ja": {
Strings: map[string]string{
"Homepage": "ホームページ",
"Search": "検索",
"Search engine": "検索エンジン",
"Settings": "設定",
"Background color": "背景色",
"Foreground color": "描画色",
"Border radius": "境界半径",
"Bookmark size": "大小",
"Hide bookmark": "非表示",
},
},
}
)
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
}