-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## go-loco | ||
|
||
A simple lib to fetch translation files from [Localise](https://localise.biz/) and save it in a [go-i18n](https://github.com/nicksnyder/go-i18n) compatible format. | ||
|
||
## Usage | ||
|
||
```golang | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
loco "github.com/lucasmdrs/go-loco" | ||
"github.com/nicksnyder/go-i18n/i18n" | ||
) | ||
|
||
func main() { | ||
err := loco.FetchTranslations("MY_LOCALISE_KEY", "./", "pt-BR", "en-US") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
i18n.MustLoadTranslationFile("en-US.json") | ||
i18n.MustLoadTranslationFile("pt-BR.json") | ||
|
||
T, _ := i18n.Tfunc("pt-BR") | ||
|
||
fmt.Println(T("hello")) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package loco | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const endpointTemplate = "https://localise.biz:443/api/export/%s.json?key=%s" | ||
|
||
// Translation represents a translation from Loco | ||
// as structure that is compatible with go-i18n | ||
type Translation struct { | ||
ID string `json:"id"` | ||
Translation string `json:"translation"` | ||
} | ||
|
||
// FetchTranslations creates translations files from a Loco Project | ||
// in the destination path it will try to | ||
// fetch all available languages if none is provided | ||
func FetchTranslations(locoKey, destinationPath string, langs ...string) error { | ||
if len(langs) > 0 { | ||
return fetchLangSpecifics(locoKey, destinationPath, langs) | ||
} | ||
return fetchAllLang(locoKey, destinationPath) | ||
} | ||
|
||
func fetchSpecificLang(locoKey, destinationPath, lang string) error { | ||
uri := fmt.Sprintf(endpointTemplate, "locale/"+lang, locoKey) | ||
locoData, err := sendLocoRequest(uri) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
translationList := keyValueToTranslation(locoData) | ||
err = writeToFile(translationList, destinationPath, lang) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func sendLocoRequest(url string) (map[string]interface{}, error) { | ||
res, err := http.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
var locoData map[string]interface{} | ||
err = json.NewDecoder(res.Body).Decode(&locoData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return locoData, nil | ||
|
||
} | ||
|
||
func fetchLangSpecifics(locoKey, destinationPath string, langs []string) error { | ||
for _, lang := range langs { | ||
err := fetchSpecificLang(locoKey, destinationPath, lang) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func fetchAllLang(locoKey, destinationPath string) error { | ||
uri := fmt.Sprintf(endpointTemplate, "all", locoKey) | ||
locoData, err := sendLocoRequest(uri) | ||
if err != nil { | ||
return err | ||
} | ||
for lang, keys := range locoData { | ||
translationList := keyValueToTranslation(keys.(map[string]interface{})) | ||
err := writeToFile(translationList, destinationPath, lang) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func keyValueToTranslation(assets map[string]interface{}) []Translation { | ||
list := make([]Translation, 0) | ||
for id, translation := range assets { | ||
list = append(list, Translation{ID: id, Translation: translation.(string)}) | ||
} | ||
return list | ||
} | ||
|
||
func writeToFile(translationList []Translation, path, lang string) error { | ||
preparePath(&path) | ||
output, err := json.Marshal(translationList) | ||
if err != nil { | ||
return err | ||
} | ||
return ioutil.WriteFile(fmt.Sprintf(path, lang), output, 0644) | ||
} | ||
|
||
func preparePath(path *string) { | ||
if !strings.HasSuffix(*path, "/") { | ||
*path = *path + "/" | ||
} | ||
*path = *path + "%s.json" | ||
} |