i18n

package module
v0.0.0-...-d9bab43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2022 License: MIT Imports: 8 Imported by: 0

README

i18n

Run Tests CodeQL codecov GoDoc Go Report Card

Usage

Download and install it:

go get github.com/gin-contrib/i18n

Import it in your code:

import ginI18n "github.com/gin-contrib/i18n"

Canonical example:

package main

import (
  "log"
  "net/http"

  ginI18n "github.com/gin-contrib/i18n"
  "github.com/gin-gonic/gin"
  "github.com/nicksnyder/go-i18n/v2/i18n"
)

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

  ginI18n.Localize()

  router.GET("/", func(context *gin.Context) {
    context.String(http.StatusOK, ginI18n.MustGetMessage("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"),
      },
    }))
  })

  if err := router.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

Customized Bundle

package main

import (
  "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"
)

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

  // init
  ginI18n.Localize(ginI18n.WithBundle(&ginI18n.BundleCfg{
    RootPath:         "./_example/localizeJSON",
    AcceptLanguage:   []language.Tag{language.German, language.English},
    DefaultLanguage:  language.English,
    UnmarshalFunc:    json.Unmarshal,
    FormatBundleFile: "json",
  }))

  router.GET("/", func(context *gin.Context) {
    context.String(http.StatusOK, ginI18n.MustGetMessage("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"),
      },
    }))
  })

  if err := router.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

Customized Get Language Handler

package main

import (
  "log"
  "net/http"

  ginI18n "github.com/gin-contrib/i18n"
  "github.com/gin-gonic/gin"
  "github.com/nicksnyder/go-i18n/v2/i18n"
)

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

  // init
  ginI18n.Localize(
    ginI18n.WithGetLngHandle(
      func(context *gin.Context, defaultLng string) string {
        lng := context.Query("lng")
        if lng == "" {
          return defaultLng
        }
        return lng
      },
    ),
  )

  router.GET("/", func(context *gin.Context) {
    context.String(http.StatusOK, ginI18n.MustGetMessage("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"),
      },
    }))
  })

  if err := router.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BundleCfg

type BundleCfg struct {
	DefaultLanguage  language.Tag
	FormatBundleFile string
	AcceptLanguage   []language.Tag
	RootPath         string
	UnmarshalFunc    i18n.UnmarshalFunc
	Loader           Loader
}

BundleCfg ...

type EmbedLoader

type EmbedLoader struct {
	FS embed.FS
}

func (*EmbedLoader) LoadMessage

func (c *EmbedLoader) LoadMessage(path string) ([]byte, error)

type GinI18n

type GinI18n interface {
	GetMessage(param interface{}, lng string) (string, error)
	MustGetMessage(param interface{}, lng string) string
	// contains filtered or unexported methods
}

GinI18n ...

func Localize

func Localize(opts ...Option) GinI18n

type Loader

type Loader interface {
	LoadMessage(path string) ([]byte, error)
}

type LoaderFunc

type LoaderFunc func(path string) ([]byte, error)

func (LoaderFunc) LoadMessage

func (f LoaderFunc) LoadMessage(path string) ([]byte, error)

type Option

type Option func(GinI18n)

Option ...

func WithBundle

func WithBundle(config *BundleCfg) Option

WithBundle ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL