i18n

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

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

Go to latest
Published: Jan 9, 2016 License: MIT Imports: 4 Imported by: 0

README

i18n

GoDoc Build Status

TOML i18n for Go. Fork of i18n-go.

Author

Andrea Franz - http://gravityblast.com

Andrew 'ando' Wayne - http://github.com/dre1080

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Documentation

Overview

Package i18n implements i18n with simple config file or custom backends. Files are simple config files based on https://github.com/mojombo/toml.

You can load a file with i18n.Load:

err := i18n.Load("sample.conf")

You can also load multiple files passing a glob pattern:

i18n.Load("/path/to/locals/*.conf")

Add all your translations under the "en" section:

[en]
greeting = "hello"

Inside a file you can have multiple sections. Each section will be a different locale:

// file sample.conf

[en]
greeting = "hello"

[it]
greeting = "ciao"

[es]
greeting = "hola"

Check the example folder for a complete example https://github.com/dre1080/i18n/tree/master/example

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddLocale

func AddLocale(locale *Locale)

Adds a locale to the available locales.

func Load

func Load(globPattern string) error

Loads translations from one or more files passing globPatterns to filepath.Glob.

func SetLocale

func SetLocale(code string) error

Sets the current locale. Returns a LocaleMissing error if no locale has been added with the specified code.

func T

func T(key string, args ...interface{}) (string, error)

Alias for Translate.

func Translate

func Translate(key string, args ...interface{}) (string, error)

Calls Translate on the current locale. An error is returned if the current locale is not set or if the translation is missing in the current locale.

Types

type Locale

type Locale struct {
	Code         string
	Translations map[string]*Translation
}

Locale represents a locale with its Code and translations

func CurrentLocale

func CurrentLocale() *Locale

Returns the current locale.

func GetLocale

func GetLocale(code string) *Locale

Return the locale with the specified code

func NewLocale

func NewLocale(code string) *Locale

Returns a new locale with the specified code.

func (*Locale) Add

func (locale *Locale) Add(key, value string) *Translation

Adds a key and its translation to locale.

func (*Locale) T

func (locale *Locale) T(key string, args ...interface{}) (string, error)

Alias for Translate

func (*Locale) Translate

func (locale *Locale) Translate(key string, args ...interface{}) (string, error)

Returns the translation for the specified key. The translation and args are passed to Sprintf. If you have a key "message" with translation "hello %s", calling Translate("message", "world") will return "hello world". An error is returned if the key is missing.

type LocaleMissing

type LocaleMissing struct {
	Code string
}

func (LocaleMissing) Error

func (m LocaleMissing) Error() string

type Options

type Options map[string]string

type Sections

type Sections map[string]Options

type Translation

type Translation [2]string

Translation is a pair of strings, the key and the value, which is the actual translation.

func NewTranslation

func NewTranslation(key, value string) *Translation

Returns a new Translation.

func (*Translation) Format

func (t *Translation) Format(args ...interface{}) string

Returns fmt.Sprintf(t.Value(), args...)

func (*Translation) Key

func (t *Translation) Key() string

func (*Translation) Value

func (t *Translation) Value() string

type TranslationMissing

type TranslationMissing struct {
	Key string
}

func (TranslationMissing) Error

func (t TranslationMissing) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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