You can use https://github.com/nicksnyder/go-i18n/
Then in your project you have to create a folder called i18n/ and use a function like this:
import (
"fmt"
"io/ioutil"
"github.com/nicksnyder/go-i18n/i18n"
)
func loadI18nFiles() {
files, _ := ioutil.ReadDir("i18n")
exists := false
for _, file := range files {
if err := i18n.LoadTranslationFile(fmt.Sprintf("i18n/%s", file.Name())); err != nil {
log.Errorf("i18n: error loading file %s. err: %s", file.Name(), err)
} else {
log.Infof("i18n: lang file %s loaded", file.Name())
}
# Check if you have a default language
if file.Name() == fmt.Sprintf("%s.json", "en-US") {
exists = true
}
}
if !exists {
panic(fmt.Sprintf("Hey! You can't use a default language (%s) that doesn't exists on i18n folder", props.DefaultLang))
}
}
Then to use, import the package and call the function:
T, _ := i18n.Tfunc("es-AR", "en-US")
fmt.Printf(T("key"))
Each file inside i18n folder is a .json
Example:
en-US.json
[
{
"id": "key",
"translation": "Hello World"
}
]
es-AR.json
[
{
"id": "key",
"translation": "Hola Mundo"
}
]