updates
Some checks failed
build / build (push) Failing after 0s

This commit is contained in:
Tijl 2024-08-20 18:25:59 +02:00
parent 305203f033
commit e6d6ba4041
Signed by: tijl
GPG Key ID: DAE24BFCD722F053
18 changed files with 164 additions and 86 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"html/template"
"net/http" "net/http"
"git.tijl.dev/tijl/tijl.dev/internal/assets" "git.tijl.dev/tijl/tijl.dev/internal/assets"
@ -28,15 +27,6 @@ func main() {
engine.AddFunc("icon", assets.Svg) 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{ app := fiber.New(fiber.Config{
Views: engine, Views: engine,
DisableStartupMessage: true, DisableStartupMessage: true,
@ -45,27 +35,57 @@ func main() {
app.Use(fiberlogrus.New()) app.Use(fiberlogrus.New())
app.Get("/", func(c *fiber.Ctx) error { 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 { 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 { 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 { 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{ app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(static.Embed), 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")) log.Fatal(app.Listen(":3000"))
} }
func getCommon(c *fiber.Ctx) fiber.Map { func getCommon(c *fiber.Ctx) fiber.Map {
return fiber.Map{ return fiber.Map{
"Path": c.Path(), "Path": c.Path(),
"Language": i18n.GetLanguage(c),
"T": i18n.GetTranslations(i18n.GetLanguage(c)),
} }
} }

View File

@ -14,7 +14,7 @@ var SVGData map[string]string
func loadSVGs() { func loadSVGs() {
SVGData = make(map[string]string) SVGData = make(map[string]string)
dir := "static/assets" dir := "static/assets/icons"
files, err := filepath.Glob(filepath.Join(dir, "*.svg")) files, err := filepath.Glob(filepath.Join(dir, "*.svg"))
if err != nil { if err != nil {

View File

@ -45,7 +45,7 @@ func Load() {
} }
func Translate(c *fiber.Ctx, key string) string { func Translate(c *fiber.Ctx, key string) string {
lang := c.Locals("lang").(string) lang := c.Cookies("language")
if messages, ok := translations[lang]; ok { if messages, ok := translations[lang]; ok {
if message, ok := messages[key]; ok { if message, ok := messages[key]; ok {
return message return message
@ -59,6 +59,20 @@ func Translate(c *fiber.Ctx, key string) string {
return key return key
} }
func GetTranslations() map[string]map[string]string { func GetLanguage(c *fiber.Ctx) string {
return translations 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]
} }

View File

@ -6,5 +6,5 @@
"english": "English", "english": "English",
"dutch": "Dutch", "dutch": "Dutch",
"about": "About", "about": "About",
"projects": "projects" "projects": "Projects"
} }

View File

View File

Before

Width:  |  Height:  |  Size: 406 B

After

Width:  |  Height:  |  Size: 406 B

View File

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 382 B

View File

Before

Width:  |  Height:  |  Size: 328 B

After

Width:  |  Height:  |  Size: 328 B

View File

Before

Width:  |  Height:  |  Size: 301 B

After

Width:  |  Height:  |  Size: 301 B

View File

Before

Width:  |  Height:  |  Size: 313 B

After

Width:  |  Height:  |  Size: 313 B

View File

Before

Width:  |  Height:  |  Size: 271 B

After

Width:  |  Height:  |  Size: 271 B

1
views/404.html Normal file
View File

@ -0,0 +1 @@
<h2>404 not found</h2>

View File

@ -1,11 +1 @@
{{ template "layouts/base" . }} <h2>TODO About</h2>
{{define "title"}}{{translate "about"}}{{end}}
{{define "header"}}
{{template "partials/menu" .}}
{{end}}
{{ define "content" }}
<div>TODO About</div>
{{ end }}

View File

@ -1,11 +1 @@
{{ template "layouts/base" . }} <h2>TODO Blog</h2>
{{define "title"}}{{translate "blog"}}{{end}}
{{define "header"}}
{{template "partials/menu" .}}
{{end}}
{{ define "content" }}
<div>TODO Blog</div>
{{ end }}

View File

@ -1,12 +1,87 @@
{{ template "layouts/base" . }}
{{define "title"}}{{translate "home"}}{{end}}
{{define "header"}}
{{template "partials/menu" .}}
{{end}}
{{ define "content" }}
<h2>Welcome to My Go App</h2> <h2>Welcome to My Go App</h2>
<p>This is the homepage.</p> <p>This is the homepage.</p>
{{ end }} <p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
<p>This is the homepage.</p>
{{.T.about}}

View File

@ -1,25 +1,22 @@
<!doctype html> <!doctype html>
<html lang="{{block " language" .}}en{{end}}"> <html lang='{{.Language}}'>
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<link rel="stylesheet" href="static/css/styles.css" /> <link rel="stylesheet" href="static/css/styles.css" />
<!-- <link rel="icon" href="/favicon.png" /> --> <link rel="icon" href="/static/assets/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#f28c18" /> <meta name="theme-color" content="#f28c18" />
<!-- <script defer data-api="/analytics/api/event" data-domain="tijl.dev" src="/analytics/js/script.js"></script> --> <title>{{.Title}}</title>
<title>{{block "title" .}}{{end}}</title>
</head> </head>
<body class="lg:w-4/5 xl:w-3/4 mx-auto"> <body class="lg:max-w-[80%] xl:max-w-[75%] mx-auto">
<header> <header>
{{block "header" .}}{{end}} {{template "partials/menu" .}}
</header> </header>
<main class="mx-8 mt-4"> <main class="mx-8 pt-24">
{{block "content" .}}{{end}} {{embed}}
</main> </main>
<footer>
</footer>
</body> </body>
</html> </html>

View File

@ -1,4 +1,4 @@
<nav class="flex"> <nav class="flex fixed mx-auto w-full lg:w-[80%] xl:w-[75%]">
<div class="mt-4 mx-4 rounded-2xl shadow-2xl bg-base-300 navbar"> <div class="mt-4 mx-4 rounded-2xl shadow-2xl bg-base-300 navbar">
<div class="flex-1"> <div class="flex-1">
<a href="/" class="btn btn-ghost font-mono text-xl">tijl.dev</a> <a href="/" class="btn btn-ghost font-mono text-xl">tijl.dev</a>
@ -18,7 +18,7 @@
<span class="w-5 text-center"> <span class="w-5 text-center">
{{icon "home" }} {{icon "home" }}
</span> </span>
<span class="text-base">{{translate "home"}}</span> <span class="text-base">{{.T.home}}</span>
</a> </a>
</li> </li>
<li> <li>
@ -26,7 +26,7 @@
<span class=" w-5 text-center"> <span class=" w-5 text-center">
{{icon "blog"}} {{icon "blog"}}
</span> </span>
<span class="text-base">{{translate "blog"}}</span> <span class="text-base">{{.T.blog}}</span>
</a> </a>
</li> </li>
<li> <li>
@ -34,7 +34,7 @@
<span class="w-5 text-center"> <span class="w-5 text-center">
{{icon "projects"}} {{icon "projects"}}
</span> </span>
<span class="text-base">{{translate "projects"}}</span> <span class="text-base">{{.T.projects}}</span>
</a> </a>
</li> </li>
<li> <li>
@ -42,49 +42,50 @@
<span class="w-5 text-center"> <span class="w-5 text-center">
{{icon "about"}} {{icon "about"}}
</span> </span>
<span class="text-base">{{translate "about"}}</span> <span class="text-base">{{.T.about}}</span>
</a> </a>
</li> </li>
<!--<li class="flex-none"> <li class="flex-none">
<details class="dropdown"> <details class="dropdown">
<summary><a class="flex gap-4"> <summary><a class="flex gap-4">
<span class="w-5 text-center"> <span class="w-5 text-center">
{{icon "language"}} {{icon "language"}}
</span> </span>
<span class="text-base">{{translate "language"}}</span> <span class="text-base">{{.T.language}}</span>
</a> </a>
</summary> </summary>
<ul class="menu dropdown-content z-[12] bg-base-200 rounded-box w-48 p-3 shadow-xl gap-1"> <ul class="menu dropdown-content z-[12] bg-base-200 rounded-box w-48 p-3 shadow-xl gap-1">
<li> <li>
<a class="flex gap-4 active" href="/settings?lang=en"> <a class='flex gap-4 {{if eq .Language "en"}}active{{end}}'
href="/settings?lang=en&redirect={{.Path}}">
<span class="w-5 h-5 text-center"> <span class="w-5 h-5 text-center">
🇬🇧 🇬🇧
</span> </span>
<span class="text-base"> <span class="text-base">
{{translate "english"}} {{.T.english}}
</span> </span>
</a> </a>
</li> </li>
<li> <li>
<a class="flex gap-4" href="/settings?lang=nl"> <a class='flex gap-4 {{if eq .Language "nl"}}active{{end}}'
href="/settings?lang=nl&redirect={{.Path}}">
<span class="w-5 h-5 text-center"> <span class="w-5 h-5 text-center">
🇳🇱 🇳🇱
</span> </span>
<span class="text-base"> <span class="text-base">
{{translate "dutch"}} {{.T.dutch}}
</span> </span>
</a> </a>
</li> </li>
</ul> </ul>
</details> </details>
</li> </li>
-->
<li><a class="active flex gap-4" href="/login"> <li><a class="active flex gap-4" href="/login">
<span class="w-5 text-center"> <span class="w-5 text-center">
{{icon "login"}} {{icon "login"}}
</span> </span>
<span class="text-base">{{translate "login"}}</span> <span class="text-base">{{.T.login}}</span>
</a> </a>
</li> </li>
</ul> </ul>

View File

@ -1,11 +1 @@
{{ template "layouts/base" . }} <h2>TODO Projects</h2>
{{define "title"}}{{translate "projects"}}{{end}}
{{define "header"}}
{{template "partials/menu" .}}
{{end}}
{{ define "content" }}
<div>TODO Projects</div>
{{ end }}