Git Product home page Git Product logo

go-i18n's Introduction

go-i18n Build Status Report card Sourcegraph

go-i18n is a Go package and a command that helps you translate Go programs into multiple languages.

Versions

  • v1 is available at 1.x.x tags.
  • v2 is available at 2.x.x tags.

This README always documents the latest version (i.e. v2).

Package i18n GoDoc

The i18n package provides support for looking up messages according to a set of locale preferences.

import "github.com/nicksnyder/go-i18n/v2/i18n"

Create a Bundle to use for the lifetime of your application.

bundle := i18n.NewBundle(language.English)

Create a Localizer to use for a set of language preferences.

func(w http.ResponseWriter, r *http.Request) {
    lang := r.FormValue("lang")
    accept := r.Header.Get("Accept-Language")
    localizer := i18n.NewLocalizer(bundle, lang, accept)
}

Use the Localizer to lookup messages.

localizer.MustLocalize(&i18n.LocalizeConfig{
    DefaultMessage: &i18n.Message{
        ID: "PersonCats",
        One: "{{.Name}} has {{.Count}} cat.",
        Other: "{{.Name}} has {{.Count}} cats.",
    },
    TemplateData: map[string]string{
        "Name": "Nick",
        "Count": 2,
    },
    PluralCount: 2,
}) // Nick has 2 cats.

It requires Go 1.9 or newer.

Command goi18n GoDoc

The goi18n command manages message files used by the i18n package.

go get -u github.com/nicksnyder/go-i18n/v2/goi18n
goi18n -help

Use goi18n extract to create a message file that contains the messages defined in your Go source files.

# en.toml
[PersonCats]
description = "The number of cats a person has"
one = "{{.Name}} has {{.Count}} cat."
other = "{{.Name}} has {{.Count}} cats."

Use goi18n merge to create message files for translation.

# translate.es.toml
[PersonCats]
description = "The number of cats a person has"
hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
one = "{{.Name}} has {{.Count}} cat."
other = "{{.Name}} has {{.Count}} cats."

Use goi18n merge to merge translated message files with your existing message files.

# active.es.toml
[PersonCats]
description = "The number of cats a person has"
hash = "sha1-f937a0e05e19bfe6cd70937c980eaf1f9832f091"
one = "{{.Name}} tiene {{.Count}} gato."
other = "{{.Name}} tiene {{.Count}} gatos."

Load the active messages into your bundle.

bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.MustLoadMessageFile("active.es.toml")

For more information and examples:

License

go-i18n is available under the MIT license. See the LICENSE file for more info.

go-i18n's People

Contributors

nicksnyder avatar parkr avatar n10v avatar mash avatar dorajistyle avatar udokmeci avatar rtfb avatar mh-cbon avatar soktherat avatar jcajka avatar wichert avatar seklfreak avatar rodcorsi avatar ultimaweapon avatar ottob avatar zippoxer avatar jondal avatar sogko avatar emosbaugh avatar dennisfaust avatar dvrkps avatar bep avatar

Watchers

James Cloos avatar Siddhesh Shankar Raut 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.