Giter Club home page Giter Club logo

i's Introduction

i

i (pronounced as in bit), a minimalistic yet flexible internationalization package.

Version 0.1.1.

Basic Usage

// i18n/ru.json
{
	"ru": {
		"default": {
			"Hello.": "Привет."
		}
	}
}
package main

import (
	"fmt"
	"github.com/ainar-g/i"
)

func main() {
	i.LoadJSON("i18n/ru.json") // Your file with translations.
	i.SetLocale("ru")
	fmt.Println(i.T("Hello."))
	// Output: "Привет."
}

Advanced Usage

Scopes and locales

Locales are your program's languages and dialects. You can use any format to name your locales, so a Russian locale may be coded as "ru_RU", "Russian", or just "ru". The latter is recommended.

Scopes are a way to resolve translation conflicts. For example, you have a program that has users and groups, and both of them have names, and you want to translate it into Russian. But in Russian the word "name" is translated to "имя" for the animate objects (i.e. people) and "название" for the inanimate objects (i.e. groups). This is where you use a scope:

i.T("Name", "user", "ru")
// Output: Имя
i.T("Name", "group", "ru")
// Output: Название

Translators

Having one scope and one locale for the whole app may be OK for the smaller programs, but with the bigger systems you might get into trouble. That's where the custom translators come into play. You can create as many of them as you want, provide them with the source to get the translations from and the storage to keep them, set the locale and the scope, and you are ready to go.

// Open your file...
file, e := os.Open("i18n/ru.json")
if e != nil {
	panic(e)
}
defer file.Close()
// create the source...
source := i.NewJSONSource(file)

// and a storage. We'll take a default one here.
storage := i.NewDefaultStorage()

// Use them to create the custom translator. Let's call him mr.
mr := i.NewTranslator("default", "ru", source, storage)

// Load the translations from source.
if e := mr.Load(); e != nil {
	panic(e)
}

// Use your custom translator as you would do with the package.
fmt.Println(mr.T("I PITY THE FOOL WHO DOESN'T MAKE USE OF I18N!"))
// Output: МНЕ ЖАЛЬ ТОГО ДУРАКА, ЧТО НЕ ИСПОЛЬЗУЕТ ИНТЕРНАЦИОНАЛИЗАЦИЮ!

Storages

Storage is where your translations are stored. i provides the DefaultStorage type, which is suitable for the most i18n needs, but if you want something more, you can create your own source, be it a map of maps or a key-value in-memory storage à la Redis.

Sources

Source is where the package (and the translators) get their translations from. i provides the default JSON and XML sources. You can make sources of your own, be it a database, a CSV file, and what-not.

GoDoc

See more package documentation at GoDoc.org.

License

Released under the MIT License.

i's People

Contributors

ainar-g avatar

Watchers

 avatar  avatar  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.