-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
56 lines (46 loc) · 1.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"github.com/eduardolat/goeasyi18n"
)
func main() {
// 1. Create a new i18n instance
i18n := goeasyi18n.NewI18n()
// 2. Load your translations from a database, api, etc.
enTranslationsString := `[
{
"Key": "hello_world",
"Default": "Hello World"
}
]`
esTranslationsBytes := []byte(`[
{
"Key": "hello_world",
"Default": "Hola Mundo"
}
]`)
// 3. Then you can load the translations from your source
// In this case we are using JSON but you can also use YAML
enTranslations, err := goeasyi18n.LoadFromJsonString(enTranslationsString)
if err != nil {
panic(err)
}
// You can also load from bytes instead of strings
esTranslations, err := goeasyi18n.LoadFromJsonBytes(esTranslationsBytes)
if err != nil {
panic(err)
}
// 4. Add your languages with their translations
i18n.AddLanguage("en", enTranslations)
i18n.AddLanguage("es", esTranslations)
// 5. Get the translations
ten := i18n.T("en", "hello_world")
tes := i18n.T("es", "hello_world")
fmt.Println(ten)
fmt.Println(tes)
/*
Prints:
Hello World
Hola Mundo
*/
}