🏗️ Use subfolders for translations

This commit is contained in:
Maxim Lebedev 2023-11-11 23:53:58 +06:00
parent 61cc8075db
commit 3296649cae
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
2 changed files with 49 additions and 8 deletions

View File

@ -1,13 +1,14 @@
package common
const (
HeaderAcceptLanguage string = "Accept-Language"
HeaderContentType string = "Content-Type"
HeaderAcceptLanguage string = "Accept-Language"
HeaderContentLanguage string = "Content-Language"
HeaderContentType string = "Content-Type"
)
const (
MIMETextHTML string = "text/html"
MIMETextHTMLCharsetUTF8 string = MIMETextHTML + "; " + charsetUTF8
MIMETextHTML string = "text/html"
MIMETextHTMLCharsetUTF8 string = MIMETextHTML + "; " + charsetUTF8
)
const charsetUTF8 string = "charset=UTF-8"

48
main.go
View File

@ -18,6 +18,7 @@ import (
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"syscall"
"time"
_ "time/tzdata"
@ -38,6 +39,7 @@ import (
"source.toby3d.me/toby3d/home/internal/templateutil"
themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs"
themeucase "source.toby3d.me/toby3d/home/internal/theme/usecase"
"source.toby3d.me/toby3d/home/internal/urlutil"
)
type (
@ -81,12 +83,42 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
server := &http.Server{
Addr: config.AddrPort().String(),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tags, _, err := language.ParseAcceptLanguage(r.Header.Get(common.HeaderAcceptLanguage))
if err != nil || len(tags) == 0 {
tags = append(tags, language.English)
head, tail := urlutil.ShiftPath(r.URL.Path)
if head == "" {
tags, _, err := language.ParseAcceptLanguage(r.Header.Get(common.HeaderAcceptLanguage))
if err != nil || len(tags) == 0 {
tags = append(tags, language.English)
}
lang, _, _ := matcher.Match(tags...)
http.Redirect(w, r, "/"+lang.String()+"/", http.StatusSeeOther)
return
}
lang, _, _ := matcher.Match(tags...)
lang, err := language.Parse(head)
if err != nil || lang == language.Und {
res, err := staticer.Do(r.Context(), r.URL.Path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeContent(w, r, res.Name(), domain.ResourceModTime(res), res)
return
}
r.URL.Path = tail
s, err := siter.Do(r.Context(), lang)
if err != nil {
@ -121,6 +153,14 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
return
}
contentLanguage := make([]string, len(p.Translations))
for i := range p.Translations {
base, _ := p.Translations[i].Language.Base()
contentLanguage[i] = base.String()
}
w.Header().Set(common.HeaderContentLanguage, strings.Join(contentLanguage, ", "))
tpl, err := themer.Do(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)