From e6d6ba40410ad4a28d9e70ee32eea0a9f92b162a Mon Sep 17 00:00:00 2001 From: tijl Date: Tue, 20 Aug 2024 18:25:59 +0200 Subject: [PATCH] updates --- cmd/server/main.go | 50 ++++++++++---- internal/assets/svg.go | 2 +- internal/i18n/i18n.go | 20 +++++- locales/en.json | 2 +- static/assets/favicon.ico | 0 static/assets/{ => icons}/about.svg | 0 static/assets/{ => icons}/blog.svg | 0 static/assets/{ => icons}/home.svg | 0 static/assets/{ => icons}/language.svg | 0 static/assets/{ => icons}/login.svg | 0 static/assets/{ => icons}/projects.svg | 0 views/404.html | 1 + views/about.html | 12 +--- views/blog.html | 12 +--- views/index.html | 95 +++++++++++++++++++++++--- views/layouts/base.html | 17 ++--- views/partials/menu.html | 27 ++++---- views/projects.html | 12 +--- 18 files changed, 164 insertions(+), 86 deletions(-) create mode 100644 static/assets/favicon.ico rename static/assets/{ => icons}/about.svg (100%) rename static/assets/{ => icons}/blog.svg (100%) rename static/assets/{ => icons}/home.svg (100%) rename static/assets/{ => icons}/language.svg (100%) rename static/assets/{ => icons}/login.svg (100%) rename static/assets/{ => icons}/projects.svg (100%) create mode 100644 views/404.html diff --git a/cmd/server/main.go b/cmd/server/main.go index da751f6..e4039c5 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,7 +1,6 @@ package main import ( - "html/template" "net/http" "git.tijl.dev/tijl/tijl.dev/internal/assets" @@ -28,15 +27,6 @@ func main() { engine.AddFunc("icon", assets.Svg) - // Todo better language system - engine.AddFunc("translate", func(key string) template.HTML { - translations := i18n.GetTranslations()["en"] // for now we just fix it to english - if translation, ok := translations[key]; ok { - return template.HTML(translation) - } - return "" - }) - app := fiber.New(fiber.Config{ Views: engine, DisableStartupMessage: true, @@ -45,27 +35,57 @@ func main() { app.Use(fiberlogrus.New()) app.Get("/", func(c *fiber.Ctx) error { - return c.Render("index", getCommon(c)) + data := getCommon(c) + data["Title"] = i18n.Translate(c, "home") + return c.Render("index", data, "layouts/base") }) app.Get("/blog", func(c *fiber.Ctx) error { - return c.Render("blog", getCommon(c)) + data := getCommon(c) + data["Title"] = i18n.Translate(c, "blog") + return c.Render("blog", data, "layouts/base") }) app.Get("/projects", func(c *fiber.Ctx) error { - return c.Render("projects", getCommon(c)) + data := getCommon(c) + data["Title"] = i18n.Translate(c, "projects") + return c.Render("projects", data, "layouts/base") }) app.Get("/about", func(c *fiber.Ctx) error { - return c.Render("about", getCommon(c)) + data := getCommon(c) + data["Title"] = i18n.Translate(c, "about") + return c.Render("about", data, "layouts/base") + }) + app.Get("/settings", func(c *fiber.Ctx) error { + lang := c.Query("lang") + redirectURL := c.Query("redirect") + if lang != "" { + c.Cookie(&fiber.Cookie{ + Name: "language", + Value: lang, + Secure: false, + }) + } + if redirectURL != "" { + return c.Redirect(redirectURL) + } + return nil }) app.Use("/static", filesystem.New(filesystem.Config{ Root: http.FS(static.Embed), })) + app.Use(func(c *fiber.Ctx) error { + data := getCommon(c) + return c.Render("404", data, "layouts/base") + }) + log.Fatal(app.Listen(":3000")) } func getCommon(c *fiber.Ctx) fiber.Map { return fiber.Map{ - "Path": c.Path(), + "Path": c.Path(), + "Language": i18n.GetLanguage(c), + "T": i18n.GetTranslations(i18n.GetLanguage(c)), } } diff --git a/internal/assets/svg.go b/internal/assets/svg.go index 03435b6..057b5c8 100644 --- a/internal/assets/svg.go +++ b/internal/assets/svg.go @@ -14,7 +14,7 @@ var SVGData map[string]string func loadSVGs() { SVGData = make(map[string]string) - dir := "static/assets" + dir := "static/assets/icons" files, err := filepath.Glob(filepath.Join(dir, "*.svg")) if err != nil { diff --git a/internal/i18n/i18n.go b/internal/i18n/i18n.go index b6d512d..7140363 100644 --- a/internal/i18n/i18n.go +++ b/internal/i18n/i18n.go @@ -45,7 +45,7 @@ func Load() { } func Translate(c *fiber.Ctx, key string) string { - lang := c.Locals("lang").(string) + lang := c.Cookies("language") if messages, ok := translations[lang]; ok { if message, ok := messages[key]; ok { return message @@ -59,6 +59,20 @@ func Translate(c *fiber.Ctx, key string) string { return key } -func GetTranslations() map[string]map[string]string { - return translations +func GetLanguage(c *fiber.Ctx) string { + lang := c.Cookies("language") + + if _, ok := translations[lang]; ok { + return lang + } else { + return DefaultLang + } +} + +func GetTranslations(lang string) map[string]string { + if messages, ok := translations[lang]; ok { + return messages + } + + return translations[DefaultLang] } diff --git a/locales/en.json b/locales/en.json index 522795b..4a11c2f 100644 --- a/locales/en.json +++ b/locales/en.json @@ -6,5 +6,5 @@ "english": "English", "dutch": "Dutch", "about": "About", - "projects": "projects" + "projects": "Projects" } diff --git a/static/assets/favicon.ico b/static/assets/favicon.ico new file mode 100644 index 0000000..e69de29 diff --git a/static/assets/about.svg b/static/assets/icons/about.svg similarity index 100% rename from static/assets/about.svg rename to static/assets/icons/about.svg diff --git a/static/assets/blog.svg b/static/assets/icons/blog.svg similarity index 100% rename from static/assets/blog.svg rename to static/assets/icons/blog.svg diff --git a/static/assets/home.svg b/static/assets/icons/home.svg similarity index 100% rename from static/assets/home.svg rename to static/assets/icons/home.svg diff --git a/static/assets/language.svg b/static/assets/icons/language.svg similarity index 100% rename from static/assets/language.svg rename to static/assets/icons/language.svg diff --git a/static/assets/login.svg b/static/assets/icons/login.svg similarity index 100% rename from static/assets/login.svg rename to static/assets/icons/login.svg diff --git a/static/assets/projects.svg b/static/assets/icons/projects.svg similarity index 100% rename from static/assets/projects.svg rename to static/assets/icons/projects.svg diff --git a/views/404.html b/views/404.html new file mode 100644 index 0000000..ea65cc4 --- /dev/null +++ b/views/404.html @@ -0,0 +1 @@ +

404 not found

diff --git a/views/about.html b/views/about.html index eddc429..2d7fe59 100644 --- a/views/about.html +++ b/views/about.html @@ -1,11 +1 @@ -{{ template "layouts/base" . }} - -{{define "title"}}{{translate "about"}}{{end}} - -{{define "header"}} -{{template "partials/menu" .}} -{{end}} - -{{ define "content" }} -
TODO About
-{{ end }} +

TODO About

diff --git a/views/blog.html b/views/blog.html index 94592e1..95f52c0 100644 --- a/views/blog.html +++ b/views/blog.html @@ -1,11 +1 @@ -{{ template "layouts/base" . }} - -{{define "title"}}{{translate "blog"}}{{end}} - -{{define "header"}} -{{template "partials/menu" .}} -{{end}} - -{{ define "content" }} -
TODO Blog
-{{ end }} +

TODO Blog

diff --git a/views/index.html b/views/index.html index 314e748..2128b25 100644 --- a/views/index.html +++ b/views/index.html @@ -1,12 +1,87 @@ -{{ template "layouts/base" . }} - -{{define "title"}}{{translate "home"}}{{end}} - -{{define "header"}} -{{template "partials/menu" .}} -{{end}} - -{{ define "content" }}

Welcome to My Go App

This is the homepage.

-{{ end }} +

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+

This is the homepage.

+{{.T.about}} diff --git a/views/layouts/base.html b/views/layouts/base.html index 92f8fe5..beec45e 100644 --- a/views/layouts/base.html +++ b/views/layouts/base.html @@ -1,25 +1,22 @@ - + - + - - {{block "title" .}}{{end}} + {{.Title}} - +
- {{block "header" .}}{{end}} + {{template "partials/menu" .}}
-
- {{block "content" .}}{{end}} +
+ {{embed}}
-
-
diff --git a/views/partials/menu.html b/views/partials/menu.html index 4380888..0749f48 100644 --- a/views/partials/menu.html +++ b/views/partials/menu.html @@ -1,4 +1,4 @@ -