Skip to content

I18n fetch language codes from transifex #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -169,4 +169,3 @@ vars:
DOCS_VERSION: dev
DOCS_ALIAS: ""
DOCS_REMOTE: "origin"
I18N_LANGS: "pt_BR"
37 changes: 37 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
55 changes: 49 additions & 6 deletions i18n/cmd/commands/transifex/pull_transifex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package transifex

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -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 {
Expand Down