Skip to content

Commit

Permalink
Update: README
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie committed Feb 26, 2022
1 parent 7a3efe3 commit 557dea9
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,51 @@ go get github.com/Charliego93/go-i18n
package main

import (
"embed"
"fmt"
"github.com/Charliego93/go-i18n"
"github.com/gin-gonic/gin"
"net/http"
"embed"
"fmt"
"github.com/Charliego93/i18n"
"github.com/gin-gonic/gin"
"net/http"
)

//go:embed examples/lan2/*
var langFS embed.FS

func main() {
engine := gin.New()
// returns the default language if the header and language key are not specified or if the language does not exist
engine.Use(gin.WrapH(i18n.Localize(language.Chinese,
i18n.WithLoader(i18n.NewLoaderWithPath("./examples/simple")))))

// Use multi loader provider
// Built-in load from file and load from fs.FS
// engine.Use(gin.WrapH(i18n.Localize(language.Chinese,
// i18n.WithLoader(i18n.NewLoaderWithFS(langFS),
// i18n.NewLoaderWithPath("./examples/lan1")))))

// curl -H "Accept-Language: en" 'http://127.0.0.1:9090/Hello' returns "hello"
// curl -H "Accept-Language: uk" 'http://127.0.0.1:9090/Hello' returns "Бонгу"
// curl 'http://127.0.0.1:9090/Hello?lang=en' returns "hello"
// curl 'http://127.0.0.1:9090/Hello?lang=uk' returns "Бонгу"
engine.GET("/:messageId", func(ctx *gin.Context) {
ctx.String(http.StatusOK, i18n.MustGetMessage(ctx.Param("messageId")))
})

// curl -H "Accept-Language: en" 'http://127.0.0.1:9090/HelloName/I18n' returns "hello I18n"
// curl -H "Accept-Language: uk" 'http://127.0.0.1:9090/HelloName/I18n' returns "Бонгу I18n"
// curl 'http://127.0.0.1:9090/HelloName/I18n?lang=en' returns "hello I18n"
// curl 'http://127.0.0.1:9090/HelloName/I18n?lang=uk' returns "Бонгу I18n"
engine.GET("/:messageId/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, i18n.MustGetMessage(&i18n.LocalizeConfig{
MessageID: ctx.Param("messageId"),
TemplateData: map[string]string{
"Name": ctx.Param("name"),
},
}))
})
fmt.Println(engine.Run())
engine := gin.New()

// returns the default language if the header and language key are not specified or if the language does not exist
engine.Use(gin.WrapH(i18n.Localize(language.Chinese,
i18n.WithLoader(i18n.NewLoaderWithPath("./examples/simple")))))

// Use multi loader provider
// Built-in load from file and load from fs.FS
// engine.Use(gin.WrapH(i18n.Localize(language.Chinese,
// i18n.WithLoader(i18n.NewLoaderWithFS(langFS),
// i18n.NewLoaderWithPath("./examples/lan1")))))

// curl -H "Accept-Language: en" 'http://127.0.0.1:9090/Hello' returns "hello"
// curl -H "Accept-Language: uk" 'http://127.0.0.1:9090/Hello' returns "Бонгу"
// curl 'http://127.0.0.1:9090/Hello?lang=en' returns "hello"
// curl 'http://127.0.0.1:9090/Hello?lang=uk' returns "Бонгу"
engine.GET("/:messageId", func(ctx *gin.Context) {
ctx.String(http.StatusOK, i18n.MustTr(ctx.Param("messageId")))
})

// curl -H "Accept-Language: en" 'http://127.0.0.1:9090/HelloName/I18n' returns "hello I18n"
// curl -H "Accept-Language: uk" 'http://127.0.0.1:9090/HelloName/I18n' returns "Бонгу I18n"
// curl 'http://127.0.0.1:9090/HelloName/I18n?lang=en' returns "hello I18n"
// curl 'http://127.0.0.1:9090/HelloName/I18n?lang=uk' returns "Бонгу I18n"
engine.GET("/:messageId/:name", func(ctx *gin.Context) {
ctx.String(http.StatusOK, i18n.MustTr(&i18n.LocalizeConfig{
MessageID: ctx.Param("messageId"),
TemplateData: map[string]string{
"Name": ctx.Param("name"),
},
}))
})

fmt.Println(engine.Run())
}
```

Expand Down

0 comments on commit 557dea9

Please sign in to comment.