Comments (4)
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/gofiber/template/django"
"log"
)
func trans(s string) string {
// TODO: How to get *fiber.Ctx
//sess, _ := session.New().Get(c)
//lang := sess.Get("lang")
return s
}
func main() {
// Create a new engine
engine := django.New("./views", ".django")
engine.AddFunc("t", trans)
app := fiber.New(fiber.Config{Views: engine})
app.Get("/i18n", func(c *fiber.Ctx) error {
sess, _ := session.New().Get(c)
sess.Set("lang", "en_US")
sess.Save()
return c.SendString("i18n")
})
log.Fatal(app.Listen(":3000"))
}
from template.
Can you provide your example where you use the render method and a template?
From the gut i would just say add the context in the data you pass to the render method or just the information that is needed like the language.
Then just use it as a parameter in the function.
from template.
Thanks for reply.
template html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
Index page.
<h5>{{ _("Hello World") }}</h5>
</body>
</html>
I also used a method similar to yours, It work!!! But every time you need to get the default language in the router.
demo
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/gofiber/template/django"
"log"
)
func trans(lang, s string) string {
// locale := someLocales[lang]
// return locale.get(s)
return s
}
func main() {
// Create a new engine
engine := django.New("./views", ".django")
engine.AddFunc("t", trans)
app := fiber.New(fiber.Config{Views: engine})
app.Get("/", func(c *fiber.Ctx) error {
sess, _ := session.New().Get(c)
return c.Render("index", fiber.Map{"lang": sess.Get("lang")})
})
app.Get("/i18n", func(c *fiber.Ctx) error {
sess, _ := session.New().Get(c)
sess.Set("lang", "en_US")
sess.Save()
return c.Redirect("/")
})
log.Fatal(app.Listen(":3000"))
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
Index page.
<h5>{{ _(lang, "Hello World") }}</h5>
</body>
</html>
from template.
go code:
package main
import (
"fmt"
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/gofiber/template/django"
)
func trans(s, lang string) string {
//fmt.Printf("%+v\n%+v\n", s, lang)
// locale := someLocales[lang]
// return locale.get(s)
return s
}
func main() {
// Create a new engine
engine := django.New("./views", ".django")
engine.AddFunc("t", trans)
// initialize the session manager outside,
// otherwise it will create a new memory storage every time, when the request is comming
sessionManager := session.New()
app := fiber.New(fiber.Config{Views: engine})
app.Get("/", func(c *fiber.Ctx) error {
sess, _ := sessionManager.Get(c)
lang := sess.Get("lang")
if lang == nil {
lang = "default"
}
fmt.Println(lang)
return c.Render("index", fiber.Map{"lang": lang})
})
app.Get("/i18n", func(c *fiber.Ctx) error {
sess, _ := sessionManager.Get(c)
sess.Set("lang", "en_US")
sess.Save()
return c.Redirect("/")
})
log.Fatal(app.Listen(":3000"))
}
template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
Index page.
<h5>{{ t("Hello World", lang) }}</h5>
</body>
</html>
i think you(@ly020044) will understood the changes, if not ask me
when you have no more questions you can close the issue
from template.
Related Issues (20)
- [Improvement]: 🚀 Interface for the template engines HOT 2
- [Improvement]: 🚀 Better benchmark statistic HOT 1
- 🐛 [Bug]: Draft new release version for template as well
- 🐛 [Bug]: django is broken after v3.1.0 HOT 2
- 🐛 [Bug]: HOT 2
- 🐛 [Bug]: v1.8.2 appears to be missing github.com/gofiber/template/html HOT 2
- 🚀 [Feature]: htmx support HOT 10
- 🐛 [Bug]: Django (pongo2) integration vulnerability to XSS HOT 10
- 🤗 [Question]: How can i divide my layout, component and views folders HOT 4
- 🤗 [Question]: How to export mustache engine as a template.IEngine ? HOT 1
- 🚀 [Feature]: Templ support? HOT 19
- 🐛 [Bug]: Module doesnt contain package HOT 3
- 🤗 [Question]: Why Mustache was excluded from the extended benchmark HOT 3
- fiber/django template uses its own non-standard `{{embed}}` tag.
- 🤗 [Question]: Is it possible to use django/v3 on gofiber/v3.0.0-beta? HOT 9
- 🐛 [Bug]: request fail before the template is available HOT 5
- 🐛 [Bug]: Jet, inside {{ embed() }} no error propagation HOT 2
- 🤗 [Question]: HOT 4
- 🐛 [Bug]: Template layout always show last loaded page. HOT 5
- 🐛 [Bug]: Mustache template engine; partials don't work HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from template.