Git Product home page Git Product logo

Comments (4)

ly020044 avatar ly020044 commented on September 3, 2024
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.

ReneWerner87 avatar ReneWerner87 commented on September 3, 2024

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.

ly020044 avatar ly020044 commented on September 3, 2024

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.

ReneWerner87 avatar ReneWerner87 commented on September 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.