Giter VIP home page Giter VIP logo

go-i18n-1's Introduction

Introduction

  • GoLang international library, easy to use, welcome to share and pull request
  • Fast、Easy to Use

Use

package main

import (
	"fmt"
	"github.com/smithyj/go-i18n/i18n"
	"os"
)

func main()  {
	// Load language config
	path, _ := os.Getwd()
	filename := "/locale/zh.yaml"
	zh, _ := i18n.LoadFileParseLanguage(fmt.Sprintf("%s%s",path, filename))
	filename = "/locale/en.yaml"
	en, _ := i18n.LoadFileParseLanguage(fmt.Sprintf("%s%s",path, filename))

	// Inject to bundle
	// First param is default language option
	// Second param is need translate language map list
	bundle := i18n.NewI18N("zh", map[string]i18n.Lang{
		"zh": zh,
		"en": en,
	})

	// Initialize translate program
	// First param is bundle
	// Second param is visitor lang, maybe source Accept-Language or custom language
	// accord with golang language package standard
	locale, _ := i18n.NewLocale(bundle, "fr")


	message := locale.Translate("hello world")
	fmt.Println(message)

	message = locale.WithTranslateDefault("hello world23", "默认值")
	fmt.Println(message)

	message = locale.WithTranslateParams("hello world", map[string]interface{}{
		"haha": "yangyj",
	})
	fmt.Println(message)

	message = locale.WithTranslateParamsDefault("hello world22223", map[string]interface{}{
		"haha": "yangyj",
	}, "默认值${haha}")
	fmt.Println(message)
}
你好,世界,变量替换:${haha}
默认值
你好,世界,变量替换:yangyj
默认值yangyj

Contcat

go-i18n-1's People

Contributors

smithyj avatar

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.