translate

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

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

Go to latest
Published: Jul 1, 2017 License: MIT Imports: 5 Imported by: 0

README

translate-i18-go

A golang implementation of an i18n library.

GoDoc

https://godoc.org/github.com/jzs/translate-i18-go

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errlog

type Errlog func(f string, data ...interface{})

Errlog is an interface that one can embed to catch strings that is not translated. It could be helpful if you want to be notified about missing translations.

type Language

type Language struct {
	ID   string
	Keys map[string]Value
}

Language represents a language that has been loaded

func LoadYaml

func LoadYaml(lang io.Reader, id string) (*Language, error)

LoadYaml loads a language from a yaml data source

type T

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

T is the instance of a particular translation

func (T) Other

func (t T) Other() T

Other returns T with plurality set to other

func (T) Plural

func (t T) Plural(count, many uint64) T

Plural returns T with a plurality set to a meaningful value

func (T) String

func (t T) String() string

String renders the resulting translation with count and args merged

func (T) With

func (t T) With(args interface{}) T

With assigns an object to T that will be merged with the translated value

func (T) Zero

func (t T) Zero() T

Zero returns T with plurality set to zero

type Translator

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

Translator struct

func New

func New(langs ...*Language) *Translator

New returns a new translator with given languages

Example
package main

import (
	"bytes"
	"fmt"

	translate "github.com/jzs/translate-i18-go"
)

func main() {
	file := `# English file
title:
  zero: No world
  one: One world
  many: Many worlds
  other: Other world

apple.count:
  zero: No apples
  one: 1 apple
  few: "{{.Count}} apples"
  many: "Many apples"
  other: "{{.Fart}} other apples"
`

	// Create a new translator with loaded languages and translate.
	enstream := bytes.NewReader([]byte(file))
	en, err := translate.LoadYaml(enstream, "en-us")
	if err != nil {
		panic(err)
	}
	ts := translate.New(en)
	fun := ts.Tfunc("en-us")
	fmt.Println(fun("title"))

	fmt.Println(fun("apple.count").Plural(0, 10))  // Zero
	fmt.Println(fun("apple.count").Plural(1, 10))  // One
	fmt.Println(fun("apple.count").Plural(5, 10))  // Few
	fmt.Println(fun("apple.count").Plural(11, 10)) // Many
	data := map[string]string{"Fart": "some"}
	fmt.Println(fun("apple.count").With(data).Other()) // Other
}
Output:

func (*Translator) SetLog

func (t *Translator) SetLog(log Errlog)

SetLog sets a logger on the translator for catching strings where a key or a plurality is missing

func (*Translator) Tfunc

func (t *Translator) Tfunc(languages ...string) func(string) T

Tfunc returns a translator func with languages in a given order. First language in the array has first priority, if a key is not found there, it will look in the second language etc.

type Value

type Value struct {
	Zero  string `json:"zero"`
	One   string `json:"one"`
	Few   string `json:"few"`
	Many  string `json:"many"`
	Other string `json:"other"`
}

Value is the translated values for a key

Jump to

Keyboard shortcuts

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