-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🚀 [Feature]: i18n refactoring from middleware to global container #1142
Comments
@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. Proposal DescriptionCurrently, 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. Key Changes:
Examples of UseExample 1: Initializing the Global i18n Containerpackage 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")
} Example 2: Accessing Localized Messages in Handlerspackage 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. Analysis chainScripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Locate the i18n middleware implementation in the codebase.
fd -e go -e md -e txt i18n
Length of output: 69 Script: #!/bin/bash
# Display the contents of the i18n middleware implementation and test files.
cat fiberi18n/i18n.go
cat fiberi18n/i18n_test.go
Length of output: 8979 Learnings added
|
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:
The text was updated successfully, but these errors were encountered: