tr

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

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

Go to latest
Published: Dec 7, 2017 License: MIT Imports: 6 Imported by: 16

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)
    	if err := tr.Init("lang", "en"); err != nil {
    		fmt.Println(err)
    		os.Exit(1)
    	}
    }
    
  3. Use simple syntax for i18n:

    // Inline syntax:
    fmt.Println("In English:", tr.Lang("en").Tr("hello"))
    fmt.Println("In French:", tr.Lang("fr").Tr("hello"))
    fmt.Println("In Russian:", tr.Lang("ru").Tr("hello"))
    
    // Shadowing
    tr := tr.Lang("fr")
    fmt.Println(tr.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

View Source
const TrimEnd = true

Pass TrimEnd as an optional 3rd argument to trim \n ending.

Variables

This section is empty.

Functions

func Init

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

Init is used to set the locales directory, as well as the default locale.

func Tr

func Tr(path string) string

Tr provides default locale's translation of path.

Types

type Engine

type Engine struct {
	Path          string
	DefaultLocale *Locale

	Langs map[string]*Locale
}

Engine represent a storage of locales.

var DefaultEngine *Engine

DefaultEngine is what used when calling package-scope Tr().

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

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 Lang

func Lang(localeName string) *Locale

Lang returns a *Locale by name.

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

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