Open ReneWerner87 opened 3 months ago
@coderabbitai please fill in the description of the proposal and add several examples of use with the new concept
[!TIP] For best results, initiate chat on the files or code changes.
@ReneWerner87
Currently, the i18n functionality is implemented as middleware, where the i18n bundle and localizer map are initialized for each request. This approach can lead to inconsistencies and errors, especially when the i18n state changes between requests.
To improve the robustness and performance of the i18n functionality, we propose refactoring it from middleware to a global container. This global container will be initialized once before the routes are set up and will manage the i18n bundle and localizer map. The global container will receive the supported languages and other configuration settings as input during initialization.
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/fiberi18n"
"golang.org/x/text/language"
)
func main() {
// Initialize global i18n container
i18nConfig := &fiberi18n.Config{
RootPath: "./locales",
DefaultLanguage: language.English,
AcceptLanguages: []language.Tag{language.English, language.Chinese},
FormatBundleFile: "json",
UnmarshalFunc: json.Unmarshal,
}
fiberi18n.Initialize(i18nConfig)
// Create a new Fiber app
app := fiber.New()
// Set up routes
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(fiberi18n.MustLocalize(c, "welcome"))
})
app.Get("/:name", func(c *fiber.Ctx) error {
return c.SendString(fiberi18n.MustLocalize(c, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": c.Params("name"),
},
}))
})
// Start the server
app.Listen(":3000")
}
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/fiberi18n"
"golang.org/x/text/language"
)
func main() {
// Initialize global i18n container
i18nConfig := &fiberi18n.Config{
RootPath: "./locales",
DefaultLanguage: language.English,
AcceptLanguages: []language.Tag{language.English, language.Chinese},
FormatBundleFile: "json",
UnmarshalFunc: json.Unmarshal,
}
fiberi18n.Initialize(i18nConfig)
// Create a new Fiber app
app := fiber.New()
// Set up routes
app.Get("/", func(c *fiber.Ctx) error {
message, err := fiberi18n.Localize(c, "welcome")
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.SendString(message)
})
app.Get("/:name", func(c *fiber.Ctx) error {
message, err := fiberi18n.Localize(c, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": c.Params("name"),
},
})
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.SendString(message)
})
// Start the server
app.Listen(":3000")
}
These examples demonstrate how to initialize the global i18n container and use it to access localized messages in route handlers. This approach ensures that the i18n configuration and state are managed globally, improving consistency and performance.
Feature Description
The i18n functionality is currently not perfect and prone to errors because the stats can change in between
The recommendation is to initialize a global container before the routes this container receives the supported languages as input and is initially initialized
TODO: provide example and more info
Additional Context (optional)
No response
Code Snippet (optional)
Checklist: