i18n

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

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

Go to latest
Published: Apr 14, 2016 License: MIT Imports: 16 Imported by: 0

README

go-i18n - Internationalization for Go

GoDoc

Getting started

If you want to use go-i18n in your project, you first have to make one or more translation maps. There are several formats supported.

JSON

All .json and .js files loaded with the NewTemplateFile loader are interpreted as a flat list of key-value pairs. Nested items are allowed and will be concatenated with a dot.

For example:

{
  "foo": {
    "bar": {
      "testing"
    }
  }
}

Here, foo → bar is accessible as foo.bar translation key.

YAML

All .yml and .yaml files loaded with the NewTemplateFile loader are interpreted as Rails Application compatible language files, see the Rails Internationalization page for more details.

Basic project structure

A basic project structure may look like follows:

package app

import (
    "git.maze.io/maze/go-i18n"

    "golang.org/net/http"
    "golang.org/x/text/language"
)

func main() {
    var lang = i18n.New(language.English)

    lang.Add(i18n.NewMap(language.English, map[string]string{
        "hello %s": "Hello %s!",
    }))

    lang.Add(i18n.NewMap(language.Dutch, map[string]string{
        "hello %s": "Hallo %s!",
    }))

    lang.Add(i18n.NewMap(language.SimplifiedChinese, map[string]string{
        "hello %s": "%s 你好!",
    }))
        
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        t := lang.Accept(r.Header().Get("Accept-Language"))
        t.Fprintf(w, "hello %s", r.RemoteAddr)
    })
    http.ListenAndServe(":8000", nil)
}

Integration

You can use i18n with many web frameworks, some of which are documented below.

Beego integration

Add these three lines before "beego.Run()" in your main() function.

for k, fn := range lang.TemplateFuncs() {
    beego.AddFuncMap(k, fn)
}
Revel integration
package app

import (
    "git.maze.io/maze/go-i18n"
    "github.com/revel/revel"
    "golang.org/x/text/language"
)

var lang *i18n.I18N

func init() {
    var err error
    if lang, err = i18n.New(language.English); err != nil {
        panic(err)
    }

    lang.TemplateInject(revel.TemplateFuncs)
}
Pongo2 integration
package app

import (
    "git.maze.io/maze/go-i18n"
    "github.com/flosch/pongo2"
    "golang.org/x/text/language"
)

var lang *i18n.I18N

func init() {
    var err error
    if lang, err = i18n.New(language.English); err != nil {
        panic(err)
    }

    pongo2.RegisterFilter("translate",
        func(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
            if param != nil {
                return pongo2.AsValue(lang.Translate(param.String(), in.String())), nil
            }
            return pongo2.AsValue(lang.Translate(lang.Default().String(), in.String())), nil
        },
    )
}

Documentation

Overview

Package i18n provides internationalization (i18n) for Go projects and various template engines.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownFormat = errors.New("i18n: unknown file format")
)

Functions

func IsNullTranslation

func IsNullTranslation(t Translation) bool

IsNullTranslation checks if the translation is a NullTranslation or NullTranslation pointer

Types

type Formatter

type Formatter interface {
	Fprint(io.Writer, ...interface{}) (int, error)
	Fprintf(io.Writer, string, ...interface{}) (int, error)
	Fprintln(io.Writer, ...interface{}) (int, error)
	Sprint(...interface{}) string
	Sprintf(string, ...interface{}) string
	Sprintln(...interface{}) string
}

Formatter partially implements the fmt package.

type Internationalization

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

func New

func New(fallback language.Tag) (*Internationalization, error)

New starts a new internationalization instance.

func (*Internationalization) Accept

func (i *Internationalization) Accept(accept string) Translation

Accept parses a HTTP Accept-Language header and returns a matching translation. May return a NullTranslation if there is no matching language and the fallback language is also not available.

func (*Internationalization) Add

Add a translation

func (*Internationalization) Default

func (i *Internationalization) Default() language.Tag

Default returns the default (fallback) tag.

func (*Internationalization) Languages

func (i *Internationalization) Languages() []string

Languages returns a slice of supported languages, in their native translation.

func (*Internationalization) Supported

func (i *Internationalization) Supported() []language.Tag

Supported returns a slice of supported language tags.

func (*Internationalization) TemplateFuncs

func (i *Internationalization) TemplateFuncs() template.FuncMap

TemplateFuncs generates a new template function map.

func (*Internationalization) TemplateInject

func (i *Internationalization) TemplateInject(f map[string]interface{})

TemplateInject injects Internationalization template functions into the passed template function map.

func (*Internationalization) TemplateNew

func (i *Internationalization) TemplateNew(name string) *template.Template

TemplateNew is a wrapper for template.New that injects the Internationalization template functions.

func (*Internationalization) Translate

func (i *Internationalization) Translate(lang, key string) string

Translate a phrase based on the selected language.

type NullTranslation

type NullTranslation struct {
	TranslationMap
}

func (NullTranslation) Tag

func (t NullTranslation) Tag() language.Tag

type Translater

type Translater interface {
	Get(string) string
	Has(string) bool
	Len() int
	Tag() language.Tag
}

type Translation

type Translation interface {
	Translater
	Formatter
}

type TranslationMap

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

func NewMap

func NewMap(lang language.Tag, keys map[string]string) TranslationMap

func NewTranslationFile

func NewTranslationFile(lang language.Tag, name string) (t TranslationMap, err error)

func ParseJSON

func ParseJSON(lang language.Tag, b []byte) (t TranslationMap, err error)

ParseJSON takes a JSON blob and tries to decode it as a TranslationMap.

func ParseTOML

func ParseTOML(lang language.Tag, b []byte) (t TranslationMap, err error)

ParseTOML takes a TOML blob and tries to decode it as a TranslationMap.

func ParseYAML

func ParseYAML(lang language.Tag, b []byte) (t TranslationMap, err error)

Parse Ruby compatible i18n translations file, which is a (tree based) key-value structure with the root keys as the language, see http://guides.rubyonrails.org/i18n.html

func (TranslationMap) Fprint

func (t TranslationMap) Fprint(w io.Writer, a ...interface{}) (int, error)

func (TranslationMap) Fprintf

func (t TranslationMap) Fprintf(w io.Writer, key string, a ...interface{}) (int, error)

func (TranslationMap) Fprintln

func (t TranslationMap) Fprintln(w io.Writer, a ...interface{}) (int, error)

func (TranslationMap) Get

func (t TranslationMap) Get(key string) string

func (TranslationMap) Has

func (t TranslationMap) Has(key string) bool

func (TranslationMap) Len

func (t TranslationMap) Len() int

func (TranslationMap) Sprint

func (t TranslationMap) Sprint(a ...interface{}) string

func (TranslationMap) Sprintf

func (t TranslationMap) Sprintf(key string, a ...interface{}) string

func (TranslationMap) Sprintln

func (t TranslationMap) Sprintln(a ...interface{}) string

func (TranslationMap) Tag

func (t TranslationMap) Tag() language.Tag

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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