GithubHelp home page GithubHelp logo

luwake / gin-i18n Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fishjar/gin-i18n

0.0 0.0 0.0 14 KB

Simple gin i18n middleware (简单的 gin 多语言支持中间件)

License: MIT License

Go 100.00%

gin-i18n's Introduction

Simple gin i18n middleware

English | 简体中文

I found that other people's gin i18n middleware too complicated, so I wrote a simple one.

Example

// example/main.go
package main

import (
	"log"
	"net/http"
	"path"

	ginI18n "github.com/fishjar/gin-i18n"
	"github.com/gin-gonic/gin"
)

func main() {
	const defaultLang = "zh-CN"                     // default language
	const supportLang = "zh-CN,en-US"               // list of supported languages ​​(must include default language)
	var filePath = path.Join("example", "localize") // multilingual file directory

	// initialize settings
	ginI18n.LocalizerInit(defaultLang, supportLang, filePath)

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

	// apply i18n middleware
	router.Use(ginI18n.GinLocalizer())

	router.GET("/", func(c *gin.Context) {
		localizer := c.MustGet("Localizer").(*ginI18n.UserLocalize)
		c.String(http.StatusOK, localizer.GetMsg("welcome"))
	})

	router.GET("/:name", func(c *gin.Context) {
		localizer := c.MustGet("Localizer").(*ginI18n.UserLocalize)
		c.String(http.StatusOK, localizer.GetMsg("hello_world", c.Param("name")))
	})

	if err := router.Run(":8080"); err != nil {
		log.Fatal(err)
	}
}
# example/localize/zh-CN.yml
welcome: 欢迎!
hello_world: 你好 %s!
# example/localize/en-US.yml
welcome: welcome!
hello_world: hello %s!
# review
go run example/main.go

curl http://127.0.0.1:8080/ -H 'accept-language: zh-CN'             # 欢迎!
curl http://127.0.0.1:8080/ -H 'accept-language: en-US'             # welcome!

curl http://127.0.0.1:8080/ -H 'accept-language: zh-CN,en-US;q=0.9' # 欢迎!
curl http://127.0.0.1:8080/ -H 'accept-language: zh-CN;q=0.9,en-US' # welcome!

curl http://127.0.0.1:8080/ -H 'accept-language: zh'                # 欢迎!
curl http://127.0.0.1:8080/ -H 'accept-language: en'                # welcome!

curl http://127.0.0.1:8080/gabe -H 'accept-language: zh-CN'         # 你好 gabe!
curl http://127.0.0.1:8080/gabe -H 'accept-language: en-US'         # hello gabe!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.