i18n

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

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

Go to latest
Published: Oct 24, 2017 License: MIT Imports: 8 Imported by: 2

README

i18n

JSON based transations written in Golang.

Installation


go get github.com/AnUnnamedProject/i18n

Benchmark


Tested on i7-5820k@4.2GHz (6 core / 12 threads)

BenchmarkPrint-12     	30000000	        54.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkPrintf-12    	10000000	       229 ns/op	      24 B/op	       2 allocs/op

Usage


Create your translation file as a JSON object and put in your project folder (i.e. i18n).

{
	"Hello world": "Hello world"
}

the scan the directory for json translation files and prepare the internal translation map:

i18n.Load("i18n")

If you need to check for missing languages or translations, enable the Debug

i18n.Debug(true)

Warnings are printed directly to stdout via log package.

To instantiate and use the library in your code:

// Get a new pointer
tr := i18n.New("es")

// Change language
tr.SetLang("it")

// Get the current language
tr.GetLang()

// Print a translation
str1 := tr.Print("Hello world")

// Printf a translation
str2 := tr.Print("Hello %s", "name")

// Plural
str3 := tr.Plural(10, "No records.", "One Record", "%d Records.")

TODO


  • App to extract strings from go and html source files.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(value bool)

Debug print missing translation strings when enabled.

func Load

func Load(pathDir string) error

Load scans for JSON files inside the provided pathDir and initialize the language map.

Types

type I18N

type I18N struct {
	// contains filtered or unexported fields
}

I18N provides methods to set and get the language depending on context.

func New

func New(language string) *I18N

New return a new I18N structure.

func (*I18N) GetLang

func (i *I18N) GetLang() string

GetLang get the current language.

func (*I18N) Plural

func (i *I18N) Plural(value int, zero string, one string, many string, values ...interface{}) string

Plural return

func (*I18N) Print

func (i *I18N) Print(str string, args ...interface{}) string

Print translate a string, if args are passed they are parsed using Sprintf

func (*I18N) SetLang

func (i *I18N) SetLang(language string)

SetLang set the language.

Jump to

Keyboard shortcuts

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