| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/leonelquinteros/gotext" |
| 8 |
alias "github.com/leonelquinteros/gotext" |
| 9 |
"github.com/leonelquinteros/gotext/cli/xgotext/fixtures/pkg" |
| 10 |
) |
| 11 |
|
| 12 |
// Fake object with methods similar to gotext |
| 13 |
type Fake struct { |
| 14 |
} |
| 15 |
|
| 16 |
// Get by id |
| 17 |
func (f Fake) Get(id int) int { |
| 18 |
return 42 |
| 19 |
} |
| 20 |
|
| 21 |
// Fake object with same methods as gotext |
| 22 |
type Fake2 struct { |
| 23 |
} |
| 24 |
|
| 25 |
// Get by str |
| 26 |
func (f Fake2) Get(s string) string { |
| 27 |
return s |
| 28 |
} |
| 29 |
|
| 30 |
func main() { |
| 31 |
// Configure package |
| 32 |
gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name") |
| 33 |
|
| 34 |
// Translate text from default domain |
| 35 |
fmt.Println(gotext.Get("My text on 'domain-name' domain")) |
| 36 |
// same as before |
| 37 |
fmt.Println(gotext.Get("My text on 'domain-name' domain")) |
| 38 |
|
| 39 |
// unsupported function call |
| 40 |
trStr := "some string to translate" |
| 41 |
fmt.Println(gotext.Get(trStr)) |
| 42 |
|
| 43 |
// same with alias package name |
| 44 |
fmt.Println(alias.Get("alias call")) |
| 45 |
|
| 46 |
// Translate text from a different domain without reconfigure |
| 47 |
fmt.Println(gotext.GetD("domain2", "Another text on a different domain")) |
| 48 |
|
| 49 |
// Create Locale with library path and language code |
| 50 |
l := gotext.NewLocale("/path/to/locales/root/dir", "es_UY") |
| 51 |
|
| 52 |
// Load domain '/path/to/locales/root/dir/es_UY/default.po' |
| 53 |
l.AddDomain("translations") |
| 54 |
l.SetDomain("translations") |
| 55 |
|
| 56 |
// Translate text from domain |
| 57 |
fmt.Println(l.GetD("translations", "Translate this")) |
| 58 |
|
| 59 |
// Get plural translations |
| 60 |
l.GetN("Singular", "Plural", 4) |
| 61 |
num := 17 |
| 62 |
l.GetN("SingularVar", "PluralVar", num) |
| 63 |
|
| 64 |
l.GetDC("domain2", "string", "ctx") |
| 65 |
l.GetNDC("translations", "ndc", "ndcs", 7, "NDC-CTX") |
| 66 |
|
| 67 |
// try fake structs |
| 68 |
f := Fake{} |
| 69 |
f.Get(3) |
| 70 |
|
| 71 |
f2 := Fake2{} |
| 72 |
f2.Get("3") |
| 73 |
|
| 74 |
// use translator of sub object |
| 75 |
t := pkg.Translate{} |
| 76 |
t.L.Get("translate package") |
| 77 |
t.S.L.Get("translate sub package") |
| 78 |
|
| 79 |
// redefine alias with fake struct |
| 80 |
alias := Fake2{} |
| 81 |
alias.Get("3") |
| 82 |
|
| 83 |
err := errors.New("test") |
| 84 |
fmt.Print(err.Error()) |
| 85 |
} |
| 86 |
|
| 87 |
// dummy function |
| 88 |
func dummy(locale *gotext.Locale) { |
| 89 |
locale.Get("inside dummy") |
| 90 |
} |