Skip to content

Commit a57b617

Browse files
authored
[skip-changelog] I18n fetch language codes from transifex (arduino#736)
* fetch available languages from transifex * fix documentation on i18n update * add documentation on contributing for i18n workflow
1 parent e1eec95 commit a57b617

File tree

4 files changed

+87
-17
lines changed

4 files changed

+87
-17
lines changed

Taskfile.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ tasks:
124124
i18n:pull:
125125
desc: Pull i18n files from transifex
126126
cmds:
127-
- go run ./i18n/cmd/main.go transifex pull -l {{.I18N_LANGS}} ./i18n/data
127+
- go run ./i18n/cmd/main.go transifex pull ./i18n/data
128128
- task: i18n:generate
129129

130130
i18n:push:
@@ -169,4 +169,3 @@ vars:
169169
DOCS_VERSION: dev
170170
DOCS_ALIAS: ""
171171
DOCS_REMOTE: "origin"
172-
I18N_LANGS: "pt_BR"

docs/CONTRIBUTING.md

+37
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,43 @@ a list of items you can check before submitting a PR:
190190
failures that seem
191191
not related to the change you are making.
192192

193+
## Internationalization (i18n)
194+
195+
In order to support i18n in the cli, any messages that are intended to be translated
196+
should be wrapped in a call to `i18n.Tr`. This call allows us to build a catalog of
197+
translatable strings, replacing the reference string at runtime with the localized value.
198+
199+
Adding or modifying these messages requires an i18n update, as this process creates the
200+
reference catalog that are shared with translators. For that reason, the `task check`
201+
command will fail if the catalog was not updated to sync with changes to the source code.
202+
203+
To update the catalog, execute the following command and commit the changes.
204+
205+
```shell
206+
task i18n:update
207+
```
208+
209+
To verify that the catalog is up-to-date, you may execute the command:
210+
211+
```shell
212+
task i18n:check
213+
```
214+
215+
Example usage:
216+
217+
```golang
218+
package main
219+
220+
import (
221+
"fmt"
222+
"github.com/arduino/arduino-cli/i18n"
223+
)
224+
225+
func main() {
226+
fmt.Println(i18n.Tr("Hello World!"))
227+
}
228+
```
229+
193230
## Additional settings
194231

195232
If you need to push a commit that's only shipping documentation changes or

i18n/README.md

-9
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,3 @@ task i18n:push
4242
```sh
4343
task i18n:pull
4444
```
45-
46-
## Adding a new language
47-
48-
To add a new supported language add the locale string to the project's Taskfile.yml (comma separated list)
49-
50-
e.g
51-
```
52-
I18N_LANGS: "pt_BR,es,jp"
53-
```

i18n/cmd/commands/transifex/pull_transifex.go

+49-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package transifex
1717

1818
import (
19+
"encoding/json"
1920
"fmt"
2021
"io/ioutil"
2122
"net/http"
@@ -26,20 +27,62 @@ import (
2627
)
2728

2829
var pullTransifexCommand = &cobra.Command{
29-
Use: "pull -l pt_BR [catalog folder]",
30+
Use: "pull [catalog folder]",
3031
Short: "pulls the translation files from transifex",
31-
Args: cobra.ExactArgs(1),
3232
Run: pullCatalog,
3333
}
3434

35-
var languages = []string{}
35+
func getLanguages() []string {
36+
req, err := http.NewRequest(
37+
"GET",
38+
fmt.Sprintf(
39+
"https://www.transifex.com/api/2/project/%s/resource/%s/stats/",
40+
project, resource,
41+
), nil)
42+
43+
if err != nil {
44+
fmt.Println(err.Error())
45+
os.Exit(1)
46+
}
47+
48+
req.SetBasicAuth("api", apiKey)
49+
50+
resp, err := http.DefaultClient.Do(req)
51+
52+
if err != nil {
53+
fmt.Println(err.Error())
54+
os.Exit(1)
55+
}
56+
57+
defer resp.Body.Close()
58+
59+
b, err := ioutil.ReadAll(resp.Body)
60+
if err != nil {
61+
fmt.Println(err.Error())
62+
os.Exit(1)
63+
}
64+
65+
var jsonResp map[string]interface{}
66+
if err := json.Unmarshal(b, &jsonResp); err != nil {
67+
fmt.Println(err.Error())
68+
os.Exit(1)
69+
}
3670

37-
func init() {
38-
pullTransifexCommand.Flags().StringSliceVarP(&languages, "languages", "l", nil, "languages")
39-
pullTransifexCommand.MarkFlagRequired("languages")
71+
var langs []string
72+
for key := range jsonResp {
73+
if key == "en" {
74+
continue
75+
}
76+
langs = append(langs, key)
77+
}
78+
79+
return langs
4080
}
4181

4282
func pullCatalog(cmd *cobra.Command, args []string) {
83+
languages := getLanguages()
84+
fmt.Println("translations found:", languages)
85+
4386
folder := args[0]
4487

4588
for _, lang := range languages {

0 commit comments

Comments
 (0)