61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"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 {
|
|
fmt.Println(r.Header.Get("Accept-Language"))
|
|
lang, _ := language.MatchStrings(languageMatcher, r.Header.Get("Accept-Language"))
|
|
text := Languages[lang]
|
|
base, _ := lang.Base()
|
|
text.Language = base.String()
|
|
return &text
|
|
}
|