Skip to content

Commit

Permalink
refactor: refactor code for context variable and i18n function usage
Browse files Browse the repository at this point in the history
- Refactor the code to replace `context` with `ctx` in the `router.GET` functions
- Update the `ginI18n.MustGetMessage` function calls to include `ctx` as a parameter
- Modify the `i18n.LocalizeConfig` object initialization to use `ctx.Param` instead of `context.Param`

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jul 27, 2023
1 parent b234b91 commit 8662692
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ func main() {
// apply i18n middleware
router.Use(ginI18n.Localize())

router.GET("/", func(context *gin.Context) {
context.String(http.StatusOK, ginI18n.MustGetMessage("welcome"))
router.GET("/", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(ctx, "welcome"))
})

router.GET("/:name", func(context *gin.Context) {
context.String(http.StatusOK, ginI18n.MustGetMessage(&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
router.GET("/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(
ctx,
&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Param("name"),
},
}))
})

if err := router.Run(":8080"); err != nil {
Expand Down Expand Up @@ -91,17 +93,19 @@ func main() {
FormatBundleFile: "json",
})))

router.GET("/", func(context *gin.Context) {
context.String(http.StatusOK, ginI18n.MustGetMessage("welcome"))
router.GET("/", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(ctx, "welcome"))
})

router.GET("/:name", func(context *gin.Context) {
context.String(http.StatusOK, ginI18n.MustGetMessage(&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
router.GET("/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(
ctx,
&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Param("name"),
},
}))
})

if err := router.Run(":8080"); err != nil {
Expand Down Expand Up @@ -142,17 +146,19 @@ func main() {
),
))

router.GET("/", func(context *gin.Context) {
context.String(http.StatusOK, ginI18n.MustGetMessage("welcome"))
router.GET("/", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(ctx, "welcome"))
})

router.GET("/:name", func(context *gin.Context) {
context.String(http.StatusOK, ginI18n.MustGetMessage(&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
}))
router.GET("/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, ginI18n.MustGetMessage(
ctx,
&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": ctx.Param("name"),
},
}))
})

if err := router.Run(":8080"); err != nil {
Expand Down

0 comments on commit 8662692

Please sign in to comment.