Commit a05eced2 authored by Henrique's avatar Henrique Committed by umbynos

[skip-changelog] I18n fetch language codes from transifex (#736)

* fetch available languages from transifex

* fix documentation on i18n update

* add documentation on contributing for i18n workflow
parent e1eec95b
......@@ -124,7 +124,7 @@ tasks:
i18n:pull:
desc: Pull i18n files from transifex
cmds:
- go run ./i18n/cmd/main.go transifex pull -l {{.I18N_LANGS}} ./i18n/data
- go run ./i18n/cmd/main.go transifex pull ./i18n/data
- task: i18n:generate
i18n:push:
......@@ -169,4 +169,3 @@ vars:
DOCS_VERSION: dev
DOCS_ALIAS: ""
DOCS_REMOTE: "origin"
I18N_LANGS: "pt_BR"
......@@ -190,6 +190,43 @@ a list of items you can check before submitting a PR:
failures that seem
not related to the change you are making.
## Internationalization (i18n)
In order to support i18n in the cli, any messages that are intended to be translated
should be wrapped in a call to `i18n.Tr`. This call allows us to build a catalog of
translatable strings, replacing the reference string at runtime with the localized value.
Adding or modifying these messages requires an i18n update, as this process creates the
reference catalog that are shared with translators. For that reason, the `task check`
command will fail if the catalog was not updated to sync with changes to the source code.
To update the catalog, execute the following command and commit the changes.
```shell
task i18n:update
```
To verify that the catalog is up-to-date, you may execute the command:
```shell
task i18n:check
```
Example usage:
```golang
package main
import (
"fmt"
"github.com/arduino/arduino-cli/i18n"
)
func main() {
fmt.Println(i18n.Tr("Hello World!"))
}
```
## Additional settings
If you need to push a commit that's only shipping documentation changes or
......
......@@ -42,12 +42,3 @@ task i18n:push
```sh
task i18n:pull
```
## Adding a new language
To add a new supported language add the locale string to the project's Taskfile.yml (comma separated list)
e.g
```
I18N_LANGS: "pt_BR,es,jp"
```
\ No newline at end of file
......@@ -16,6 +16,7 @@
package transifex
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
......@@ -26,20 +27,62 @@ import (
)
var pullTransifexCommand = &cobra.Command{
Use: "pull -l pt_BR [catalog folder]",
Use: "pull [catalog folder]",
Short: "pulls the translation files from transifex",
Args: cobra.ExactArgs(1),
Run: pullCatalog,
}
var languages = []string{}
func getLanguages() []string {
req, err := http.NewRequest(
"GET",
fmt.Sprintf(
"https://www.transifex.com/api/2/project/%s/resource/%s/stats/",
project, resource,
), nil)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
req.SetBasicAuth("api", apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var jsonResp map[string]interface{}
if err := json.Unmarshal(b, &jsonResp); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
func init() {
pullTransifexCommand.Flags().StringSliceVarP(&languages, "languages", "l", nil, "languages")
pullTransifexCommand.MarkFlagRequired("languages")
var langs []string
for key := range jsonResp {
if key == "en" {
continue
}
langs = append(langs, key)
}
return langs
}
func pullCatalog(cmd *cobra.Command, args []string) {
languages := getLanguages()
fmt.Println("translations found:", languages)
folder := args[0]
for _, lang := range languages {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment