handlebars-i18n-cli 1.0.4 → 2.0.0
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 +35 -18
- package/.github/workflows/node.js.yml +2 -2
- package/README.md +384 -52
- package/bin/i18n-collect +36 -2
- package/bin/i18n-deepl +71 -0
- package/de.json +72 -0
- package/en.json +5 -0
- package/fi.json +5 -0
- package/my.json +23 -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
|
-
- uses: actions/checkout@
|
|
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,8 +1,11 @@
|
|
|
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
11
|

|
|
@@ -10,15 +13,20 @@ json files from it. It also helps to keep your translations up to date when chan
|
|
|
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`.
|
|
24
|
+
|
|
25
|
+
<p> </p>
|
|
26
|
+
|
|
27
|
+
## General Use
|
|
20
28
|
|
|
21
|
-
|
|
29
|
+
### 1. Language Key Extraction `i18n-collect`
|
|
22
30
|
|
|
23
31
|
Abstract syntax is:
|
|
24
32
|
|
|
@@ -26,10 +34,11 @@ Abstract syntax is:
|
|
|
26
34
|
i18n-collect <source> <target> <options...>
|
|
27
35
|
```
|
|
28
36
|
|
|
29
|
-
This will generate a file `translations.json` holding the translations for `de`, `fr`, and `en` by extracting all key
|
|
37
|
+
This will generate a file `translations.json` holding the translations for `de`, `fr`, and `en` by extracting all key
|
|
38
|
+
names intended for i18next translation from all html files in your project:
|
|
30
39
|
|
|
31
|
-
```
|
|
32
|
-
|
|
40
|
+
```shell
|
|
41
|
+
i18n-collect my-project/**/*.html my-project/translations.json --lng de,en,fr
|
|
33
42
|
```
|
|
34
43
|
|
|
35
44
|
From a very simple template like this …
|
|
@@ -38,16 +47,14 @@ From a very simple template like this …
|
|
|
38
47
|
<!DOCTYPE html>
|
|
39
48
|
<html lang="{{_locale}}">
|
|
40
49
|
<head>
|
|
41
|
-
|
|
50
|
+
<title>{{__ title}}</title>
|
|
42
51
|
</head>
|
|
43
52
|
<body>
|
|
44
|
-
|
|
53
|
+
{{__ body.greeting textvar1="hello" textvar2="world"}}
|
|
45
54
|
</body>
|
|
46
55
|
</html>
|
|
47
56
|
```
|
|
48
57
|
|
|
49
|
-
|
|
50
|
-
|
|
51
58
|
… the generated translations.json would be:
|
|
52
59
|
|
|
53
60
|
```json
|
|
@@ -75,20 +82,56 @@ From a very simple template like this …
|
|
|
75
82
|
}
|
|
76
83
|
```
|
|
77
84
|
|
|
78
|
-
|
|
85
|
+
### 2. Automatic Translation via DeepL API `i18n-deepl`
|
|
86
|
+
|
|
87
|
+
Abstract syntax is:
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
i18n-deepl translate <source> <target> <targetLang> <options...>
|
|
91
|
+
```
|
|
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,27 @@ 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
|
|
|
109
|
-
|
|
110
|
-
## Source and Target
|
|
152
|
+
## Command `i18n-collect`
|
|
111
153
|
|
|
112
154
|
`<source>`
|
|
113
155
|
|
|
114
156
|
* 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
|
|
157
|
+
* i18n-collect is agnostic against the data type of the template(s) you want to extract translations keys from. It works
|
|
158
|
+
with `.html` as well as `.js` files.
|
|
116
159
|
|
|
117
160
|
`<target>`
|
|
118
161
|
|
|
119
|
-
* The output will always be in `.json` format. The file(s) can then be required for your i18next translation
|
|
162
|
+
* The output will always be in `.json` format. The file(s) can then be required for your i18next translation
|
|
163
|
+
as [JSON v2](https://www.i18next.com/misc/json-format#i18next-json-v2)
|
|
120
164
|
|
|
121
165
|
```
|
|
122
166
|
i18next.init({
|
|
@@ -124,12 +168,11 @@ i18next.init({
|
|
|
124
168
|
});
|
|
125
169
|
```
|
|
126
170
|
|
|
127
|
-
|
|
128
|
-
## Usage options
|
|
171
|
+
### Usage options
|
|
129
172
|
|
|
130
173
|
`--alphabetical` or `-a`
|
|
131
174
|
|
|
132
|
-
This will order the keys to the translation strings alphabetically in the generated json file(s). When the flag
|
|
175
|
+
This will order the keys to the translation strings alphabetically in the generated json file(s). When the flag
|
|
133
176
|
--alphabetical is not set the keys appear in order as within the template(s).
|
|
134
177
|
|
|
135
178
|
---
|
|
@@ -147,7 +190,7 @@ value strings contain current language and key name.
|
|
|
147
190
|
|
|
148
191
|
Example:
|
|
149
192
|
|
|
150
|
-
The template
|
|
193
|
+
The template
|
|
151
194
|
|
|
152
195
|
```html
|
|
153
196
|
<h1>{{__ headline userName="Frank"}}</h1>
|
|
@@ -182,9 +225,10 @@ instead of
|
|
|
182
225
|
|
|
183
226
|
---
|
|
184
227
|
|
|
185
|
-
`--lng
|
|
228
|
+
`--lng language1,language2,...languageN`
|
|
186
229
|
|
|
187
|
-
The list of language shortcodes you want to be generated with an own set in the json. Arguments are comma separated (no
|
|
230
|
+
The list of language shortcodes you want to be generated with an own set in the json. Arguments are comma separated (no
|
|
231
|
+
blank space between, no quotation marks around).
|
|
188
232
|
If no language is defined, "en" is the default.
|
|
189
233
|
|
|
190
234
|
---
|
|
@@ -195,15 +239,16 @@ Logs the final result that is written to the json files(s) into the console as w
|
|
|
195
239
|
|
|
196
240
|
---
|
|
197
241
|
|
|
198
|
-
`--separateLngFiles` or `-sf`
|
|
242
|
+
`--separateLngFiles` or `-sf`
|
|
199
243
|
|
|
200
244
|
Write each language in a separate json file instead of a single one.
|
|
201
245
|
|
|
202
246
|
```bash
|
|
203
|
-
|
|
247
|
+
i18n-collect my-project/template.html my-project/translation.json --lng de,en,fr --separateLngFiles
|
|
204
248
|
```
|
|
205
249
|
|
|
206
|
-
Will generate three json files: **translation.de.json**, **translation.en.json**, and **translation.fn.json** each
|
|
250
|
+
Will generate three json files: **translation.de.json**, **translation.en.json**, and **translation.fn.json** each
|
|
251
|
+
holding
|
|
207
252
|
only the translation for their respective language. By default all translations are written to a single json file.
|
|
208
253
|
|
|
209
254
|
---
|
|
@@ -211,7 +256,8 @@ only the translation for their respective language. By default all translations
|
|
|
211
256
|
`--translFunc=yourCustomFunctionName`
|
|
212
257
|
|
|
213
258
|
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
|
|
259
|
+
i18n-collect as well.Say your translation function has the name *t* instead of handlebars-i18n’s *__* (double
|
|
260
|
+
underscore)
|
|
215
261
|
and your template usage would look like
|
|
216
262
|
|
|
217
263
|
```html
|
|
@@ -221,7 +267,7 @@ and your template usage would look like
|
|
|
221
267
|
you can do
|
|
222
268
|
|
|
223
269
|
```bash
|
|
224
|
-
|
|
270
|
+
i18n-collect my-project/template.html my-project/translation.json --translFunc=t
|
|
225
271
|
```
|
|
226
272
|
|
|
227
273
|
--translFunc=t then substitutes the default *__* with a search for t.
|
|
@@ -236,36 +282,322 @@ will be added.
|
|
|
236
282
|
Works also with the option --separateLngFiles:
|
|
237
283
|
|
|
238
284
|
```bash
|
|
239
|
-
|
|
285
|
+
i18n-collect my-project/**/*.html my-project/translation --update --lng de,en,fr --separateLngFiles
|
|
240
286
|
```
|
|
241
287
|
|
|
242
|
-
Leave out the language ending and json file extension and give only the base name for <target>. In this example case
|
|
288
|
+
Leave out the language ending and json file extension and give only the base name for <target>. In this example case
|
|
289
|
+
handlebars-i18n-cli would look for *translation.de.json*, *translation.en.json*, and *translation.en.json* to update
|
|
290
|
+
them. A language file that does not exist yet will be generated.
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
## Commands for `i18n-collect`
|
|
243
294
|
|
|
295
|
+
i18n-deepl is a command-line tool to translate i18next JSON files using the DeepL API. Below is a detailed guide to its
|
|
296
|
+
commands and usage. To use this tool, you need a valid DeepL API key. Set this key using the setAuth command or provide
|
|
297
|
+
it directly via the --auth-key option when running commands.
|
|
244
298
|
|
|
245
|
-
|
|
299
|
+
### 1. `setAuth`
|
|
246
300
|
|
|
247
|
-
|
|
301
|
+
Saves your DeepL Auth Key as an environment variable in a .env file for future use.
|
|
302
|
+
|
|
303
|
+
#### Syntax
|
|
304
|
+
|
|
305
|
+
```
|
|
306
|
+
i18n-deepl setAuth <authKey>
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### Example
|
|
248
310
|
|
|
249
311
|
```bash
|
|
250
|
-
|
|
312
|
+
i18n-deepl setAuth abcdefghijklmnopqrstuvwxyz123456
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
#### Output
|
|
316
|
+
|
|
317
|
+
```
|
|
318
|
+
Success. DeepL Auth Key is now set.
|
|
251
319
|
```
|
|
252
320
|
|
|
253
|
-
|
|
321
|
+
### 2. `languages`
|
|
322
|
+
|
|
323
|
+
#### Description
|
|
324
|
+
|
|
325
|
+
Lists all languages supported by the DeepL API.
|
|
326
|
+
|
|
327
|
+
#### Syntax
|
|
328
|
+
|
|
329
|
+
```
|
|
330
|
+
i18n-deepl languages [--auth-key <authKey>]
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
#### Options
|
|
334
|
+
|
|
335
|
+
```
|
|
336
|
+
--auth-key <authKey>: (Optional) Provide the DeepL Auth Key directly. If not provided, the tool uses the key from the .env file.
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
#### Example
|
|
254
340
|
|
|
255
341
|
```bash
|
|
256
|
-
|
|
257
|
-
sudo npm link
|
|
342
|
+
i18n-deepl languages
|
|
258
343
|
```
|
|
259
344
|
|
|
345
|
+
#### Output
|
|
260
346
|
|
|
261
|
-
|
|
347
|
+
```
|
|
348
|
+
DeepL’s Supported Languages:
|
|
349
|
+
EN - English
|
|
350
|
+
DE - German
|
|
351
|
+
FR - French
|
|
352
|
+
...
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### 3. `translate`
|
|
356
|
+
|
|
357
|
+
Translates the contents of a JSON file into the specified target language and saves the output in a new JSON file.
|
|
358
|
+
|
|
359
|
+
#### Syntax
|
|
360
|
+
|
|
361
|
+
```
|
|
362
|
+
i18n-deepl translate <source> <target> <targetLang> [options]
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
#### Arguments
|
|
366
|
+
|
|
367
|
+
+ `<source>`: Path to the source JSON file (e.g., ./translations.json).
|
|
368
|
+
+ `<target>`: Path where the translated JSON file will be saved.
|
|
369
|
+
+ `<targetLang>`: Target language code (e.g., fr for French, es for Spanish).
|
|
370
|
+
|
|
371
|
+
#### Options
|
|
372
|
+
|
|
373
|
+
+ `--auth-key, -ak <authKey>`: (Optional) Provide the DeepL Auth Key directly.
|
|
374
|
+
+ `--source-lang, -sl <sourceLang>`: (Optional) Specify the source language (e.g., en for English). Defaults to auto-detection.
|
|
375
|
+
+ `--source-nested, -sn <sourceNested>`: (Optional) Specify a nested key within the JSON for translation (e.g., data.translations).
|
|
376
|
+
+ `--log, -l`: (Optional) Log the translation process to the console.
|
|
377
|
+
+ `--dryRun, -dr`: (Optional) Perform a dry run, logging the process without modifying or saving data.
|
|
378
|
+
+ `--options, -o <options>`: (Optional) Pass additional DeepL API options as an object (e.g., --options="{formality: 'less'}").
|
|
379
|
+
|
|
380
|
+
#### Example
|
|
262
381
|
|
|
263
382
|
```bash
|
|
264
|
-
|
|
383
|
+
i18n-deepl translate ./source.json ./output.json fr --source-lang en --log
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
#### Output
|
|
387
|
+
|
|
388
|
+
```
|
|
389
|
+
Translation complete. See ./output.json for your results.
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
<p> </p>
|
|
395
|
+
|
|
396
|
+
## Programmatical Use
|
|
397
|
+
|
|
398
|
+
You can use the functions of `handlebars-i18n-cli` in a programatical way too, and make them part of your continuous integration.
|
|
399
|
+
|
|
400
|
+
---
|
|
401
|
+
|
|
402
|
+
### Specification for `i18nCollect` Function
|
|
403
|
+
|
|
404
|
+
#### Function Signature
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
export function i18nCollect(
|
|
408
|
+
source: string,
|
|
409
|
+
target: string,
|
|
410
|
+
options?: opts
|
|
411
|
+
): Promise<true | void>;
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
#### Description
|
|
415
|
+
|
|
416
|
+
This function scans the source location for internationalization keys, processes them according to the provided options,
|
|
417
|
+
and writes them to the specified target location. It supports features like sorting keys alphabetically, updating
|
|
418
|
+
existing keys, and generating separate files for each language.
|
|
419
|
+
|
|
420
|
+
#### Parameters
|
|
421
|
+
|
|
422
|
+
**Positional Arguments**
|
|
423
|
+
|
|
424
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
425
|
+
|-----------|---------------|-----------------------------------------|---------------------------|
|
|
426
|
+
| `source` | `string` | Path to the source files. | `./src/template.html` |
|
|
427
|
+
| `target` | `string` | Path to the target files or directory. | `./src/translations.json` |
|
|
428
|
+
|
|
429
|
+
**Optional `options` Object Properties**
|
|
430
|
+
|
|
431
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
432
|
+
|--------------------|-----------------|-------------------------------------------------------------------|-----------------------|
|
|
433
|
+
| `alphabetical` | `boolean` | If `true`, sorts keys alphabetically. | `true` |
|
|
434
|
+
| `dryRun` | `boolean` | If `true`, performs a simulation without modifying files. | `false` |
|
|
435
|
+
| `lng` | `Array<string>`| List of language codes to process. | `["en", "de", "fr"]` |
|
|
436
|
+
| `log` | `boolean` | If `true`, enables detailed logging to the console. | `true` |
|
|
437
|
+
| `separateLngFiles` | `boolean` | If `true`, creates separate files for each language. | `true` |
|
|
438
|
+
| `translFunc` | `string` | Specifies a custom translation function name to look for in code. | `"t"` |
|
|
439
|
+
| `update` | `boolean` | If `true`, updates existing translation keys in the target file. | `true` |
|
|
440
|
+
|
|
441
|
+
#### Returns
|
|
442
|
+
|
|
443
|
+
Returns a Promise that resolves to: `true`: Indicates successful completion of the operation.
|
|
444
|
+
`void`: Indicates the function performed no action.
|
|
445
|
+
|
|
446
|
+
**Basic Usage**
|
|
447
|
+
|
|
448
|
+
```javascript
|
|
449
|
+
import {i18nCollect} from 'handlebars-i18n-cli';
|
|
450
|
+
await i18nCollect('./src/template.html', './src/translations.json');
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
**With Parameters**
|
|
454
|
+
|
|
455
|
+
```javascript
|
|
456
|
+
await i18nCollect('./src/template.html', './src/translations.json', {
|
|
457
|
+
alphabetical: true,
|
|
458
|
+
dryRun: false,
|
|
459
|
+
lng: ['en', 'de', 'fr'],
|
|
460
|
+
log: true,
|
|
461
|
+
separateLngFiles: true,
|
|
462
|
+
translFunc: 't',
|
|
463
|
+
update: true,
|
|
464
|
+
});
|
|
465
|
+
```
|
|
466
|
+
---
|
|
467
|
+
|
|
468
|
+
### Specification for DeepL Utility Functions
|
|
469
|
+
|
|
470
|
+
This module provides a set of functions to interact with the DeepL API for authentication, language retrieval, text translation, and JSON file translation.
|
|
471
|
+
|
|
472
|
+
### Function `setAuthKey`
|
|
473
|
+
|
|
474
|
+
Writes the DeepL authentication key to a .env file.
|
|
475
|
+
|
|
476
|
+
#### Function Signature
|
|
477
|
+
|
|
478
|
+
```typescript
|
|
479
|
+
export function setAuthKey(key: string, path?: string): Promise<boolean>;
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
#### Parameters
|
|
483
|
+
|
|
484
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
485
|
+
|-------------------|-----------|--------------------------------------------------------|-----------------|
|
|
486
|
+
| `key` | `string` | Your DeepL API authentication key. | `"abcd1234xyz"` |
|
|
487
|
+
| `path` (optional) | `string` | The path where the `.env` file should go. Default: `./` | `"abcd1234xyz"` |
|
|
488
|
+
|
|
489
|
+
#### Returns
|
|
490
|
+
|
|
491
|
+
`Promise<boolean>`: Resolves to `true` if the key was successfully written; otherwise, `false`.
|
|
492
|
+
|
|
493
|
+
```javascript
|
|
494
|
+
import {setAuthKey} from 'handlebars-i18n-cli';
|
|
495
|
+
await setAuthKey('abcd1234xyz');
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
Fetch the authKey with `const key = process.env.DEEPL_AUTH;`.
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
### Function `getSupportedLanguages`
|
|
502
|
+
|
|
503
|
+
Fetches the list of languages supported by the DeepL API.
|
|
504
|
+
|
|
505
|
+
#### Function Signature
|
|
506
|
+
|
|
507
|
+
```typescript
|
|
508
|
+
export function getSupportedLanguages(authKey: string): Promise<any>;
|
|
265
509
|
```
|
|
266
510
|
|
|
511
|
+
#### Parameters
|
|
512
|
+
|
|
513
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
514
|
+
|------------|-----------|--------------------------------------------------|-----------------|
|
|
515
|
+
| `authKey` | `string` | Your DeepL API authentication key. | `"abcd1234xyz"` |
|
|
516
|
+
|
|
517
|
+
#### Returns
|
|
518
|
+
|
|
519
|
+
`Promise<any>`: Resolves with an object containing the supported languages.
|
|
520
|
+
|
|
521
|
+
#### Usage Example
|
|
522
|
+
|
|
523
|
+
```javascript
|
|
524
|
+
import {getSupportedLanguages} from 'handlebars-i18n-cli';
|
|
525
|
+
let languages = await getSupportedLanguages('abcd1234xyz');
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### Function `translateToJSON`
|
|
529
|
+
|
|
530
|
+
Reads a JSON file, translates its content using the DeepL API, and writes the result as a JSON file.
|
|
531
|
+
|
|
532
|
+
#### Function Signature
|
|
533
|
+
|
|
534
|
+
```typescript
|
|
535
|
+
export function translateToJSON(
|
|
536
|
+
authKey: string,
|
|
537
|
+
JsonSrc: string,
|
|
538
|
+
JsonTarget: string,
|
|
539
|
+
sourceLang: string,
|
|
540
|
+
targetLang: string,
|
|
541
|
+
deeplOpts?: object,
|
|
542
|
+
sourceNested?: string,
|
|
543
|
+
log?: boolean,
|
|
544
|
+
dryRun?: boolean
|
|
545
|
+
): Promise<boolean>;
|
|
546
|
+
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
#### Parameters
|
|
550
|
+
|
|
551
|
+
| **Name** | **Type** | **Description** | **Example** |
|
|
552
|
+
|-----------------|------------------|------------------------------------------------------------------|------------------------------|
|
|
553
|
+
| `authKey` | `string` | Your DeepL API authentication key. | `"abcd1234xyz"` |
|
|
554
|
+
| `JsonSrc` | `string` | Path to the source JSON file. | `"./source.json"` |
|
|
555
|
+
| `JsonTarget` | `string` | Path to the target JSON file. | `"./target.json"` |
|
|
556
|
+
| `sourceLang` | `string` | The source language code. Use `""` for auto-detection. | `"en"` |
|
|
557
|
+
| `targetLang` | `string` | The target language code. | `"de"` |
|
|
558
|
+
| `deeplOpts` | `object` (optional)| Additional DeepL API options (e.g., formality). | `{formality: "formal"}` |
|
|
559
|
+
| `sourceNested` | `string` (optional)| Nested object key to process within the JSON file. | `"translations"` |
|
|
560
|
+
| `log` | `boolean` (optional)| If `true`, enables logging of the process. | `true` |
|
|
561
|
+
| `dryRun` | `boolean` (optional)| If `true`, performs a simulation without modifying the file. | `false` |
|
|
562
|
+
|
|
563
|
+
#### Returns
|
|
564
|
+
|
|
565
|
+
`Promise<boolean>`: Resolves to `true` if the operation succeeds; otherwise, `false`.
|
|
566
|
+
|
|
567
|
+
#### Usage Example
|
|
568
|
+
|
|
569
|
+
```javascript
|
|
570
|
+
import {translateToJSON} from 'handlebars-i18n-cli';
|
|
571
|
+
|
|
572
|
+
const authKey = "abcd1234xyz"; // Your DeepL API key
|
|
573
|
+
const sourceFile = "./translations/source.json"; // Path to the source JSON file
|
|
574
|
+
const targetFile = "./translations/translated.json"; // Path to the target JSON file
|
|
575
|
+
const sourceLang = "en"; // Source language
|
|
576
|
+
const targetLang = "de"; // Target language
|
|
577
|
+
const deepLOptions = { formality: "formal" }; // Optional DeepL API options
|
|
578
|
+
|
|
579
|
+
const res = await translateToJSON(
|
|
580
|
+
authKey,
|
|
581
|
+
sourceFile,
|
|
582
|
+
targetFile,
|
|
583
|
+
sourceLang,
|
|
584
|
+
targetLang,
|
|
585
|
+
deepLOptions,
|
|
586
|
+
"data.translations", // Key for nested translations, we expect source key "en" to be here
|
|
587
|
+
true, // Enable logging
|
|
588
|
+
false // Not a dry run, so it will modify/create the target file
|
|
589
|
+
);
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
---
|
|
593
|
+
|
|
594
|
+
## Run tests
|
|
595
|
+
|
|
596
|
+
```bash
|
|
597
|
+
npm run test
|
|
598
|
+
```
|
|
267
599
|
|
|
268
600
|
## License
|
|
269
601
|
|
|
270
|
-
Copyright (c) 2022 Florian Walzel,
|
|
271
|
-
MIT License
|
|
602
|
+
Copyright (c) 2022-24 Florian Walzel,
|
|
603
|
+
MIT License
|