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 (
|
2022-09-06 15:09:02 +02:00
|
|
|
Languages = map[string]Text{
|
|
|
|
"en": {
|
2022-05-08 14:32:13 +02:00
|
|
|
Strings: map[string]string{
|
2022-09-05 14:17:07 +02:00
|
|
|
"Homepage": "Homepage",
|
|
|
|
"Search": "Search",
|
|
|
|
"Search engine": "Search engine",
|
|
|
|
"Settings": "Settings",
|
|
|
|
"Background color": "Background color",
|
|
|
|
"Foreground color": "Foreground color",
|
|
|
|
"Border radius": "Border radius",
|
2022-09-07 11:27:35 +02:00
|
|
|
"Bookmark size": "Size",
|
2023-05-17 19:11:37 +02:00
|
|
|
"Hide bookmark": "Hide bookmark",
|
2022-05-08 14:32:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2022-09-06 15:09:02 +02:00
|
|
|
"de": {
|
2022-05-08 14:32:13 +02:00
|
|
|
Strings: map[string]string{
|
2022-09-05 14:17:07 +02:00
|
|
|
"Homepage": "Homepage",
|
|
|
|
"Search": "Suche",
|
|
|
|
"Search engine": "Suchmaschine",
|
|
|
|
"Settings": "Einstellungen",
|
|
|
|
"Background color": "Hintergrundfarbe",
|
|
|
|
"Foreground color": "Textfarbe",
|
|
|
|
"Border radius": "Randradius",
|
2022-09-07 11:27:35 +02:00
|
|
|
"Bookmark size": "Größe",
|
2023-05-17 19:11:37 +02:00
|
|
|
"Hide bookmark": "Verstecken",
|
2022-05-08 14:32:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2022-09-06 15:09:02 +02:00
|
|
|
"ja": {
|
2022-05-08 14:32:13 +02:00
|
|
|
Strings: map[string]string{
|
2022-09-05 14:17:07 +02:00
|
|
|
"Homepage": "ホームページ",
|
|
|
|
"Search": "検索",
|
|
|
|
"Search engine": "検索エンジン",
|
|
|
|
"Settings": "設定",
|
|
|
|
"Background color": "背景色",
|
|
|
|
"Foreground color": "描画色",
|
|
|
|
"Border radius": "境界半径",
|
2022-09-07 11:27:35 +02:00
|
|
|
"Bookmark size": "大小",
|
2023-05-17 19:11:37 +02:00
|
|
|
"Hide bookmark": "非表示",
|
2022-05-08 14:32:13 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2022-09-06 15:09:02 +02:00
|
|
|
text := Languages[base.String()]
|
2022-05-08 14:32:13 +02:00
|
|
|
text.Language = base.String()
|
|
|
|
return &text
|
|
|
|
}
|