languages

package
Version: v0.0.0-...-e7b361f Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2018 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package languages provides a mechanism to create and retrieve translations for languages. A language can be backed by fallback languages, so if a translation does not exist in the language, its fallback languages are checked in the specified order. Translations are pure Go code and thus compiled with the program, increasing portability.

Example
package main

import (
	"fmt"

	"github.com/ChristianSiegert/go-packages/i18n/languages"
)

func main() {
	language := languages.NewLanguage("de", "German")
	language.Set("greeting", "Hallo")

	text := language.T("greeting")
	fmt.Println(text)
}
Output:

Hallo
Example (WithData)
package main

import (
	"fmt"

	"github.com/ChristianSiegert/go-packages/i18n/languages"
)

func main() {
	language := languages.NewLanguage("de", "German")
	language.Set("greeting", "Hallo {{.Name}}")

	text := language.T("greeting", map[string]interface{}{"Name": "Christian"})
	fmt.Println(text)
}
Output:

Hallo Christian
Example (WithFallbackLanguages)
package main

import (
	"fmt"

	"github.com/ChristianSiegert/go-packages/i18n/languages"
)

func main() {
	german := languages.NewLanguage("de", "German")
	german.Set("greeting", "Hallo {{.Name}}")

	english := languages.NewLanguage("en", "English")
	english.Set("greeting", "Hello {{.Name}}")

	spanish := languages.NewLanguage("es", "Spanish")
	spanish.Set("greeting", "Hola {{.Name}}")
	spanish.Set("farewell", "Adiós {{.Name}}")

	german.Fallbacks = []*languages.Language{english, spanish}

	text := german.T("greeting", map[string]interface{}{"Name": "Christian"})
	fmt.Println(text)

	text = german.T("farewell", map[string]interface{}{"Name": "Christian"})
	fmt.Println(text)

}
Output:

Hallo Christian
Adiós Christian

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Language

type Language struct {
	// Language code, e.g. “de”, “en” or “en-US”.
	Code string

	// Fallback languages to check when a translation is missing for this
	// language.
	Fallbacks []*Language

	// Language name, e.g. “German”.
	Name string

	// Translation IDs and associated translation.
	Translations map[string]*Translation
}

Language is a set of translation IDs and their translation text.

func NewLanguage

func NewLanguage(code, name string) *Language

NewLanguage returns a new instance of Language. Code is the language code, e.g. “de”, “en” or “en-US”. Name is the language name, e.g. “German”.

func (*Language) Get

func (l *Language) Get(translationID string) *Translation

Get retrieves a translation. If translationID cannot be found, nil is returned.

func (*Language) Remove

func (l *Language) Remove(translationIDs ...string)

Remove removes translations from the language.

func (*Language) Set

func (l *Language) Set(translationID string, translation interface{}) (*Translation, error)

Set adds a translation identified by translationID to the language. If a translation with the provided translationID already exists, it is replaced. translation can be of type string or *Translation.

func (*Language) SetMulti

func (l *Language) SetMulti(translations map[string]interface{}) error

SetMulti adds translations to the language. translations is a map of translation ID as key and translation as value. If a translation with the provided translation ID already exists, it is replaced. translation can be of type string or *Translation.

func (*Language) T

func (l *Language) T(translationID string, args ...map[string]interface{}) string

T returns the translation associated with translationID. If the translation is missing from l, l.Fallbacks will be checked. If the translation is still missing, translationID is returned. Args is optional. The first item of args is provided to the translation as data, additional items are ignored.

type Translation

type Translation struct {
	Zero,
	One,
	Two,
	Few,
	Many,
	Other *template.Template
}

Translation allows to store multiple versions of a translation, one for each plural group. See <http://cldr.unicode.org/index/cldr-spec/plural-rules>.

Jump to

Keyboard shortcuts

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