summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-08-07 01:51:13 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-08-07 01:51:13 +0200
commit3cb7572a8f1092b3169f4faaa2e76776dea39993 (patch)
tree62bdfdf1547fa88f5b25e4da1aac8d734906509f
parente555accb612e189ab9bdad403958d4e0a6d809ae (diff)
Just use hardcoded strings
-rw-r--r--main.go6
-rw-r--r--middleware/i18n.go6
-rw-r--r--templates/base.go5
-rw-r--r--templates/navbar.templ2
-rw-r--r--templates/navbar_templ.go2
-rw-r--r--templates/root.templ8
-rw-r--r--templates/root_templ.go43
7 files changed, 26 insertions, 46 deletions
diff --git a/main.go b/main.go
index 6ecdf32..e0c88ef 100644
--- a/main.go
+++ b/main.go
@@ -40,7 +40,7 @@ func main() {
}
func finalHandler(w http.ResponseWriter, r *http.Request) {
- p := r.Context().Value(middleware.PrinterKey).(i18n.Printer)
+ p := r.Context().Value("printer").(i18n.Printer)
/* Strip trailing slash from the URL */
path := r.URL.Path
@@ -57,7 +57,7 @@ func finalHandler(w http.ResponseWriter, r *http.Request) {
}
func setUserLanguage(w http.ResponseWriter, r *http.Request) {
- loc := r.FormValue(templates.LocaleKey)
+ loc := r.FormValue("locale")
_, ok := i18n.Printers[strings.ToLower(loc)]
if !ok {
w.WriteHeader(http.StatusBadRequest)
@@ -65,7 +65,7 @@ func setUserLanguage(w http.ResponseWriter, r *http.Request) {
return
}
http.SetCookie(w, &http.Cookie{
- Name: "lang",
+ Name: "locale",
Value: loc,
MaxAge: math.MaxInt32,
})
diff --git a/middleware/i18n.go b/middleware/i18n.go
index a30a9f6..0ed07d4 100644
--- a/middleware/i18n.go
+++ b/middleware/i18n.go
@@ -12,13 +12,11 @@ import (
"git.thomasvoss.com/euro-cash.eu/templates"
)
-const PrinterKey = "printer"
-
func I18n(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var p, pZero i18n.Printer
- if c, err := r.Cookie("lang"); errors.Is(err, http.ErrNoCookie) {
+ if c, err := r.Cookie("locale"); errors.Is(err, http.ErrNoCookie) {
log.Println("Language cookie not set")
} else {
var ok bool
@@ -29,7 +27,7 @@ func I18n(next http.Handler) http.Handler {
}
ctx := context.WithValue(
- r.Context(), PrinterKey, cmp.Or(p, i18n.DefaultPrinter))
+ r.Context(), "printer", cmp.Or(p, i18n.DefaultPrinter))
if p == pZero {
http.SetCookie(w, &http.Cookie{
diff --git a/templates/base.go b/templates/base.go
index f0b288b..e6647f2 100644
--- a/templates/base.go
+++ b/templates/base.go
@@ -1,8 +1,3 @@
package templates
-const (
- PrinterKey = "printer"
- LocaleKey = "locale"
-)
-
//go:generate templ generate -log-level warn
diff --git a/templates/navbar.templ b/templates/navbar.templ
index 95e8563..5007183 100644
--- a/templates/navbar.templ
+++ b/templates/navbar.templ
@@ -3,7 +3,7 @@ package templates
import "git.thomasvoss.com/euro-cash.eu/i18n"
templ navbar() {
- {{ p := ctx.Value(PrinterKey).(i18n.Printer) }}
+ {{ p := ctx.Value("printer").(i18n.Printer) }}
<nav>
<menu>
diff --git a/templates/navbar_templ.go b/templates/navbar_templ.go
index 90fa955..942f644 100644
--- a/templates/navbar_templ.go
+++ b/templates/navbar_templ.go
@@ -28,7 +28,7 @@ func navbar() templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
- p := ctx.Value(PrinterKey).(i18n.Printer)
+ p := ctx.Value("printer").(i18n.Printer)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<nav><menu><li><a href=\"/\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
diff --git a/templates/root.templ b/templates/root.templ
index 722aa2c..7a565ae 100644
--- a/templates/root.templ
+++ b/templates/root.templ
@@ -4,7 +4,7 @@ import "strings"
import "git.thomasvoss.com/euro-cash.eu/i18n"
templ Root(head, body templ.Component) {
- {{ p := ctx.Value(PrinterKey).(i18n.Printer) }}
+ {{ p := ctx.Value("printer").(i18n.Printer) }}
<!DOCTYPE html>
<html lang={ p.Lang } data-theme="dark">
@@ -34,7 +34,7 @@ templ Root(head, body templ.Component) {
}
templ Index() {
- {{ p := ctx.Value(PrinterKey).(i18n.Printer) }}
+ {{ p := ctx.Value("printer").(i18n.Printer) }}
<header>
@navbar()
@@ -49,7 +49,7 @@ templ Index() {
}
templ SetLanguage() {
- {{ p := ctx.Value(PrinterKey).(i18n.Printer) }}
+ {{ p := ctx.Value("printer").(i18n.Printer) }}
<header>
@navbar()
@@ -83,7 +83,7 @@ templ languageGrid(eurozone bool) {
if loc.Eurozone == eurozone {
<button
type="submit"
- name={ LocaleKey }
+ name="locale"
value={ loc.Code }
disabled?={ !loc.Enabled }
>
diff --git a/templates/root_templ.go b/templates/root_templ.go
index d35ddd2..14e434c 100644
--- a/templates/root_templ.go
+++ b/templates/root_templ.go
@@ -29,7 +29,7 @@ func Root(head, body templ.Component) templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
- p := ctx.Value(PrinterKey).(i18n.Printer)
+ p := ctx.Value("printer").(i18n.Printer)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html><html lang=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
@@ -115,7 +115,7 @@ func Index() templ.Component {
templ_7745c5c3_Var5 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
- p := ctx.Value(PrinterKey).(i18n.Printer)
+ p := ctx.Value("printer").(i18n.Printer)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<header>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
@@ -215,7 +215,7 @@ func SetLanguage() templ.Component {
templ_7745c5c3_Var11 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
- p := ctx.Value(PrinterKey).(i18n.Printer)
+ p := ctx.Value("printer").(i18n.Printer)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<header>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
@@ -324,29 +324,16 @@ func languageGrid(eurozone bool) templ.Component {
}
for _, loc := range i18n.Locales {
if loc.Eurozone == eurozone {
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<button type=\"submit\" name=\"")
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<button type=\"submit\" name=\"locale\" value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var17 string
- templ_7745c5c3_Var17, templ_7745c5c3_Err = templ.JoinStringErrs(LocaleKey)
- if templ_7745c5c3_Err != nil {
- return templ.Error{Err: templ_7745c5c3_Err, FileName: `root.templ`, Line: 86, Col: 22}
- }
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var17))
- if templ_7745c5c3_Err != nil {
- return templ_7745c5c3_Err
- }
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" value=\"")
- if templ_7745c5c3_Err != nil {
- return templ_7745c5c3_Err
- }
- var templ_7745c5c3_Var18 string
- templ_7745c5c3_Var18, templ_7745c5c3_Err = templ.JoinStringErrs(loc.Code)
+ templ_7745c5c3_Var17, templ_7745c5c3_Err = templ.JoinStringErrs(loc.Code)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `root.templ`, Line: 87, Col: 22}
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var18))
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var17))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@@ -364,12 +351,12 @@ func languageGrid(eurozone bool) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- var templ_7745c5c3_Var19 string
- templ_7745c5c3_Var19, templ_7745c5c3_Err = templ.JoinStringErrs(loc.Code)
+ var templ_7745c5c3_Var18 string
+ templ_7745c5c3_Var18, templ_7745c5c3_Err = templ.JoinStringErrs(loc.Code)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `root.templ`, Line: 91, Col: 22}
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var19))
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var18))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@@ -377,12 +364,12 @@ func languageGrid(eurozone bool) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- var templ_7745c5c3_Var20 string
- templ_7745c5c3_Var20, templ_7745c5c3_Err = templ.JoinStringErrs(strings.ToUpper(loc.Language()))
+ var templ_7745c5c3_Var19 string
+ templ_7745c5c3_Var19, templ_7745c5c3_Err = templ.JoinStringErrs(strings.ToUpper(loc.Language()))
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `root.templ`, Line: 92, Col: 50}
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var20))
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var19))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@@ -390,12 +377,12 @@ func languageGrid(eurozone bool) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
- var templ_7745c5c3_Var21 string
- templ_7745c5c3_Var21, templ_7745c5c3_Err = templ.JoinStringErrs(loc.Name)
+ var templ_7745c5c3_Var20 string
+ templ_7745c5c3_Var20, templ_7745c5c3_Err = templ.JoinStringErrs(loc.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `root.templ`, Line: 94, Col: 17}
}
- _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var21))
+ _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var20))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}