🏗️ 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 package common
const ( const (
HeaderAcceptLanguage string = "Accept-Language" HeaderAcceptLanguage string = "Accept-Language"
HeaderContentType string = "Content-Type" HeaderContentLanguage string = "Content-Language"
HeaderContentType string = "Content-Type"
) )
const ( const (
MIMETextHTML string = "text/html" MIMETextHTML string = "text/html"
MIMETextHTMLCharsetUTF8 string = MIMETextHTML + "; " + charsetUTF8 MIMETextHTMLCharsetUTF8 string = MIMETextHTML + "; " + charsetUTF8
) )
const charsetUTF8 string = "charset=UTF-8" const charsetUTF8 string = "charset=UTF-8"

48
main.go
View File

@ -18,6 +18,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"strings"
"syscall" "syscall"
"time" "time"
_ "time/tzdata" _ "time/tzdata"
@ -38,6 +39,7 @@ import (
"source.toby3d.me/toby3d/home/internal/templateutil" "source.toby3d.me/toby3d/home/internal/templateutil"
themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs" themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs"
themeucase "source.toby3d.me/toby3d/home/internal/theme/usecase" themeucase "source.toby3d.me/toby3d/home/internal/theme/usecase"
"source.toby3d.me/toby3d/home/internal/urlutil"
) )
type ( type (
@ -81,12 +83,42 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
server := &http.Server{ server := &http.Server{
Addr: config.AddrPort().String(), Addr: config.AddrPort().String(),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tags, _, err := language.ParseAcceptLanguage(r.Header.Get(common.HeaderAcceptLanguage)) head, tail := urlutil.ShiftPath(r.URL.Path)
if err != nil || len(tags) == 0 {
tags = append(tags, language.English) 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) s, err := siter.Do(r.Context(), lang)
if err != nil { if err != nil {
@ -121,6 +153,14 @@ func NewApp(ctx context.Context, config *domain.Config) (*App, error) {
return 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()) tpl, err := themer.Do(r.Context())
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)