Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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.
8 changes: 6 additions & 2 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 8 additions & 25 deletions tr.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
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
}

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
}