Giter VIP home page Giter VIP logo

go-i18n's Introduction

Usage

Loading dictionaries from files

	err = i18n.InitFromDir(`en`, `/usr/lib/app/translations`)
	if err != nil {
		log.Println(`Dictionary loading error`, err)
	}

JSON dictionary template

{
  "errors": {
    "internal_error": "Server error, please try again later"
  },
  "errors.user.signup": {
    "username_to_short": "Username to short",
    "form_min_age": "Minimum age is {min}",
    "form_max_age": "Maximum age is {max}",
    "form_min_length": "{field} minimum length is {min}",
    "form_max_length": "{field} maximum length is {max}"
  }
}

Using dictionary

package main

import (
	"github.com/censync/go-i18n"
	"log"
	"os"
)

const (
	defaultLanguage = "en_US"

	// Json dictionaries location
	translationsPath = "/usr/lib/my_app/translations"
)

func main() {
	err := i18n.InitFromDir(defaultLanguage, translationsPath, `en_US`, `cs_CZ`)
	if err != nil {
		log.Println(`Loading dictionaries error`, err)
		os.Exit(1)
	}
	userLang, _ := os.LookupEnv("LANG")

	// Get translator for locale
	tr := i18n.New(userLang)

	// Creating simple string
	//
	//	Sample dict data:
	//	{
	//		"form.signup" : {
	//			"welcome" : "Welcome to registration"
	//		}
	//	}
	strSimple := tr.T("form.signup", "welcome")

	// Creating formatted string
	//
	//	Sample dict data with arguments:
	//	{
	//		"form.login" : {
	//			"connections_limit" : "Hello, {name}"
	//		}
	//	}
	strFormatted := tr.Tf("form.login", "connection_lost", i18n.M{"{name}": "John"})

	// Creating simple error
	errSimple := tr.ErrT("errors.connections", "connection_lost")

	// Creating formatted error
	//
	//
	//	Sample dict data:
	//	{
	//		"errors.connections" : {
	//			"connections_limit" : "Connections limit is {count}"
	//		}
	//	}
	errFormatted := tr.ErrTf("errors.connections", "connections_limit", i18n.M{"{count}": 50})

	log.Println(strSimple, strFormatted, errSimple, errFormatted)
}

Using advanced errors with translator

package main

import (
	"github.com/censync/go-i18n"
	"log"
	"net/http"
	"os"
)

const (
	defaultLanguage = "en_US"
)

func main() {
	collection := i18n.DictionaryCollection{
		"en_US": {
			"errors": {
				"unknown": "Unknown error",
			},
			"errors.connections": {
				"connections_limit": "Connections limit is {count}",
			},
			"form.signup": {
				"welcome": "Welcome to registration",
				"disabled": "Registration is temporarily unavailable",
			},
			"form.login": {
				"title": "Hello, {name}",
			},
		},
		"cs_CZ": {
			"errors": {
				"unknown": "Neznámá chyba",
			},
			"errors.connections": {
				"connections_limit": "Limit připojení je {count}",
			},
			"form.signup": {
				"welcome":  "Vítejte v registraci",
				"disabled": "Registrace je dočasně nedostupná",
			},
			"form.login": {
				"title": "Ahoj, {name}",
			},
		},
	}

	err := i18n.Init(defaultLanguage, collection)
	if err != nil {
		log.Println(`Loading dictionaries error`, err)
		os.Exit(1)
	}
	userLang, _ := os.LookupEnv("LANG")

	// Get translator for locale
	tr := i18n.New(userLang)

	errAdvanced := i18n.NewErr("form.signup", "disabled")

	log.Println(errAdvanced.T(tr)) // Returns string "Registration is disabled"

	log.Println(errAdvanced.ErrT(tr)) // Returns error "Registration is disabled"

	errAdvanced = i18n.NewErrWithCode(
		http.StatusTooManyRequests,
		"errors.connections",
		"connections_limit",
		i18n.M{"{count}": 50},
	)

	log.Println(errAdvanced.Tf(tr)) // Returns string "Connections limit is 50"

	log.Println(errAdvanced.ErrTf(tr)) // Returns error "Connections limit is 50"
}

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.