From e092a34055bb3ce63fcb635f5ea833accba11011 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 17 Sep 2024 04:06:22 +0400 Subject: [PATCH] Index was translated. Also all necessary i18n code was added. --- cmd/dwelling-radio/main.go | 33 ++++++++++++++++++++++++++++++--- web/index.templ | 37 +++++++++++++++++++++---------------- web/locales/en/en.yaml | 19 +++++++++++++++++++ web/locales/locales.go | 7 +++++++ web/locales/ru/ru.yaml | 19 +++++++++++++++++++ 5 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 web/locales/en/en.yaml create mode 100644 web/locales/locales.go create mode 100644 web/locales/ru/ru.yaml diff --git a/cmd/dwelling-radio/main.go b/cmd/dwelling-radio/main.go index 30a3810..6f72a5a 100644 --- a/cmd/dwelling-radio/main.go +++ b/cmd/dwelling-radio/main.go @@ -1,12 +1,12 @@ package main import ( - "context" ihttp "dwelling-radio/internal/http" "dwelling-radio/internal/radio" sqlite_stats "dwelling-radio/internal/statistics/db/sqlite" "dwelling-radio/pkg/utils" "dwelling-radio/web" + "dwelling-radio/web/locales" "flag" "fmt" "log" @@ -17,6 +17,7 @@ import ( "syscall" "git.arav.su/Arav/httpr" + "github.com/invopop/ctxi18n" ) var ( @@ -53,6 +54,10 @@ func main() { log.Fatalln(err) } + if err := ctxi18n.LoadWithDefault(locales.Content, "en"); err != nil { + log.Fatalln(err) + } + r := httpr.New() r.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) { @@ -63,7 +68,7 @@ func main() { lstnrs.RLock() defer lstnrs.RUnlock() - web.Index(¤tSong, lst, *songListLen, lstnrs, r).Render(context.Background(), w) + web.Index(version, ¤tSong, lst, *songListLen, lstnrs, r).Render(r.Context(), w) }) r.Handler(http.MethodGet, "/filelist", func(w http.ResponseWriter, r *http.Request) { @@ -94,7 +99,7 @@ func main() { s.Handler(http.MethodGet, "/playlist", djh.PlaylistNext) s.Handler(http.MethodGet, "/status", djh.Status) - srv := ihttp.NewHttpServer(r) + srv := ihttp.NewHttpServer(I18nMiddleware(r)) if err := srv.Start(*listenAddress); err != nil { log.Fatalln(err) @@ -130,3 +135,25 @@ func main() { } } } + +func I18nMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + lang := "en" + + if lq := r.URL.Query().Get("lang"); lq != "" { + lc := http.Cookie{Name: "lang", Value: lq, HttpOnly: false, MaxAge: 0} + http.SetCookie(w, &lc) + lang = lq + } else if l, err := r.Cookie("lang"); err == nil { + lang = l.Value + } else if al := r.Header.Get("Accept-Language"); al != "" { + lang = r.Header.Get("Accept-Language") + } + + ctx, err := ctxi18n.WithLocale(r.Context(), lang) + if err != nil { + log.Println("i18nmw:", err) + } + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} diff --git a/web/index.templ b/web/index.templ index 06a8f53..971236c 100644 --- a/web/index.templ +++ b/web/index.templ @@ -3,12 +3,14 @@ package web import "net/http" import "strconv" +import "github.com/invopop/ctxi18n/i18n" + import "dwelling-radio/internal/radio" import "dwelling-radio/pkg/utils" -templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.ListenerCounter, r *http.Request) { +templ Index(prgVer string, curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.ListenerCounter, r *http.Request) { - + @@ -16,11 +18,11 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis - Arav's dwelling / Radio + Arav's dwelling / { i18n.T(ctx, "title") } - - + + @@ -36,8 +38,8 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis Welcome to my sacred place, wanderer

@@ -92,13 +94,13 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis

-

Last { strconv.FormatInt(slLen, 10) } songs

+

{ i18n.T(ctx, "last-songs.h", i18n.M{"n": strconv.FormatInt(slLen, 10)}) }

- - - + + + @@ -117,7 +119,10 @@ templ Index(curSong *radio.Song, sl []radio.Song, slLen int64, lstnrs *radio.Lis
StartO/PSong{ i18n.T(ctx, "last-songs.tab-start") }{ i18n.T(ctx, "last-songs.tab-stat") }{ i18n.T(ctx, "last-songs.tab-song") }
diff --git a/web/locales/en/en.yaml b/web/locales/en/en.yaml new file mode 100644 index 0000000..f1b5403 --- /dev/null +++ b/web/locales/en/en.yaml @@ -0,0 +1,19 @@ +en: + title: Radio + description: Internet-radio broadcasting from under my desk. + keywords: self-host radio home-radio various music + back-home: Back home + link: + filelist: filelist + playlist: playlist + direct-link: direct link + no-audio-tag: Seems like your browser doesn't support an audio element, but you can grab the + last-songs: + h: Last %{n} songs + tab-start: Start + tab-stat: O/P + tab-stat-tip: Overall/Peak listeners + tab-song: Song + footer: + author: Alexander ❝Arav❞ Andreev + privacy: Privacy statements \ No newline at end of file diff --git a/web/locales/locales.go b/web/locales/locales.go new file mode 100644 index 0000000..10b2462 --- /dev/null +++ b/web/locales/locales.go @@ -0,0 +1,7 @@ +package locales + +import "embed" + +//go:embed en +//go:embed ru +var Content embed.FS diff --git a/web/locales/ru/ru.yaml b/web/locales/ru/ru.yaml new file mode 100644 index 0000000..bb2c61d --- /dev/null +++ b/web/locales/ru/ru.yaml @@ -0,0 +1,19 @@ +ru: + title: Радио + description: Интернет-радио вещающееся из-под моего стола. + keywords: само-хост селф-хост радио разное музыка + back-home: Назад домой + link: + filelist: список файлов + playlist: плейлист + direct-link: прямая ссылка + no-audio-tag: Похоже на то, что твой браузер не поддерживает audio элемент, хреновенько, но можешь взять + last-songs: + h: Последние %{n} песен + tab-start: Начало + tab-stat: В/П + tab-stat-tip: Всего/Пиковое кол-во слушателей + tab-song: Песня + footer: + author: Александр «Arav» Андреев + privacy: О приватности \ No newline at end of file