From 878bcca7edb93cdeddcc2921448269c2b5231305 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 28 Jul 2023 08:52:31 +0800 Subject: [PATCH] docs: update example --- _example/{testdata => i18n}/localize/de.yaml | 0 _example/{testdata => i18n}/localize/en.yaml | 0 _example/{testdata => i18n}/localize/fr.yaml | 0 .../{testdata => i18n}/localizeJSON/de.json | 0 .../{testdata => i18n}/localizeJSON/en.json | 0 .../{testdata => i18n}/localizeJSON/zh.json | 0 _example/main.go | 19 ++++++++++++++++++- 7 files changed, 18 insertions(+), 1 deletion(-) rename _example/{testdata => i18n}/localize/de.yaml (100%) rename _example/{testdata => i18n}/localize/en.yaml (100%) rename _example/{testdata => i18n}/localize/fr.yaml (100%) rename _example/{testdata => i18n}/localizeJSON/de.json (100%) rename _example/{testdata => i18n}/localizeJSON/en.json (100%) rename _example/{testdata => i18n}/localizeJSON/zh.json (100%) diff --git a/_example/testdata/localize/de.yaml b/_example/i18n/localize/de.yaml similarity index 100% rename from _example/testdata/localize/de.yaml rename to _example/i18n/localize/de.yaml diff --git a/_example/testdata/localize/en.yaml b/_example/i18n/localize/en.yaml similarity index 100% rename from _example/testdata/localize/en.yaml rename to _example/i18n/localize/en.yaml diff --git a/_example/testdata/localize/fr.yaml b/_example/i18n/localize/fr.yaml similarity index 100% rename from _example/testdata/localize/fr.yaml rename to _example/i18n/localize/fr.yaml diff --git a/_example/testdata/localizeJSON/de.json b/_example/i18n/localizeJSON/de.json similarity index 100% rename from _example/testdata/localizeJSON/de.json rename to _example/i18n/localizeJSON/de.json diff --git a/_example/testdata/localizeJSON/en.json b/_example/i18n/localizeJSON/en.json similarity index 100% rename from _example/testdata/localizeJSON/en.json rename to _example/i18n/localizeJSON/en.json diff --git a/_example/testdata/localizeJSON/zh.json b/_example/i18n/localizeJSON/zh.json similarity index 100% rename from _example/testdata/localizeJSON/zh.json rename to _example/i18n/localizeJSON/zh.json diff --git a/_example/main.go b/_example/main.go index b318054..f1da314 100644 --- a/_example/main.go +++ b/_example/main.go @@ -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"))