From 8ac2fc6a858dd5db89fad583f0ecbf1278b4e4a8 Mon Sep 17 00:00:00 2001 From: Henrique Date: Tue, 2 Jun 2020 23:35:26 -0300 Subject: [PATCH 1/3] fetch available languages from transifex --- i18n/cmd/commands/transifex/pull_transifex.go | 55 +++++++++++++++++-- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/i18n/cmd/commands/transifex/pull_transifex.go b/i18n/cmd/commands/transifex/pull_transifex.go index e94755789cd..ad7212188ed 100644 --- a/i18n/cmd/commands/transifex/pull_transifex.go +++ b/i18n/cmd/commands/transifex/pull_transifex.go @@ -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 { From 53fdf5001804159365d27c57030e3b83804889a7 Mon Sep 17 00:00:00 2001 From: Henrique Date: Tue, 2 Jun 2020 23:35:59 -0300 Subject: [PATCH 2/3] fix documentation on i18n update --- Taskfile.yml | 3 +-- i18n/README.md | 9 --------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 6377dc6ac9b..d7b434a966f 100755 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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" diff --git a/i18n/README.md b/i18n/README.md index 417fc3cbdd8..43d07d4f5ac 100644 --- a/i18n/README.md +++ b/i18n/README.md @@ -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 From 84517c1c09fe47c558a1fcaa320b3fcf5e4576ae Mon Sep 17 00:00:00 2001 From: Henrique Date: Wed, 3 Jun 2020 00:11:00 -0300 Subject: [PATCH 3/3] add documentation on contributing for i18n workflow --- docs/CONTRIBUTING.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index fbac0fd703e..149ec142dc4 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -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