@lingual/i18n-check 0.1.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.
Files changed (39) hide show
  1. package/.github/workflows/tests.yaml +31 -0
  2. package/LICENSE +21 -0
  3. package/README.md +402 -0
  4. package/jest.config.js +6 -0
  5. package/package.json +28 -0
  6. package/src/bin/index.ts +244 -0
  7. package/src/errorReporters.ts +18 -0
  8. package/src/index.ts +68 -0
  9. package/src/types.ts +9 -0
  10. package/src/utils/findInvalidTranslations.test.ts +78 -0
  11. package/src/utils/findInvalidTranslations.ts +120 -0
  12. package/src/utils/findInvalidi18nTranslations.test.ts +213 -0
  13. package/src/utils/findInvalidi18nTranslations.ts +137 -0
  14. package/src/utils/findMissingKeys.test.ts +60 -0
  15. package/src/utils/findMissingKeys.ts +27 -0
  16. package/src/utils/flattenTranslations.test.ts +32 -0
  17. package/src/utils/flattenTranslations.ts +42 -0
  18. package/src/utils/i18NextParser.test.ts +169 -0
  19. package/src/utils/i18NextParser.ts +149 -0
  20. package/translations/de-de.json +6 -0
  21. package/translations/en-us.json +6 -0
  22. package/translations/flattenExamples/de-de.json +6 -0
  23. package/translations/flattenExamples/en-us.json +18 -0
  24. package/translations/folderExample/de-DE/index.json +7 -0
  25. package/translations/folderExample/en-US/index.json +8 -0
  26. package/translations/i18NextMessageExamples/de-de.json +73 -0
  27. package/translations/i18NextMessageExamples/en-us.json +90 -0
  28. package/translations/largeFileExamples/de-de.json +5272 -0
  29. package/translations/largeFileExamples/en-us.json +5278 -0
  30. package/translations/largeFileExamples/fr-fr.json +871 -0
  31. package/translations/messageExamples/de-de.json +24 -0
  32. package/translations/messageExamples/en-us.json +30 -0
  33. package/translations/multipleFilesFolderExample/de-DE/one.json +7 -0
  34. package/translations/multipleFilesFolderExample/de-DE/three.json +8 -0
  35. package/translations/multipleFilesFolderExample/de-DE/two.json +5 -0
  36. package/translations/multipleFilesFolderExample/en-US/one.json +8 -0
  37. package/translations/multipleFilesFolderExample/en-US/three.json +8 -0
  38. package/translations/multipleFilesFolderExample/en-US/two.json +6 -0
  39. package/tsconfig.json +113 -0
@@ -0,0 +1,31 @@
1
+ name: Test
2
+ on:
3
+ pull_request:
4
+ branches:
5
+ - main
6
+ push:
7
+ branches:
8
+ - main
9
+
10
+ jobs:
11
+ test:
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@master
16
+
17
+ - name: yarn install & test
18
+ run: |
19
+ yarn install
20
+ yarn test
21
+
22
+ test-build:
23
+ runs-on: ubuntu-latest
24
+
25
+ steps:
26
+ - uses: actions/checkout@master
27
+
28
+ - name: yarn install & build
29
+ run: |
30
+ yarn install
31
+ yarn build
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 lingual
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,402 @@
1
+ # Lingual i18n Check
2
+
3
+ ## Installation and Usage
4
+
5
+ Using **yarn**:
6
+
7
+ ```bash
8
+ yarn add --dev @lingual/i18n-check
9
+ ```
10
+
11
+ Using **npm**:
12
+
13
+ ```bash
14
+ npm install --save-dev @lingual/i18n-check
15
+ ```
16
+
17
+ Using **pnpm**:
18
+
19
+ ```bash
20
+ pnpm add --save-dev @lingual/i18n-check
21
+ ```
22
+
23
+ After the installation, `i18n-check` can either be accessed via defining a command in the `package.json` file or directly in the CLI.
24
+
25
+ Update the your `package.json` and add a new command:
26
+
27
+ ```bash
28
+ "scripts": {
29
+ // ...other commands,
30
+ "i18n:check": "i18n-check"
31
+ }
32
+ ```
33
+
34
+ Now you can run the `i18n:check` command directly from the command-line, i.e. `yarn i18n:check`.
35
+
36
+ Alternatively you can also access the library directly:
37
+
38
+ ```bash
39
+ node_modules/.bin/i18n-check
40
+ ```
41
+
42
+ ## Options
43
+
44
+ ### --target
45
+
46
+ With the `-t` or `--target` option you define which folder or multiple folders you want to run the i18n checks against. It is a **required** option. `i18n-check` will try to find all target locale files and compare these files against the defined source file(s).
47
+ Check the [example](#examples) to see how different locale translation files are organised and how they can be addressed.
48
+
49
+ ```bash
50
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json
51
+ ```
52
+
53
+ ### --source
54
+
55
+ With the `-s` or `--source` option you define which file(s) or folder(s) you want to use as the source to compare all target files against. It is a **required** option. `i18n-check` will try to find all target locale files and compare these files against the defined source file(s).
56
+ Check the [example](#examples) to see how different locale translation files are organised and how they can be addressed.
57
+
58
+ ```bash
59
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json
60
+ ```
61
+
62
+ ### --format
63
+
64
+ By default `i18n-check` will validate against any [`icu`](https://github.com/unicode-org/icu) compliant translations.
65
+ Additionally the `i18next` format is supported and can be set via the `-f` or `--format` option.
66
+
67
+ There are i18n libraries that have their own specific format, which might not be based on `icu` and therefore can not be validated against currently. On a side-note: there might be future support for more specific formats.
68
+
69
+ ```bash
70
+ yarn i18n:check -t translations/i18NextMessageExamples -s translations/i18NextMessageExamples/en-us.json -f i18next
71
+ ```
72
+
73
+ ### --check
74
+
75
+ By default the `i18n-check` will perform a validation against any **missing** and/or **invalid** keys. There are situations where only a specific check should run. By using the `-c` or `--check` option you can specify a specific check to run.
76
+
77
+ The available options are `missingKeys`, which will check against any missing keys in the target files and `invalidKeys` will check for invalid keys, where the target translations has a different type then the one defined in the source file.
78
+
79
+ Check for missing keys:
80
+
81
+ ```bash
82
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json -c missingKeys
83
+ ```
84
+
85
+ Check for invalid keys:
86
+
87
+ ```bash
88
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json -c invalidKeys
89
+ ```
90
+
91
+ Check for missing an invalid keys (which is the default):
92
+
93
+ ```bash
94
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json -c missingKeys,invalidKeys
95
+ ```
96
+
97
+ ### --reporter
98
+
99
+ The standard reporting prints out all the missing or invalid keys.
100
+ Using the `-r` or `--reporter` option enables to override the standard error reporting. Passing the `summary` option will print a summary of the missing or invalid keys.
101
+
102
+ ```bash
103
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json -r summary
104
+ ```
105
+
106
+ ### --exclude
107
+
108
+ There are situations where we want to exclude a single or multiple files or a single folder or a group of folders. A typical scenario would be that some keys are missing in a specific folder, as they are being work in progress for example. To exclude this or these files/folders you can use the `-e` or `--exclude` option. It expects a comma separated string of files and/or folders.
109
+
110
+ To exclude a single file:
111
+
112
+ ```bash
113
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json -e translations/messageExamples/fr-fr.json
114
+ ```
115
+
116
+ To exclude multiple files, provide a comma-separated list:
117
+
118
+ ```bash
119
+ yarn i18n:check -t translations/messageExamples -s translations/messageExamples/en-us.json -e translations/messageExamples/fr-fr.json,translations/messageExamples/de-at.json
120
+ ```
121
+
122
+ To exclude a single folder:
123
+
124
+ ```bash
125
+ yarn i18n:check -t translations/folderExamples -s translations/folderExamples/en -e translations/folderExamples/fr/*
126
+ ```
127
+
128
+ Alternatively you can exclude multiple folders by providing a comma-separated list of folders to be excluded:
129
+
130
+ ```bash
131
+ yarn i18n:check -t translations/folderExamples -s translations/folderExamples/en -e translations/folderExamples/fr/*,translations/folderExample/it/*
132
+ ```
133
+
134
+ The `--exclude` option also accepts a mix of files and folders, which follows the same pattern as above and can be defined as a comma-separated list, i.e.
135
+ `-e translations/folderExamples/fr/*,translations/messageExamples/it.json`
136
+
137
+ ## Examples
138
+
139
+ `i18n-check` is able to load and validate against different locale folder structures. Depending on how the locale files are organized, there are different configuration options.
140
+
141
+ #### Single folder
142
+
143
+ If all the locales are organized in a **single folder**:
144
+
145
+ - locales/
146
+ - en-en.json
147
+ - de-de.json
148
+
149
+ Use the `d` or `dir` option to define the directory that should be checked for target files. With the `s` or `source` option you can specify the base/reference file to compare the target files against.
150
+
151
+ ```bash
152
+ yarn i18n:check -t locales -s locales/en-us.json
153
+ ```
154
+
155
+ #### Folder per locale
156
+
157
+ If the locales are **organised as folders** containing a single json file:
158
+
159
+ - locales/
160
+ - en-US/
161
+ - index.json
162
+ - de-DE/
163
+ - index.json
164
+
165
+ Define the `locales` folder as the directory to look for target files.
166
+
167
+ ```bash
168
+ yarn i18n:check -t locales -s locales/en-US/index.json
169
+ ```
170
+
171
+ #### Folder per locale with multiple files
172
+
173
+ If the locales are **organised as folders** containing multiple json files:
174
+
175
+ - locales/
176
+ - en-US/
177
+ - one.json
178
+ - two.json
179
+ - three.json
180
+ - de-DE/
181
+ - one.json
182
+ - two.json
183
+ - three.json
184
+
185
+ Define the `locales` folder as the directory to look for target files and pass `locales/en-US/` as the `source` option. `i18n-check` will try to collect all the files in the provided source directory and compare each one against the corresponding files in the target locales.
186
+
187
+ ```bash
188
+ yarn i18n:check -t locales -s locales/en-US/
189
+ ```
190
+
191
+ #### Multiple folders containing locales
192
+
193
+ If the locales are **organised as folders** containing multiple json files:
194
+
195
+ - dirOne
196
+ - locales/
197
+ - en-US/
198
+ - one.json
199
+ - two.json
200
+ - three.json
201
+ - de-DE/
202
+ - one.json
203
+ - two.json
204
+ - three.json
205
+ - dirTwo
206
+ - locales/
207
+ - en/
208
+ - one.json
209
+ - two.json
210
+ - three.json
211
+ - de/
212
+ - one.json
213
+ - two.json
214
+ - three.json
215
+
216
+ Define the `locales` folder as the directory to look for target files and pass `locales/en-US/` as the `source` option. `i18n-check` will try to collect all the files in the provided source directory and compare each one against the corresponding files in the target locales.
217
+
218
+ ```bash
219
+ yarn i18n:check -t dirOne,dirTwo -s dirOne/en/,dirTwo/de
220
+ ```
221
+
222
+ ## As Github Action
223
+
224
+ We currently do not offer an explicit **Github Action** you can use out of the box, but if you have `i18n-check` already installed, you can define your own **YAML** file. The following example can be starting point that you can adapt to your current setup:
225
+
226
+ ```
227
+ name: i18n Check
228
+ on:
229
+ pull_request:
230
+ branches:
231
+ - main
232
+ push:
233
+ branches:
234
+ - main
235
+
236
+ jobs:
237
+ i18n-check:
238
+ runs-on: ubuntu-latest
239
+
240
+ steps:
241
+ - uses: actions/checkout@master
242
+
243
+ - name: yarn install & build
244
+ run: |
245
+ yarn install
246
+ yarn build
247
+
248
+ - name: yarn i18n-check
249
+ run: |
250
+ yarn i18n-check -t translations/messageExamples -s translations/messageExamples/en-us.json
251
+ ```
252
+
253
+ ## API
254
+
255
+ Aside from using the CLI, `i18n-check` also exposes a set of check functions that can be accessed programmatically.
256
+ Start by importing `i18n-check`:
257
+
258
+ ```ts
259
+ import * as i18nCheck from "@lingual/i18n-check";
260
+ ```
261
+
262
+ ### `i18nCheck.checkTranslations(source, targets [, options])`
263
+
264
+ `checkTranslations` expects the base and comparison or target files and returns an object containing the missing and invalid keys. The optional `options` objects can be provided as a third argument to define the format style via the `format` property, this is useful if you want to validate `i18next` specific translations.
265
+
266
+ ```ts
267
+ import { checkTranslations } from "@lingual/i18n-check";
268
+
269
+ const options = {
270
+ format: "i18next",
271
+ };
272
+
273
+ const { invalidKeys, missingKeys } = checkTranslations(
274
+ source,
275
+ targets,
276
+ options
277
+ );
278
+ ```
279
+
280
+ Additionally the `options` object enables to also define which checks should run via the `checks` property, f.e. if you only want to check for missing or invalid keys only.
281
+
282
+ ```ts
283
+ import { checkTranslations } from "@lingual/i18n-check";
284
+
285
+ const options = {
286
+ format: "icu",
287
+ checks: ["invalidKeys"],
288
+ };
289
+
290
+ const { invalidKeys } = checkTranslations(source, targets, options);
291
+ ```
292
+
293
+ Calling `checkTranslation` will return the following shape:
294
+
295
+ ```ts
296
+ export type CheckResult = Record<string, string[]>;
297
+
298
+ type Result = {
299
+ missingKeys: CheckResult | undefined;
300
+ invalidKeys: CheckResult | undefined;
301
+ };
302
+ ```
303
+
304
+ The result for `missingKeys` as well as `invalidKeys` is an object containing the provided locales and their corresponding affected keys as an array
305
+
306
+ ```ts
307
+ {
308
+ missingKeys:
309
+ {
310
+ "de-de": ["missing_example_key", "some_other_key"],
311
+ "fr-fr": [],
312
+ }
313
+ };
314
+ ```
315
+
316
+ ### `i18nCheck.checkMissingTranslations(source, targets)`
317
+
318
+ `checkMissingTranslations` checks for any missing keys in the target files. All files are compared against the source file.
319
+
320
+ ```ts
321
+ import { checkMissingTranslations } from "@lingual/i18n-check";
322
+
323
+ const result = checkMissingTranslations(source, targets);
324
+
325
+ // {
326
+ // "de-de": ["missing_translation_key", "some_other_missing_translation_key"],
327
+ // "fr-fr": [],
328
+ // };
329
+ ```
330
+
331
+ The result is an object containing the provided locales and their corresponding missing keys as an array.
332
+
333
+ ### `i18nCheck.checkInvalidTranslations(source, targets [, options])`
334
+
335
+ `checkInvalidTranslations` checks if there are any invalid keys in the target files. All files are compared against the source file.
336
+
337
+ ```ts
338
+ import { checkInvalidTranslations } from "@lingual/i18n-check";
339
+
340
+ const options = {
341
+ format: "i18next",
342
+ };
343
+
344
+ const result = checkInvalidTranslations(source, targets, options);
345
+
346
+ // {
347
+ // "de-de": ["invalid_translation_key", "some_other_invalid_translation_key"],
348
+ // "fr-fr": [],
349
+ // };
350
+ ```
351
+
352
+ The result is an object containing the provided locales and their corresponding invalid keys as an array.
353
+
354
+ ## Development
355
+
356
+ If you want to checkout and run the code, you need to run the `build` command first.
357
+
358
+ Run `yarn build`, `pnpm run build` or `npm run build` and then depending on the scenario one of the following commands.
359
+
360
+ Basic icu translation example:
361
+
362
+ ```bash
363
+ node dist/bin/index.js -t translations/messageExamples -s translations/messageExamples/en-us.json
364
+ ```
365
+
366
+ Flatted translation keys example:
367
+
368
+ ```bash
369
+ node dist/bin/index.js -t translations/flattenExamples -s translations/flattenExamples/en-us.json
370
+ ```
371
+
372
+ i18next translation example:
373
+
374
+ ```bash
375
+ node dist/bin/index.js -t translations/i18NextMessageExamples -s translations/i18NextMessageExamples/en-us.json -f i18next
376
+ ```
377
+
378
+ Single file translation example:
379
+
380
+ ```bash
381
+ node dist/bin/index.js -t translations/folderExample -s translations/folderExample/en-US/
382
+ ```
383
+
384
+ Multiple files per folder translation example:
385
+
386
+ ```bash
387
+ node dist/bin/index.js -t translations/multipleFilesFolderExample/ -s translations/multipleFilesFolderExample/en-US/
388
+ ```
389
+
390
+ Multiple folders containing locales translation example:
391
+
392
+ ```bash
393
+ node dist/bin/index.js -t translations/folderExample,translations/messageExamples -s translations/folderExample/en-US/,translations/messageExamples/en-us.json
394
+ ```
395
+
396
+ ## Test
397
+
398
+ To run the tests use the following command:
399
+
400
+ ```bash
401
+ pnpm run test // yarn test or npm test
402
+ ```
package/jest.config.js ADDED
@@ -0,0 +1,6 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ module.exports = {
3
+ preset: "ts-jest",
4
+ testEnvironment: "node",
5
+ testPathIgnorePatterns: ["dist"],
6
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@lingual/i18n-check",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "bin": {
8
+ "i18n-check": "./dist/bin/index.js"
9
+ },
10
+ "types": "./dist/index.d.ts",
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "test": "jest"
14
+ },
15
+ "dependencies": {
16
+ "@formatjs/icu-messageformat-parser": "^2.7.6",
17
+ "chalk": "^4.1.2",
18
+ "commander": "^11.0.0",
19
+ "glob": "^10.3.10",
20
+ "typescript": "^5.2.2"
21
+ },
22
+ "devDependencies": {
23
+ "@types/jest": "^29.5.12",
24
+ "@types/node": "^20.12.12",
25
+ "jest": "^29.7.0",
26
+ "ts-jest": "^29.1.2"
27
+ }
28
+ }