tr

package module
v0.0.0-...-1771f0c Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2017 License: MIT Imports: 7 Imported by: 0

README

tr

Easy drop-in i18n solution for Go applications.

GoDoc

I was looking for a real easy way to provide i18n support for my Telegram bot, for which the data is pretty much a set of 20 different text messages. I couldn't find a single solution that would utilize the file system. Here's how tr works:

  1. You have to create a locales directory, e.g. $ tree lang:

    lang
    ├── en
    │   ├── hello.txt
    │   └── inner
    │       └── text.txt
    ├── fr
    │   ├── hello.txt
    │   └── inner
    │       └── text.txt
    └── ru
        ├── hello.txt
        └── inner
            └── text.html
    
    6 directories, 6 files
    

    Your files could be of any extension, it doesn't really matter, since tr ignores extensions anyway.

  2. Init tr properly in your program:

    package main
    
    import (
    	"fmt"
    	"os"
    
    	"github.com/tucnak/tr"
    )
    
    func init() {
    	// tr.Init(localesDirectory, defaultLocale)
        engine, err := tr.Init("lang", "en")
    	if err != nil {
    		fmt.Println(err)
    		os.Exit(1)
    	}
    }
    
  3. Use simple syntax for i18n:

    // Inline syntax:
    fmt.Println("In English:", engine.Lang("en").Tr("hello"))
    fmt.Println("In French:", engine.Lang("fr").Tr("hello"))
    fmt.Println("In Russian:", engine.Lang("ru").Tr("hello"))
    
    // Shadowing
    engine := engine.Lang("fr")
    fmt.Println(engine.Tr("inner/text"))
    

Pass an optional third true argument to tr.Init() if you wish to trim all \ns from the end of the string returned.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	Path          string
	DefaultLocale *Locale

	Langs map[string]*Locale
}

Engine represent a storage of locales.

func Init

func Init(path, defaultLocale string, trimOptional ...bool) (*Engine, error)

Init is used to instantiate an Engine with its locales directory, as well as the default locale.

func NewEngine

func NewEngine(path, defaultLocale string, trim bool) (*Engine, error)

NewEngine constructs a new translation engine.

func (*Engine) Lang

func (e *Engine) Lang(localeName string) *Locale

Lang returns a *Locale by name.

func (*Engine) Tr

func (e *Engine) Tr(path string) (string, error)

Tr provides default locale's translation of path.

type Locale

type Locale struct {
	Root string
	Name string
	Trim bool
	// contains filtered or unexported fields
}

Locale is a radix tree of available translation paths.

func NewLocale

func NewLocale(root, name string, paths []string, trim bool) (*Locale, error)

NewLocale constructs a new locale.

func (*Locale) Tr

func (c *Locale) Tr(path string) (string, error)

Tr returns locale's translation for path.

Jump to

Keyboard shortcuts

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