handlebars-i18n-cli 1.0.5 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/coveralls.yml +34 -17
- package/.github/workflows/node.js.yml +2 -2
- package/README.md +191 -55
- package/README_programmatic.md +195 -0
- package/bin/i18n-collect +36 -2
- package/bin/i18n-deepl +71 -0
- package/package.json +27 -16
- package/src/i18n-collect.js +162 -289
- package/src/i18n-deepl.js +304 -0
- package/src/index.js +4 -0
- package/test/handlebars-i18n-cli.test.mjs +472 -0
- package/tsconfig.json +20 -0
- package/types/i18n-collect.d.ts +5 -0
- package/types/i18n-collect.d.ts.map +1 -0
- package/types/i18n-deepl.d.ts +44 -0
- package/types/i18n-deepl.d.ts.map +1 -0
- package/types/index.d.ts +7 -0
- package/types/index.d.ts.map +1 -0
- package/want.md +18 -0
- package/src/deepl-free-api-key.json +0 -3
- package/src/deepl-pro-api-key.json +0 -3
- package/test/handlebars-i18n-cli.test.js +0 -174
- package/test/test-generated/test-12.json +0 -8
|
@@ -1,30 +1,47 @@
|
|
|
1
|
-
|
|
1
|
+
name: Test and Coverage
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ "main" ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ "main" ]
|
|
4
8
|
|
|
5
9
|
jobs:
|
|
6
|
-
|
|
7
|
-
build:
|
|
8
|
-
name: Build
|
|
10
|
+
test:
|
|
9
11
|
runs-on: ubuntu-latest
|
|
10
12
|
steps:
|
|
11
13
|
|
|
12
14
|
- uses: actions/checkout@v3
|
|
13
15
|
|
|
14
|
-
- name: Use Node.js
|
|
16
|
+
- name: Use Node.js 22.x
|
|
15
17
|
uses: actions/setup-node@v3
|
|
16
18
|
with:
|
|
17
|
-
node-version:
|
|
19
|
+
node-version: 22.x
|
|
20
|
+
|
|
21
|
+
- name: Verify Test Assets
|
|
22
|
+
run: mkdir test/test-generated && ls -R test
|
|
18
23
|
|
|
19
|
-
- name: npm install, make test-coverage
|
|
24
|
+
- name: npm install, make test-coverage on 22.x
|
|
20
25
|
run: |
|
|
21
26
|
npm install
|
|
22
|
-
npm test -- --
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
npm run test:coverage -- --verbose --timeout=10000
|
|
28
|
+
sleep 2
|
|
29
|
+
|
|
30
|
+
- name: Report Coveralls (Linux)
|
|
31
|
+
if: startsWith(runner.os, 'Linux')
|
|
32
|
+
run: curl -sL https://coveralls.io/coveralls-linux.tar.gz | tar -xz && ./coveralls
|
|
33
|
+
env:
|
|
34
|
+
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
|
|
35
|
+
|
|
36
|
+
- name: Report Coveralls (Windows)
|
|
37
|
+
if: startsWith(runner.os, 'Windows')
|
|
38
|
+
run: curl -sL https://github.com/coverallsapp/coverage-reporter/releases/latest/download/coveralls-windows.zip | zcat > ./coveralls.exe && ./coveralls.exe
|
|
39
|
+
env:
|
|
40
|
+
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
|
|
41
|
+
|
|
42
|
+
- name: Report Coveralls (macOS)
|
|
43
|
+
if: startsWith(runner.os, 'macOS')
|
|
44
|
+
run: |
|
|
45
|
+
brew tap coverallsapp/coveralls --quiet
|
|
46
|
+
brew install coveralls --quiet
|
|
47
|
+
coveralls
|
|
@@ -16,7 +16,7 @@ jobs:
|
|
|
16
16
|
|
|
17
17
|
strategy:
|
|
18
18
|
matrix:
|
|
19
|
-
node-version: [14.x, 16.x, 18.x]
|
|
19
|
+
node-version: [14.x, 16.x, 18.x, 20.x]
|
|
20
20
|
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
|
21
21
|
|
|
22
22
|
steps:
|
|
@@ -27,4 +27,4 @@ jobs:
|
|
|
27
27
|
node-version: ${{ matrix.node-version }}
|
|
28
28
|
cache: 'npm'
|
|
29
29
|
- run: npm ci
|
|
30
|
-
- run: npm run build --if-present
|
|
30
|
+
- run: npm run build --if-present
|
package/README.md
CHANGED
|
@@ -1,35 +1,42 @@
|
|
|
1
1
|
# handlebars-i18n-cli
|
|
2
2
|
|
|
3
|
-
`handlebars-i18n-cli` is an additional command line interface
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
`handlebars-i18n-cli` is an additional command line interface
|
|
4
|
+
for [handlebars-i18n](https://github.com/fwalzel/handlebars-i18n.git) and other implementations of i18next in handlebars.
|
|
5
|
+
|
|
6
|
+
* programmatically extract/ update translation strings from handlebars templates and generate i18next conform
|
|
7
|
+
JSON files from it
|
|
8
|
+
* automatic translation of i18next JSON via [DeepL’s](https://www.deepl.com/en/pro-api/) free API
|
|
6
9
|
|
|
7
10
|
[](https://opensource.org/licenses/MIT)
|
|
8
|
-

|
|
9
12
|
[](https://github.com/fwalzel/handlebars-i18n-cli/actions/workflows/node.js.yml/badge.svg)
|
|
10
13
|
[](https://coveralls.io/github/fwalzel/handlebars-i18n-cli?branch=main)
|
|
11
14
|
[](https://snyk.io/test/github/fwalzel/handlebars-i18n-cli/badge.svg)
|
|
12
15
|
|
|
13
|
-
|
|
14
16
|
## Install
|
|
15
17
|
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
+
```shell
|
|
19
|
+
npm i handlebars-i18n-cli --save-dev
|
|
20
|
+
npm link handlebars-i18n-cli
|
|
18
21
|
```
|
|
19
22
|
|
|
23
|
+
If you do not link the package, you may run into the error `bash: i18n-collect: command not found`.
|
|
20
24
|
|
|
21
|
-
##
|
|
25
|
+
## General Use
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
### 1. Language Key Extraction: _i18n-collect_
|
|
28
|
+
|
|
29
|
+
#### Syntax:
|
|
24
30
|
|
|
25
31
|
```
|
|
26
32
|
i18n-collect <source> <target> <options...>
|
|
27
33
|
```
|
|
28
34
|
|
|
29
|
-
This will generate a file `translations.json` holding the translations for `de`, `fr`, and `en` by extracting all key
|
|
35
|
+
This will generate a file `translations.json` holding the translations for `de`, `fr`, and `en` by extracting all key
|
|
36
|
+
names intended for i18next translation from all html files in your project:
|
|
30
37
|
|
|
31
|
-
```
|
|
32
|
-
|
|
38
|
+
```shell
|
|
39
|
+
i18n-collect my-project/**/*.html my-project/translations.json --lng de,en,fr
|
|
33
40
|
```
|
|
34
41
|
|
|
35
42
|
From a very simple template like this …
|
|
@@ -38,16 +45,14 @@ From a very simple template like this …
|
|
|
38
45
|
<!DOCTYPE html>
|
|
39
46
|
<html lang="{{_locale}}">
|
|
40
47
|
<head>
|
|
41
|
-
|
|
48
|
+
<title>{{__ title}}</title>
|
|
42
49
|
</head>
|
|
43
50
|
<body>
|
|
44
|
-
|
|
51
|
+
{{__ body.greeting textvar1="hello" textvar2="world"}}
|
|
45
52
|
</body>
|
|
46
53
|
</html>
|
|
47
54
|
```
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
|
|
51
56
|
… the generated translations.json would be:
|
|
52
57
|
|
|
53
58
|
```json
|
|
@@ -75,20 +80,58 @@ From a very simple template like this …
|
|
|
75
80
|
}
|
|
76
81
|
```
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
### 2. Automatic Translation via DeepL API: _i18n-deepl_
|
|
84
|
+
|
|
85
|
+
#### Syntax:
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
i18n-deepl translate <source> <target> <targetLang> <options...>
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### Example:
|
|
92
|
+
|
|
93
|
+
``` shell
|
|
94
|
+
i18n-deepl translate en.json fi.json fi
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Will run the file en.json against DeepL API. From this file
|
|
79
98
|
|
|
80
|
-
|
|
81
|
-
|
|
99
|
+
```
|
|
100
|
+
{
|
|
101
|
+
"header": {
|
|
102
|
+
"greet": "Hello World!"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
… will be generated the Finish translation fi.json:
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
{
|
|
111
|
+
"header": {
|
|
112
|
+
"greet": "Hei maailma!"
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
## Purpose / Motivation
|
|
119
|
+
|
|
120
|
+
Managing large volumes of translations can be a tedious and time-consuming task, for each change in the template needs
|
|
121
|
+
to be mapped to all languages. Usually this comes along with a lot of redundant typing or copy/paste action. Moreover
|
|
82
122
|
the chance of missing some translation strings increases with many translations in play.
|
|
83
123
|
|
|
84
|
-
`handlebars-i18n-cli` automates the task of extracting and updating key names indicating translation strings and
|
|
85
|
-
generating template JSON files from them. The key names for the translations need to specified only once in the
|
|
86
|
-
the carry to the according language JSON is done by the CLI. You then only have to fill in according translations.
|
|
87
|
-
In case a translation string expects variables for replacement, these variables will be added to your json template.
|
|
124
|
+
`handlebars-i18n-cli` automates the task of extracting and updating key names indicating translation strings and
|
|
125
|
+
generating template JSON files from them. The key names for the translations need to specified only once in the
|
|
126
|
+
template, the carry to the according language JSON is done by the CLI. You then only have to fill in according translations.
|
|
127
|
+
In case a translation string expects variables for replacement, these variables will be added to your json template.
|
|
88
128
|
|
|
89
|
-
If you are not using [handlebars-i18n](https://github.com/fwalzel/handlebars-i18n.git) for translation but a custom
|
|
90
|
-
integration of i18next into handlebars.js, you might be able to appropriate this cli by using the option --translFunc (
|
|
129
|
+
If you are not using [handlebars-i18n](https://github.com/fwalzel/handlebars-i18n.git) for translation but a custom
|
|
130
|
+
integration of i18next into handlebars.js, you might be able to appropriate this cli by using the option --translFunc (
|
|
131
|
+
see below).
|
|
91
132
|
|
|
133
|
+
Also `handlebars-i18n-cli` allows you to **auto-translate** a JSON file with an existing translation to another language
|
|
134
|
+
via the DeepL API, while the original key names and JSOn structure are kept.
|
|
92
135
|
|
|
93
136
|
## Example
|
|
94
137
|
|
|
@@ -97,26 +140,33 @@ Try the examples folder within this repo.
|
|
|
97
140
|
For generating a single JSON file:
|
|
98
141
|
|
|
99
142
|
```bash
|
|
100
|
-
|
|
143
|
+
i18n-collect examples/templates/*.html examples/generated/translations.json --lng de,fr,en
|
|
101
144
|
```
|
|
102
145
|
|
|
103
146
|
For one JSON file per language:
|
|
104
147
|
|
|
105
148
|
```bash
|
|
106
|
-
|
|
149
|
+
i18n-collect examples/templates/*.html examples/generated/translations.json --separateLngFiles --lng de,fr,en
|
|
107
150
|
```
|
|
108
151
|
|
|
152
|
+
## Programmatical Use
|
|
109
153
|
|
|
110
|
-
|
|
154
|
+
You can use `handlebars-i18n-cli` in a programmatical way too. For import and integration of the Javascript Functions see
|
|
155
|
+
[handlebars-i18n-cli Programmatical Use](README_programmatic.md).
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
## Detailed Description for Command _i18n-collect_
|
|
111
159
|
|
|
112
160
|
`<source>`
|
|
113
161
|
|
|
114
162
|
* The source files can be passed in as [glob](https://www.npmjs.com/package/glob) pattern.
|
|
115
|
-
* i18n-collect is agnostic against the data type of the template(s) you want to extract translations keys from. It works
|
|
163
|
+
* i18n-collect is agnostic against the data type of the template(s) you want to extract translations keys from. It works
|
|
164
|
+
with `.html` as well as `.js` files.
|
|
116
165
|
|
|
117
166
|
`<target>`
|
|
118
167
|
|
|
119
|
-
* The output will always be in `.json` format. The file(s) can then be required for your i18next translation
|
|
168
|
+
* The output will always be in `.json` format. The file(s) can then be required for your i18next translation
|
|
169
|
+
as [JSON v2](https://www.i18next.com/misc/json-format#i18next-json-v2)
|
|
120
170
|
|
|
121
171
|
```
|
|
122
172
|
i18next.init({
|
|
@@ -124,12 +174,11 @@ i18next.init({
|
|
|
124
174
|
});
|
|
125
175
|
```
|
|
126
176
|
|
|
127
|
-
|
|
128
|
-
## Usage options
|
|
177
|
+
### Usage options
|
|
129
178
|
|
|
130
179
|
`--alphabetical` or `-a`
|
|
131
180
|
|
|
132
|
-
This will order the keys to the translation strings alphabetically in the generated json file(s). When the flag
|
|
181
|
+
This will order the keys to the translation strings alphabetically in the generated json file(s). When the flag
|
|
133
182
|
--alphabetical is not set the keys appear in order as within the template(s).
|
|
134
183
|
|
|
135
184
|
---
|
|
@@ -147,7 +196,7 @@ value strings contain current language and key name.
|
|
|
147
196
|
|
|
148
197
|
Example:
|
|
149
198
|
|
|
150
|
-
The template
|
|
199
|
+
The template
|
|
151
200
|
|
|
152
201
|
```html
|
|
153
202
|
<h1>{{__ headline userName="Frank"}}</h1>
|
|
@@ -182,9 +231,10 @@ instead of
|
|
|
182
231
|
|
|
183
232
|
---
|
|
184
233
|
|
|
185
|
-
`--lng
|
|
234
|
+
`--lng language1,language2,...languageN`
|
|
186
235
|
|
|
187
|
-
The list of language shortcodes you want to be generated with an own set in the json. Arguments are comma separated (no
|
|
236
|
+
The list of language shortcodes you want to be generated with an own set in the json. Arguments are comma separated (no
|
|
237
|
+
blank space between, no quotation marks around).
|
|
188
238
|
If no language is defined, "en" is the default.
|
|
189
239
|
|
|
190
240
|
---
|
|
@@ -195,24 +245,24 @@ Logs the final result that is written to the json files(s) into the console as w
|
|
|
195
245
|
|
|
196
246
|
---
|
|
197
247
|
|
|
198
|
-
`--separateLngFiles` or `-sf`
|
|
248
|
+
`--separateLngFiles` or `-sf`
|
|
199
249
|
|
|
200
250
|
Write each language in a separate json file instead of a single one.
|
|
201
251
|
|
|
202
252
|
```bash
|
|
203
|
-
|
|
253
|
+
i18n-collect my-project/template.html my-project/translation.json --lng de,en,fr --separateLngFiles
|
|
204
254
|
```
|
|
205
255
|
|
|
206
|
-
Will generate three json files: **translation.de.json**, **translation.en.json**, and **translation.fn.json** each
|
|
207
|
-
only the translation for their respective language. By default all translations are written to a single json file.
|
|
256
|
+
Will generate three json files: **translation.de.json**, **translation.en.json**, and **translation.fn.json** each
|
|
257
|
+
holding only the translation for their respective language. By default all translations are written to a single json file.
|
|
208
258
|
|
|
209
259
|
---
|
|
210
260
|
|
|
211
261
|
`--translFunc=yourCustomFunctionName`
|
|
212
262
|
|
|
213
263
|
If you are not using handlebars-i18n for translations but a custom handlebars helper, you might be able to use
|
|
214
|
-
i18n-collect as well.Say your translation function has the name *t* instead of handlebars-i18n’s *__* (double
|
|
215
|
-
and your template usage would look like
|
|
264
|
+
i18n-collect as well.Say your translation function has the name *t* instead of handlebars-i18n’s *__* (double
|
|
265
|
+
underscore) and your template usage would look like
|
|
216
266
|
|
|
217
267
|
```html
|
|
218
268
|
{{t myKeyNameToTranslation}}
|
|
@@ -221,7 +271,7 @@ and your template usage would look like
|
|
|
221
271
|
you can do
|
|
222
272
|
|
|
223
273
|
```bash
|
|
224
|
-
|
|
274
|
+
i18n-collect my-project/template.html my-project/translation.json --translFunc=t
|
|
225
275
|
```
|
|
226
276
|
|
|
227
277
|
--translFunc=t then substitutes the default *__* with a search for t.
|
|
@@ -236,36 +286,122 @@ will be added.
|
|
|
236
286
|
Works also with the option --separateLngFiles:
|
|
237
287
|
|
|
238
288
|
```bash
|
|
239
|
-
|
|
289
|
+
i18n-collect my-project/**/*.html my-project/translation --update --lng de,en,fr --separateLngFiles
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Leave out the language ending and json file extension and give only the base name for <target>. In this example case
|
|
293
|
+
handlebars-i18n-cli would look for *translation.de.json*, *translation.en.json*, and *translation.en.json* to update
|
|
294
|
+
them. A language file that does not exist yet will be generated.
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
## Detailed Description for _i18n-deepl_ Commands
|
|
298
|
+
|
|
299
|
+
i18n-deepl is a command-line tool to translate i18next JSON files using the DeepL API. Below is a detailed guide to its
|
|
300
|
+
commands and usage. To use this tool, you need a valid DeepL API key. Set this key using the setAuth command or provide
|
|
301
|
+
it directly via the --auth-key option when running commands.
|
|
302
|
+
|
|
303
|
+
### 1. `setAuth`
|
|
304
|
+
|
|
305
|
+
Saves your DeepL Auth Key as an environment variable in a .env file for future use.
|
|
306
|
+
|
|
307
|
+
#### Syntax
|
|
308
|
+
|
|
309
|
+
```
|
|
310
|
+
i18n-deepl setAuth <authKey>
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
#### Example
|
|
314
|
+
|
|
315
|
+
```bash
|
|
316
|
+
i18n-deepl setAuth abcdefghijklmnopqrstuvwxyz123456
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
#### Output
|
|
320
|
+
|
|
321
|
+
```
|
|
322
|
+
Success. DeepL Auth Key is now set.
|
|
240
323
|
```
|
|
241
324
|
|
|
242
|
-
|
|
325
|
+
### 2. `languages`
|
|
326
|
+
|
|
327
|
+
#### Description
|
|
328
|
+
|
|
329
|
+
Lists all languages supported by the DeepL API.
|
|
330
|
+
|
|
331
|
+
#### Syntax
|
|
332
|
+
|
|
333
|
+
```
|
|
334
|
+
i18n-deepl languages [--auth-key <authKey>]
|
|
335
|
+
```
|
|
243
336
|
|
|
337
|
+
#### Options
|
|
244
338
|
|
|
245
|
-
|
|
339
|
+
```
|
|
340
|
+
--auth-key <authKey>: (Optional) Provide the DeepL Auth Key directly. If not provided, the tool uses the key from the .env file.
|
|
341
|
+
```
|
|
246
342
|
|
|
247
|
-
|
|
343
|
+
#### Example
|
|
248
344
|
|
|
249
345
|
```bash
|
|
250
|
-
|
|
346
|
+
i18n-deepl languages
|
|
251
347
|
```
|
|
252
348
|
|
|
253
|
-
|
|
349
|
+
#### Output
|
|
350
|
+
|
|
351
|
+
```
|
|
352
|
+
DeepL’s Supported Languages:
|
|
353
|
+
EN - English
|
|
354
|
+
DE - German
|
|
355
|
+
FR - French
|
|
356
|
+
...
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
### 3. `translate`
|
|
360
|
+
|
|
361
|
+
Translates the contents of a JSON file into the specified target language and saves the output in a new JSON file.
|
|
362
|
+
|
|
363
|
+
#### Syntax
|
|
364
|
+
|
|
365
|
+
```
|
|
366
|
+
i18n-deepl translate <source> <target> <targetLang> [options]
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
#### Arguments
|
|
370
|
+
|
|
371
|
+
+ `<source>`: Path to the source JSON file (e.g., ./translations.json).
|
|
372
|
+
+ `<target>`: Path where the translated JSON file will be saved.
|
|
373
|
+
+ `<targetLang>`: Target language code (e.g., fr for French, es for Spanish).
|
|
374
|
+
|
|
375
|
+
#### Options
|
|
376
|
+
|
|
377
|
+
+ `--auth-key, -ak <authKey>`: (Optional) Provide the DeepL Auth Key directly.
|
|
378
|
+
+ `--source-lang, -sl <sourceLang>`: (Optional) Specify the source language (e.g., en for English). Defaults to auto-detection.
|
|
379
|
+
+ `--source-nested, -sn <sourceNested>`: (Optional) Specify a nested key within the JSON for translation (e.g., data.translations).
|
|
380
|
+
+ `--log, -l`: (Optional) Log the translation process to the console.
|
|
381
|
+
+ `--dryRun, -dr`: (Optional) Perform a dry run, logging the process without modifying or saving data.
|
|
382
|
+
+ `--options, -o <options>`: (Optional) Pass additional DeepL API options as an object (e.g., --options="{formality: 'less'}").
|
|
383
|
+
|
|
384
|
+
#### Example
|
|
254
385
|
|
|
255
386
|
```bash
|
|
256
|
-
|
|
257
|
-
|
|
387
|
+
i18n-deepl translate ./source.json ./output.json fr --source-lang en --log
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
#### Output
|
|
392
|
+
|
|
393
|
+
```
|
|
394
|
+
Translation complete. See ./output.json for your results.
|
|
258
395
|
```
|
|
259
396
|
|
|
260
397
|
|
|
261
398
|
## Run tests
|
|
262
399
|
|
|
263
400
|
```bash
|
|
264
|
-
|
|
401
|
+
npm run test
|
|
265
402
|
```
|
|
266
403
|
|
|
267
|
-
|
|
268
404
|
## License
|
|
269
405
|
|
|
270
|
-
Copyright (c) 2022 Florian Walzel,
|
|
271
|
-
MIT License
|
|
406
|
+
Copyright (c) 2022-24 Florian Walzel,
|
|
407
|
+
MIT License
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# handlebars-i18n-cli Programmatical Use
|
|
2
|
+
|
|
3
|
+
You can use the functions of `handlebars-i18n-cli` in a programatical way, i.e. to make them part of your continuous integration.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Specification for Function _i18nCollect_
|
|
8
|
+
|
|
9
|
+
#### Function Signature
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
export function i18nCollect(
|
|
13
|
+
source: string,
|
|
14
|
+
target: string,
|
|
15
|
+
options?: opts
|
|
16
|
+
): Promise<true | void>;
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
#### Description
|
|
20
|
+
|
|
21
|
+
This function scans the source location for internationalization keys, processes them according to the provided options,
|
|
22
|
+
and writes them to the specified target location. It supports features like sorting keys alphabetically, updating
|
|
23
|
+
existing keys, and generating separate files for each language.
|
|
24
|
+
|
|
25
|
+
#### Parameters
|
|
26
|
+
|
|
27
|
+
**Positional Arguments**
|
|
28
|
+
|
|
29
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
30
|
+
|-----------|---------------|-----------------------------------------|---------------------------|
|
|
31
|
+
| `source` | `string` | Path to the source files. | `./src/template.html` |
|
|
32
|
+
| `target` | `string` | Path to the target files or directory. | `./src/translations.json` |
|
|
33
|
+
|
|
34
|
+
**Optional `options` Object Properties**
|
|
35
|
+
|
|
36
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
37
|
+
|--------------------|-----------------|-------------------------------------------------------------------|-----------------------|
|
|
38
|
+
| `alphabetical` | `boolean` | If `true`, sorts keys alphabetically. | `true` |
|
|
39
|
+
| `dryRun` | `boolean` | If `true`, performs a simulation without modifying files. | `false` |
|
|
40
|
+
| `lng` | `Array<string>`| List of language codes to process. | `["en", "de", "fr"]` |
|
|
41
|
+
| `log` | `boolean` | If `true`, enables detailed logging to the console. | `true` |
|
|
42
|
+
| `separateLngFiles` | `boolean` | If `true`, creates separate files for each language. | `true` |
|
|
43
|
+
| `translFunc` | `string` | Specifies a custom translation function name to look for in code. | `"t"` |
|
|
44
|
+
| `update` | `boolean` | If `true`, updates existing translation keys in the target file. | `true` |
|
|
45
|
+
|
|
46
|
+
#### Returns
|
|
47
|
+
|
|
48
|
+
Returns a Promise that resolves to: `true`: Indicates successful completion of the operation.
|
|
49
|
+
`void`: Indicates the function performed no action.
|
|
50
|
+
|
|
51
|
+
**Basic Usage**
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
import {i18nCollect} from 'handlebars-i18n-cli';
|
|
55
|
+
await i18nCollect('./src/template.html', './src/translations.json');
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**With Parameters**
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
await i18nCollect('./src/template.html', './src/translations.json', {
|
|
62
|
+
alphabetical: true,
|
|
63
|
+
dryRun: false,
|
|
64
|
+
lng: ['en', 'de', 'fr'],
|
|
65
|
+
log: true,
|
|
66
|
+
separateLngFiles: true,
|
|
67
|
+
translFunc: 't',
|
|
68
|
+
update: true,
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Specification for DeepL Utility Functions
|
|
74
|
+
|
|
75
|
+
This module provides a set of functions to interact with the DeepL API for authentication, language retrieval, text translation, and JSON file translation.
|
|
76
|
+
|
|
77
|
+
### Function `setAuthKey`
|
|
78
|
+
|
|
79
|
+
Writes the DeepL authentication key to a .env file.
|
|
80
|
+
|
|
81
|
+
#### Function Signature
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
export function setAuthKey(key: string, path?: string): Promise<boolean>;
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Parameters
|
|
88
|
+
|
|
89
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
90
|
+
|-------------------|-----------|--------------------------------------------------------|-----------------|
|
|
91
|
+
| `key` | `string` | Your DeepL API authentication key. | `"abcd1234xyz"` |
|
|
92
|
+
| `path` (optional) | `string` | The path where the `.env` file should go. Default: `./` | `"abcd1234xyz"` |
|
|
93
|
+
|
|
94
|
+
#### Returns
|
|
95
|
+
|
|
96
|
+
`Promise<boolean>`: Resolves to `true` if the key was successfully written; otherwise, `false`.
|
|
97
|
+
|
|
98
|
+
```javascript
|
|
99
|
+
import {setAuthKey} from 'handlebars-i18n-cli';
|
|
100
|
+
await setAuthKey('abcd1234xyz');
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Fetch the authKey with `const key = process.env.DEEPL_AUTH;`.
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
### Function `getSupportedLanguages`
|
|
107
|
+
|
|
108
|
+
Fetches the list of languages supported by the DeepL API.
|
|
109
|
+
|
|
110
|
+
#### Function Signature
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
export function getSupportedLanguages(authKey: string): Promise<any>;
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### Parameters
|
|
117
|
+
|
|
118
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
119
|
+
|------------|-----------|--------------------------------------------------|-----------------|
|
|
120
|
+
| `authKey` | `string` | Your DeepL API authentication key. | `"abcd1234xyz"` |
|
|
121
|
+
|
|
122
|
+
#### Returns
|
|
123
|
+
|
|
124
|
+
`Promise<any>`: Resolves with an object containing the supported languages.
|
|
125
|
+
|
|
126
|
+
#### Usage Example
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
import {getSupportedLanguages} from 'handlebars-i18n-cli';
|
|
130
|
+
let languages = await getSupportedLanguages('abcd1234xyz');
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Function `translateToJSON`
|
|
134
|
+
|
|
135
|
+
Reads a JSON file, translates its content using the DeepL API, and writes the result as a JSON file.
|
|
136
|
+
|
|
137
|
+
#### Function Signature
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
export function translateToJSON(
|
|
141
|
+
authKey: string,
|
|
142
|
+
JsonSrc: string,
|
|
143
|
+
JsonTarget: string,
|
|
144
|
+
sourceLang: string,
|
|
145
|
+
targetLang: string,
|
|
146
|
+
deeplOpts?: object,
|
|
147
|
+
sourceNested?: string,
|
|
148
|
+
log?: boolean,
|
|
149
|
+
dryRun?: boolean
|
|
150
|
+
): Promise<boolean>;
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
#### Parameters
|
|
155
|
+
|
|
156
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
157
|
+
|-----------------|------------------|------------------------------------------------------------------|------------------------------|
|
|
158
|
+
| `authKey` | `string` | Your DeepL API authentication key. | `"abcd1234xyz"` |
|
|
159
|
+
| `JsonSrc` | `string` | Path to the source JSON file. | `"./source.json"` |
|
|
160
|
+
| `JsonTarget` | `string` | Path to the target JSON file. | `"./target.json"` |
|
|
161
|
+
| `sourceLang` | `string` | The source language code. Use `""` for auto-detection. | `"en"` |
|
|
162
|
+
| `targetLang` | `string` | The target language code. | `"de"` |
|
|
163
|
+
| `deeplOpts` | `object` (optional)| Additional DeepL API options (e.g., formality). | `{formality: "formal"}` |
|
|
164
|
+
| `sourceNested` | `string` (optional)| Nested object key to process within the JSON file. | `"translations"` |
|
|
165
|
+
| `log` | `boolean` (optional)| If `true`, enables logging of the process. | `true` |
|
|
166
|
+
| `dryRun` | `boolean` (optional)| If `true`, performs a simulation without modifying the file. | `false` |
|
|
167
|
+
|
|
168
|
+
#### Returns
|
|
169
|
+
|
|
170
|
+
`Promise<boolean>`: Resolves to `true` if the operation succeeds; otherwise, `false`.
|
|
171
|
+
|
|
172
|
+
#### Usage Example
|
|
173
|
+
|
|
174
|
+
```javascript
|
|
175
|
+
import {translateToJSON} from 'handlebars-i18n-cli';
|
|
176
|
+
|
|
177
|
+
const authKey = "abcd1234xyz"; // Your DeepL API key
|
|
178
|
+
const sourceFile = "./translations/source.json"; // Path to the source JSON file
|
|
179
|
+
const targetFile = "./translations/translated.json"; // Path to the target JSON file
|
|
180
|
+
const sourceLang = "en"; // Source language
|
|
181
|
+
const targetLang = "de"; // Target language
|
|
182
|
+
const deepLOptions = { formality: "formal" }; // Optional DeepL API options
|
|
183
|
+
|
|
184
|
+
const res = await translateToJSON(
|
|
185
|
+
authKey,
|
|
186
|
+
sourceFile,
|
|
187
|
+
targetFile,
|
|
188
|
+
sourceLang,
|
|
189
|
+
targetLang,
|
|
190
|
+
deepLOptions,
|
|
191
|
+
"data.translations", // Key for nested translations, we expect source key "en" to be here
|
|
192
|
+
true, // Enable logging
|
|
193
|
+
false // Not a dry run, so it will modify/create the target file
|
|
194
|
+
);
|
|
195
|
+
```
|