Skip to content

Commit

Permalink
docs: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Jul 28, 2023
1 parent 6aaa9b1 commit 878bcca
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 18 additions & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package main

import (
"embed"
"encoding/json"
"log"
"net/http"

ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)

//go:embed i18n/localizeJSON/*
var fs embed.FS

func main() {
// new gin engine
gin.SetMode(gin.ReleaseMode)
router := gin.New()

// apply i18n middleware
router.Use(ginI18n.Localize())
router.Use(ginI18n.Localize(ginI18n.WithBundle(&ginI18n.BundleCfg{
DefaultLanguage: language.English,
FormatBundleFile: "json",
AcceptLanguage: []language.Tag{language.English, language.German, language.Chinese},
RootPath: "./i18n/localizeJSON/",
UnmarshalFunc: json.Unmarshal,
// After commenting this line, use defaultLoader
// it will be loaded from the file
Loader: &ginI18n.EmbedLoader{
FS: fs,
},
})))

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

0 comments on commit 878bcca

Please sign in to comment.