diff --git a/README.md b/README.md index 7467766..606c2d1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # tr + > Easy drop-in i18n solution for Go applications. [![GoDoc](https://godoc.org/github.com/tucnak/tr?status.svg)](https://godoc.org/github.com/tucnak/tr) @@ -9,6 +10,7 @@ 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 @@ -31,36 +33,39 @@ Here's how `tr` works: since `tr` ignores extensions anyway. 2. Init `tr` properly in your program: - ```go - package main - import ( - "fmt" - "os" + ```go + package main - "github.com/tucnak/tr" - ) + import ( + "fmt" + "os" - func init() { - // tr.Init(localesDirectory, defaultLocale) - if err := tr.Init("lang", "en"); err != nil { - fmt.Println(err) - os.Exit(1) - } - } - ``` + "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: - ```go - // 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")) - ``` + + ```go + // 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 `\n`s from the end of the string returned. diff --git a/engine.go b/engine.go index f891b43..e8fa22b 100644 --- a/engine.go +++ b/engine.go @@ -67,10 +67,14 @@ type Engine struct { // Lang returns a *Locale by name. func (e *Engine) Lang(localeName string) *Locale { if e.Langs == nil { - panic("tr: default engine is not sent, see tr.Init()") + panic("tr: default engine is not set, see tr.Init()") + } + + if l, ok := e.Langs[localeName]; ok { + return l } - return e.Langs[localeName] + panic("tr: Unknown localeName") } // Tr provides default locale's translation of path. diff --git a/tr.go b/tr.go index 6563a9a..00c710c 100644 --- a/tr.go +++ b/tr.go @@ -1,18 +1,12 @@ package tr -// DefaultEngine is what used when calling package-scope Tr(). -var DefaultEngine *Engine +import ( + "fmt" +) -func init() { - DefaultEngine = &Engine{} -} - -// Pass TrimEnd as an optional 3rd argument to trim \n ending. -const TrimEnd = true - -// Init is used to set the locales directory, as well as the +// Init is used to instantiate an Engine with its locales directory, as well as the // default locale. -func Init(path, defaultLocale string, trimOptional ...bool) error { +func Init(path, defaultLocale string, trimOptional ...bool) (*Engine, error) { trim := false if len(trimOptional) > 0 { trim = true @@ -20,19 +14,8 @@ func Init(path, defaultLocale string, trimOptional ...bool) error { engine, err := NewEngine(path, defaultLocale, trim) if err != nil { - return err + return nil, fmt.Errorf("Could not load new engine. Error: %v", err) } - DefaultEngine = engine - return nil -} - -// Tr provides default locale's translation of path. -func Tr(path string) string { - return DefaultEngine.Tr(path) -} - -// Lang returns a *Locale by name. -func Lang(localeName string) *Locale { - return DefaultEngine.Lang(localeName) -} + return engine, nil +} \ No newline at end of file