@remvst/localization 1.0.0 → 1.0.2
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/check.yaml +1 -1
- package/lib/commands/combine-json.js +5 -0
- package/lib/commands/google-translate.js +5 -0
- package/lib/commands/parse-csv.js +5 -0
- package/lib/commands/to-typescript.js +5 -0
- package/lib/model/translation-set.js +3 -2
- package/package.json +6 -6
- package/src/commands/combine-json.ts +2 -0
- package/src/commands/google-translate.ts +2 -0
- package/src/commands/parse-csv.ts +2 -0
- package/src/commands/to-typescript.ts +2 -0
- package/src/index.ts +0 -0
- package/src/model/translation-set.ts +3 -2
- package/test/localization.json +7 -1
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.combineJsonCommand = void 0;
|
|
4
7
|
const fs_1 = require("fs");
|
|
5
8
|
const translation_set_1 = require("../model/translation-set");
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
6
10
|
async function combineJsonCommand(options) {
|
|
7
11
|
const fallbackTranslations = [];
|
|
8
12
|
for (const fallbackPath of options.fallback || []) {
|
|
@@ -25,6 +29,7 @@ async function combineJsonCommand(options) {
|
|
|
25
29
|
}
|
|
26
30
|
return null;
|
|
27
31
|
});
|
|
32
|
+
await fs_1.promises.mkdir(path_1.default.dirname(options.out), { recursive: true });
|
|
28
33
|
await fs_1.promises.writeFile(options.out, JSON.stringify(outTranslations.toJSON(), null, 4));
|
|
29
34
|
}
|
|
30
35
|
exports.combineJsonCommand = combineJsonCommand;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.googleTranslateCommand = void 0;
|
|
4
7
|
const fs_1 = require("fs");
|
|
5
8
|
const translation_set_1 = require("../model/translation-set");
|
|
6
9
|
const google_translate_api_1 = require("@vitalets/google-translate-api");
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
7
11
|
async function googleTranslateCommand(options) {
|
|
8
12
|
const mainJson = JSON.parse(await fs_1.promises.readFile(options.in, 'utf-8'));
|
|
9
13
|
const outTranslations = translation_set_1.TranslationSet.fromJSON(mainJson);
|
|
@@ -15,6 +19,7 @@ async function googleTranslateCommand(options) {
|
|
|
15
19
|
const translationResult = await (0, google_translate_api_1.translate)(translationInFallbackLocale, { from: options.fallbackLocale, to: locale });
|
|
16
20
|
return translationResult?.text;
|
|
17
21
|
});
|
|
22
|
+
await fs_1.promises.mkdir(path_1.default.dirname(options.out), { recursive: true });
|
|
18
23
|
await fs_1.promises.writeFile(options.out, JSON.stringify(outTranslations.toJSON(), null, 4));
|
|
19
24
|
}
|
|
20
25
|
exports.googleTranslateCommand = googleTranslateCommand;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.parseCsvCommand = exports.parseCsv = void 0;
|
|
4
7
|
const fs_1 = require("fs");
|
|
5
8
|
const translation_set_1 = require("../model/translation-set");
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
6
10
|
async function parseCsv(path, languagesLine) {
|
|
7
11
|
const csvContent = await fs_1.promises.readFile(path, 'utf-8');
|
|
8
12
|
const csvLines = csvContent.split('\n');
|
|
@@ -26,6 +30,7 @@ async function parseCsv(path, languagesLine) {
|
|
|
26
30
|
exports.parseCsv = parseCsv;
|
|
27
31
|
async function parseCsvCommand(options) {
|
|
28
32
|
const polyglotTranslations = await parseCsv(options.in, options.languagesLineIndex);
|
|
33
|
+
await fs_1.promises.mkdir(path_1.default.dirname(options.out), { recursive: true });
|
|
29
34
|
await fs_1.promises.writeFile(options.out, JSON.stringify(polyglotTranslations, null, 4));
|
|
30
35
|
}
|
|
31
36
|
exports.parseCsvCommand = parseCsvCommand;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.toTypescriptCommand = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
4
8
|
const translation_set_1 = require("../model/translation-set");
|
|
5
9
|
const fs_1 = require("fs");
|
|
6
10
|
async function toTypescriptCommand(options) {
|
|
7
11
|
const sourceJson = JSON.parse(await fs_1.promises.readFile(options.in, 'utf-8'));
|
|
8
12
|
const outTranslations = translation_set_1.TranslationSet.fromJSON(sourceJson);
|
|
13
|
+
await fs_1.promises.mkdir(path_1.default.dirname(options.out), { recursive: true });
|
|
9
14
|
await fs_1.promises.writeFile(options.out, outTranslations.toTypeScript());
|
|
10
15
|
}
|
|
11
16
|
exports.toTypescriptCommand = toTypescriptCommand;
|
|
@@ -48,11 +48,11 @@ class TranslationSet {
|
|
|
48
48
|
generatedFileContent += `export type LocalizationItem = {[key in Locale]?: string};\n\n`;
|
|
49
49
|
generatedFileContent += 'export const LOCALIZATION: {[key in LocalizedKey]: LocalizationItem} = {\n';
|
|
50
50
|
for (const [key, localizedItem] of this.translations.entries()) {
|
|
51
|
-
generatedFileContent += ` ${key}: {\n`;
|
|
51
|
+
generatedFileContent += ` ${JSON.stringify(key)}: {\n`;
|
|
52
52
|
for (const [locale, translation] of localizedItem.entries()) {
|
|
53
53
|
if (!translation)
|
|
54
54
|
continue;
|
|
55
|
-
generatedFileContent += ` ${locale}: ${JSON.stringify(translation)},\n`;
|
|
55
|
+
generatedFileContent += ` ${JSON.stringify(locale)}: ${JSON.stringify(translation)},\n`;
|
|
56
56
|
}
|
|
57
57
|
generatedFileContent += ` },\n`;
|
|
58
58
|
}
|
|
@@ -63,6 +63,7 @@ class TranslationSet {
|
|
|
63
63
|
generatedFileContent += `}\n\n`;
|
|
64
64
|
generatedFileContent += `export function localize(key: LocalizedKey) {\n`;
|
|
65
65
|
generatedFileContent += ` const localizationItem: LocalizationItem = LOCALIZATION[key];\n`;
|
|
66
|
+
generatedFileContent += ` if (!localizationItem) return key;\n`;
|
|
66
67
|
generatedFileContent += ` return localizationItem[LOCALE] || localizationItem.en;\n`;
|
|
67
68
|
generatedFileContent += `}\n`;
|
|
68
69
|
return generatedFileContent;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remvst/localization",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "rm -rf lib && tsc",
|
|
11
11
|
"prepublishOnly": "npm i && npm run build",
|
|
12
|
-
"test:polyglot": "ts-node src/index.ts
|
|
13
|
-
"test:
|
|
14
|
-
"test:
|
|
15
|
-
"test:to-typescript": "ts-node src/index.ts to-typescript -i testOut/localization-
|
|
16
|
-
"test": "npm run test:polyglot && npm run test:
|
|
12
|
+
"test:polyglot": "ts-node src/index.ts parse-csv -i test/polyglot.csv -o testOut/polyglot.json -l 1",
|
|
13
|
+
"test:combine-json": "ts-node src/index.ts combine-json --main=test/localization.json --out=testOut/localization-combined.json --fallback=testOut/polyglot.json -l en -l fr -l es",
|
|
14
|
+
"test:google-translate": "ts-node src/index.ts google-translate --in=testOut/localization-combined.json --out=testOut/localization-backfilled-google-translate.json -l en -l fr -l es",
|
|
15
|
+
"test:to-typescript": "ts-node src/index.ts to-typescript -i testOut/localization-combined.json -o testOut/localization.ts && tsc testOut/localization.ts",
|
|
16
|
+
"test": "npm run test:polyglot && npm run test:combine-json && npm run test:to-typescript"
|
|
17
17
|
},
|
|
18
18
|
"author": "Rémi Vansteelandt",
|
|
19
19
|
"license": "UNLICENSED",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { promises as fs } from 'fs';
|
|
2
2
|
import { TranslationSet, Key, Locale, LocalizedItem } from '../model/translation-set';
|
|
3
|
+
import path from 'path';
|
|
3
4
|
|
|
4
5
|
export async function combineJsonCommand(options: {
|
|
5
6
|
main: string,
|
|
@@ -35,5 +36,6 @@ export async function combineJsonCommand(options: {
|
|
|
35
36
|
}
|
|
36
37
|
);
|
|
37
38
|
|
|
39
|
+
await fs.mkdir(path.dirname(options.out), { recursive: true });
|
|
38
40
|
await fs.writeFile(options.out, JSON.stringify(outTranslations.toJSON(), null, 4));
|
|
39
41
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { promises as fs } from 'fs';
|
|
2
2
|
import { Key, Locale, LocalizedItem, TranslationSet } from "../model/translation-set";
|
|
3
3
|
import { translate } from '@vitalets/google-translate-api';
|
|
4
|
+
import path from 'path';
|
|
4
5
|
|
|
5
6
|
export async function googleTranslateCommand(options: {
|
|
6
7
|
in: string,
|
|
@@ -28,5 +29,6 @@ export async function googleTranslateCommand(options: {
|
|
|
28
29
|
}
|
|
29
30
|
);
|
|
30
31
|
|
|
32
|
+
await fs.mkdir(path.dirname(options.out), { recursive: true });
|
|
31
33
|
await fs.writeFile(options.out, JSON.stringify(outTranslations.toJSON(), null, 4));
|
|
32
34
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { promises as fs } from 'fs';
|
|
2
2
|
import { TranslationSet } from "../model/translation-set";
|
|
3
|
+
import path from 'path';
|
|
3
4
|
|
|
4
5
|
export async function parseCsv(
|
|
5
6
|
path: string,
|
|
@@ -37,5 +38,6 @@ export async function parseCsvCommand(options: {
|
|
|
37
38
|
languagesLineIndex: number,
|
|
38
39
|
}) {
|
|
39
40
|
const polyglotTranslations = await parseCsv(options.in!, options.languagesLineIndex);
|
|
41
|
+
await fs.mkdir(path.dirname(options.out), { recursive: true });
|
|
40
42
|
await fs.writeFile(options.out!, JSON.stringify(polyglotTranslations, null, 4));
|
|
41
43
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import path from "path";
|
|
1
2
|
import { TranslationSet } from "../model/translation-set";
|
|
2
3
|
import { promises as fs } from 'fs';
|
|
3
4
|
|
|
@@ -8,5 +9,6 @@ export async function toTypescriptCommand(options: {
|
|
|
8
9
|
const sourceJson = JSON.parse(await fs.readFile(options.in, 'utf-8'));
|
|
9
10
|
const outTranslations = TranslationSet.fromJSON(sourceJson);
|
|
10
11
|
|
|
12
|
+
await fs.mkdir(path.dirname(options.out), { recursive: true });
|
|
11
13
|
await fs.writeFile(options.out, outTranslations.toTypeScript());
|
|
12
14
|
}
|
package/src/index.ts
CHANGED
|
File without changes
|
|
@@ -65,11 +65,11 @@ export class TranslationSet {
|
|
|
65
65
|
generatedFileContent += 'export const LOCALIZATION: {[key in LocalizedKey]: LocalizationItem} = {\n';
|
|
66
66
|
|
|
67
67
|
for (const [key, localizedItem] of this.translations.entries()) {
|
|
68
|
-
generatedFileContent += ` ${key}: {\n`;
|
|
68
|
+
generatedFileContent += ` ${JSON.stringify(key)}: {\n`;
|
|
69
69
|
|
|
70
70
|
for (const [locale, translation] of localizedItem.entries()) {
|
|
71
71
|
if (!translation) continue;
|
|
72
|
-
generatedFileContent += ` ${locale}: ${JSON.stringify(translation)},\n`;
|
|
72
|
+
generatedFileContent += ` ${JSON.stringify(locale)}: ${JSON.stringify(translation)},\n`;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
generatedFileContent += ` },\n`;
|
|
@@ -85,6 +85,7 @@ export class TranslationSet {
|
|
|
85
85
|
|
|
86
86
|
generatedFileContent += `export function localize(key: LocalizedKey) {\n`;
|
|
87
87
|
generatedFileContent += ` const localizationItem: LocalizationItem = LOCALIZATION[key];\n`;
|
|
88
|
+
generatedFileContent += ` if (!localizationItem) return key;\n`;
|
|
88
89
|
generatedFileContent += ` return localizationItem[LOCALE] || localizationItem.en;\n`;
|
|
89
90
|
generatedFileContent += `}\n`;
|
|
90
91
|
|
package/test/localization.json
CHANGED
|
@@ -5,5 +5,11 @@
|
|
|
5
5
|
"back": { "en": "Back" },
|
|
6
6
|
"play": { "en": "Play" },
|
|
7
7
|
"time": { "en": "Time" },
|
|
8
|
-
"
|
|
8
|
+
"some-Fancy-Key?": { "en": "Fancy key" },
|
|
9
|
+
"backToMainMenu": { "en": "Back To Main Menu", "fr": "Retour au menu principal" },
|
|
10
|
+
"difficulty": { "en": "Difficulty" },
|
|
11
|
+
"difficultyNormal": { "en": "Normal" },
|
|
12
|
+
"difficultyHard": { "en": "Hard" },
|
|
13
|
+
"difficultyEasy": { "en": "Easy" },
|
|
14
|
+
"difficultyVeryEasy": { "en": "Very Easy" }
|
|
9
15
|
}
|