From f19aad1834369c17ab8165a2102645b02fd913c8 Mon Sep 17 00:00:00 2001 From: Arinze Okeke Date: Sun, 17 Dec 2017 23:16:15 +0100 Subject: [PATCH 1/3] update TrimEnd comment to follow godoc convention --- tr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tr.go b/tr.go index 6563a9a..95774f8 100644 --- a/tr.go +++ b/tr.go @@ -7,7 +7,7 @@ func init() { DefaultEngine = &Engine{} } -// Pass TrimEnd as an optional 3rd argument to trim \n ending. +// TrimEnd is an optional 3rd argument to trim \n ending. const TrimEnd = true // Init is used to set the locales directory, as well as the From 15ad8c14b1de319ec515ce9dffe17329a86f4f19 Mon Sep 17 00:00:00 2001 From: Arinze Okeke Date: Sun, 17 Dec 2017 23:38:49 +0100 Subject: [PATCH 2/3] return Engine on Init,rm reduntant Tr and Lang from tr.go, check for localeName typo in Lang function, some typo fixes --- engine.go | 8 ++++++-- tr.go | 33 ++++++++------------------------- 2 files changed, 14 insertions(+), 27 deletions(-) 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 95774f8..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{} -} - -// TrimEnd is 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 From 0cdfee0873cd4bc3f3c105ab3f9674649fb2cda0 Mon Sep 17 00:00:00 2001 From: Arinze Okeke Date: Sun, 17 Dec 2017 23:47:19 +0100 Subject: [PATCH 3/3] update readme --- README.md | 55 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 25 deletions(-) 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.