@theia/localization-manager 1.23.0-next.8 → 1.23.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/README.md +39 -2
- package/lib/common.d.ts +5 -0
- package/lib/common.d.ts.map +1 -0
- package/lib/common.js +27 -0
- package/lib/common.js.map +1 -0
- package/lib/deepl-api.d.ts +27 -0
- package/lib/deepl-api.d.ts.map +1 -0
- package/lib/deepl-api.js +94 -0
- package/lib/deepl-api.js.map +1 -0
- package/lib/index.d.ts +2 -15
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +18 -16
- package/lib/index.js.map +1 -1
- package/lib/localization-extractor.d.ts +5 -21
- package/lib/localization-extractor.d.ts.map +1 -1
- package/lib/localization-extractor.js +76 -39
- package/lib/localization-extractor.js.map +1 -1
- package/lib/localization-extractor.spec.d.ts +0 -15
- package/lib/localization-extractor.spec.d.ts.map +1 -1
- package/lib/localization-extractor.spec.js +27 -26
- package/lib/localization-extractor.spec.js.map +1 -1
- package/lib/localization-manager.d.ts +23 -0
- package/lib/localization-manager.d.ts.map +1 -0
- package/lib/localization-manager.js +141 -0
- package/lib/localization-manager.js.map +1 -0
- package/lib/localization-manager.spec.d.ts +2 -0
- package/lib/localization-manager.spec.d.ts.map +1 -0
- package/lib/localization-manager.spec.js +75 -0
- package/lib/localization-manager.spec.js.map +1 -0
- package/package.json +7 -4
- package/src/common.ts +27 -0
- package/src/deepl-api.ts +153 -0
- package/src/index.ts +17 -15
- package/src/localization-extractor.spec.ts +22 -21
- package/src/localization-extractor.ts +80 -44
- package/src/localization-manager.spec.ts +80 -0
- package/src/localization-manager.ts +151 -0
|
@@ -1,33 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
2
|
+
// *****************************************************************************
|
|
3
|
+
// Copyright (C) 2021 TypeFox and others.
|
|
4
|
+
//
|
|
5
|
+
// This program and the accompanying materials are made available under the
|
|
6
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
7
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
8
|
+
//
|
|
9
|
+
// This Source Code may also be made available under the following Secondary
|
|
10
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
11
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
12
|
+
// with the GNU Classpath Exception which is available at
|
|
13
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
|
|
16
|
+
// *****************************************************************************
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
const assert = require("assert");
|
|
19
19
|
const localization_extractor_1 = require("./localization-extractor");
|
|
20
20
|
const TEST_FILE = 'test.ts';
|
|
21
|
+
const quiet = { quiet: true };
|
|
21
22
|
describe('correctly extracts from file content', () => {
|
|
22
23
|
it('should extract from simple nls.localize() call', async () => {
|
|
23
24
|
const content = 'nls.localize("key", "value")';
|
|
24
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content), {
|
|
25
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
25
26
|
'key': 'value'
|
|
26
27
|
});
|
|
27
28
|
});
|
|
28
29
|
it('should extract from nested nls.localize() call', async () => {
|
|
29
30
|
const content = 'nls.localize("nested/key", "value")';
|
|
30
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content), {
|
|
31
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
31
32
|
'nested': {
|
|
32
33
|
'key': 'value'
|
|
33
34
|
}
|
|
@@ -44,7 +45,7 @@ describe('correctly extracts from file content', () => {
|
|
|
44
45
|
label: 'command-label2'
|
|
45
46
|
}, 'command-key');
|
|
46
47
|
`;
|
|
47
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content), {
|
|
48
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
48
49
|
'command-id1': 'command-label1',
|
|
49
50
|
'command-key': 'command-label2'
|
|
50
51
|
});
|
|
@@ -56,7 +57,7 @@ describe('correctly extracts from file content', () => {
|
|
|
56
57
|
label: 'label',
|
|
57
58
|
category: 'category'
|
|
58
59
|
}, undefined, 'category-key');`;
|
|
59
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content), {
|
|
60
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
60
61
|
'id': 'label',
|
|
61
62
|
'category-key': 'category'
|
|
62
63
|
});
|
|
@@ -66,7 +67,7 @@ describe('correctly extracts from file content', () => {
|
|
|
66
67
|
nls.localize('nested/key1', 'value1');
|
|
67
68
|
nls.localize('nested/key2', 'value2');
|
|
68
69
|
`;
|
|
69
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content), {
|
|
70
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
70
71
|
'nested': {
|
|
71
72
|
'key1': 'value1',
|
|
72
73
|
'key2': 'value2'
|
|
@@ -78,14 +79,14 @@ describe('correctly extracts from file content', () => {
|
|
|
78
79
|
const a = 'key';
|
|
79
80
|
nls.localize(a, 'value');
|
|
80
81
|
`;
|
|
81
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content), {
|
|
82
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
82
83
|
'key': 'value'
|
|
83
84
|
});
|
|
84
85
|
});
|
|
85
86
|
it('should return an error when resolving is not successful', async () => {
|
|
86
87
|
const content = "nls.localize(a, 'value')";
|
|
87
88
|
const errors = [];
|
|
88
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content, errors), {});
|
|
89
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {});
|
|
89
90
|
assert.deepStrictEqual(errors, [
|
|
90
91
|
"test.ts(1,14): Could not resolve reference to 'a'"
|
|
91
92
|
]);
|
|
@@ -93,7 +94,7 @@ describe('correctly extracts from file content', () => {
|
|
|
93
94
|
it('should return an error when resolving from an expression', async () => {
|
|
94
95
|
const content = "nls.localize(test.value, 'value');";
|
|
95
96
|
const errors = [];
|
|
96
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content, errors), {});
|
|
97
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {});
|
|
97
98
|
assert.deepStrictEqual(errors, [
|
|
98
99
|
"test.ts(1,14): 'test.value' is not a string constant"
|
|
99
100
|
]);
|
|
@@ -104,7 +105,7 @@ describe('correctly extracts from file content', () => {
|
|
|
104
105
|
nls.localize('key/nested', 'value');
|
|
105
106
|
`.trim();
|
|
106
107
|
const errors = [];
|
|
107
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content, errors), {
|
|
108
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {
|
|
108
109
|
'key': 'value'
|
|
109
110
|
});
|
|
110
111
|
assert.deepStrictEqual(errors, [
|
|
@@ -117,7 +118,7 @@ describe('correctly extracts from file content', () => {
|
|
|
117
118
|
nls.localize('key', 'value');
|
|
118
119
|
`.trim();
|
|
119
120
|
const errors = [];
|
|
120
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content, errors), {
|
|
121
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {
|
|
121
122
|
'key': {
|
|
122
123
|
'nested': 'value'
|
|
123
124
|
}
|
|
@@ -129,7 +130,7 @@ describe('correctly extracts from file content', () => {
|
|
|
129
130
|
it('should show error for template literals', async () => {
|
|
130
131
|
const content = 'nls.localize("key", `template literal value`)';
|
|
131
132
|
const errors = [];
|
|
132
|
-
assert.deepStrictEqual(await localization_extractor_1.extractFromFile(TEST_FILE, content, errors), {});
|
|
133
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {});
|
|
133
134
|
assert.deepStrictEqual(errors, [
|
|
134
135
|
"test.ts(1,20): Template literals are not supported for localization. Please use the additional arguments of the 'nls.localize' function to format strings"
|
|
135
136
|
]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localization-extractor.spec.js","sourceRoot":"","sources":["../src/localization-extractor.spec.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"localization-extractor.spec.js","sourceRoot":"","sources":["../src/localization-extractor.spec.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,2EAA2E;AAC3E,gFAAgF;;AAEhF,iCAAiC;AACjC,qEAA8E;AAE9E,MAAM,SAAS,GAAG,SAAS,CAAC;AAC5B,MAAM,KAAK,GAAsB,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAEjD,QAAQ,CAAC,sCAAsC,EAAE,GAAG,EAAE;IAElD,EAAE,CAAC,gDAAgD,EAAE,KAAK,IAAI,EAAE;QAC5D,MAAM,OAAO,GAAG,8BAA8B,CAAC;QAC/C,MAAM,CAAC,eAAe,CAAC,MAAM,IAAA,wCAAe,EAAC,SAAS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AC,MAAM,EAAE;YAC3B,qDAAqD;SACxD,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gEAAgE,EAAE,KAAK,IAAI,EAAE;QAC5E,MAAM,OAAO,GAAG;;;SAGf,CAAC,IAAI,EAAE,CAAC;QACT,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,MAAM,CAAC,eAAe,CAAC,MAAM,IAAA,wCAAe,EAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE;YAC7E,KAAK,EAAE;gBACH,QAAQ,EAAE,OAAO;aACpB;SACJ,CAAC,CAAC;QACH,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE;YAC3B,iEAAiE;SACpE,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;QACrD,MAAM,OAAO,GAAG,+CAA+C,CAAC;QAChE,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,MAAM,CAAC,eAAe,CAAC,MAAM,IAAA,wCAAe,EAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QACrF,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE;YAC3B,2JAA2J;SAC9J,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AAEP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Localization } from './common';
|
|
2
|
+
import { deepl, DeeplParameters } from './deepl-api';
|
|
3
|
+
export interface LocalizationOptions {
|
|
4
|
+
freeApi: Boolean;
|
|
5
|
+
authKey: string;
|
|
6
|
+
sourceFile: string;
|
|
7
|
+
sourceLanguage?: string;
|
|
8
|
+
targetLanguages: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare type LocalizationFunction = (parameters: DeeplParameters) => Promise<string[]>;
|
|
11
|
+
export declare class LocalizationManager {
|
|
12
|
+
private localizationFn;
|
|
13
|
+
constructor(localizationFn?: typeof deepl);
|
|
14
|
+
localize(options: LocalizationOptions): Promise<void>;
|
|
15
|
+
protected translationFileName(original: string, language: string): string;
|
|
16
|
+
translateLanguage(source: Localization, target: Localization, targetLanguage: string, options: LocalizationOptions): Promise<void>;
|
|
17
|
+
protected buildLocalizationMap(source: Localization, target: Localization): LocalizationMap;
|
|
18
|
+
}
|
|
19
|
+
export interface LocalizationMap {
|
|
20
|
+
text: string[];
|
|
21
|
+
localize: (index: number, value: string) => void;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=localization-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization-manager.d.ts","sourceRoot":"","sources":["../src/localization-manager.ts"],"names":[],"mappings":"AAoBA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,KAAK,EAAiB,eAAe,EAA2C,MAAM,aAAa,CAAC;AAE7G,MAAM,WAAW,mBAAmB;IAChC,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,EAAE,MAAM,CAAA;IAClB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,eAAe,EAAE,MAAM,EAAE,CAAA;CAC5B;AAED,oBAAY,oBAAoB,GAAG,CAAC,UAAU,EAAE,eAAe,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;AAEtF,qBAAa,mBAAmB;IAEhB,OAAO,CAAC,cAAc;gBAAd,cAAc,eAAQ;IAEpC,QAAQ,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IA2C3D,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAMnE,iBAAiB,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAuBxI,SAAS,CAAC,oBAAoB,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,YAAY,GAAG,eAAe;CAoC9F;AAED,MAAM,WAAW,eAAe;IAC5B,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CACnD"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// *****************************************************************************
|
|
3
|
+
// Copyright (C) 2021 TypeFox and others.
|
|
4
|
+
//
|
|
5
|
+
// This program and the accompanying materials are made available under the
|
|
6
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
7
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
8
|
+
//
|
|
9
|
+
// This Source Code may also be made available under the following Secondary
|
|
10
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
11
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
12
|
+
// with the GNU Classpath Exception which is available at
|
|
13
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
|
|
16
|
+
// *****************************************************************************
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.LocalizationManager = void 0;
|
|
19
|
+
const chalk = require("chalk");
|
|
20
|
+
const fs = require("fs-extra");
|
|
21
|
+
const path = require("path");
|
|
22
|
+
const _1 = require(".");
|
|
23
|
+
const deepl_api_1 = require("./deepl-api");
|
|
24
|
+
class LocalizationManager {
|
|
25
|
+
constructor(localizationFn = deepl_api_1.deepl) {
|
|
26
|
+
this.localizationFn = localizationFn;
|
|
27
|
+
}
|
|
28
|
+
async localize(options) {
|
|
29
|
+
let source = {};
|
|
30
|
+
const cwd = process.env.INIT_CWD || process.cwd();
|
|
31
|
+
const sourceFile = path.resolve(cwd, options.sourceFile);
|
|
32
|
+
try {
|
|
33
|
+
source = await fs.readJson(sourceFile);
|
|
34
|
+
}
|
|
35
|
+
catch (_a) {
|
|
36
|
+
console.log(chalk.red(`Could not read file "${options.sourceFile}"`));
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
const languages = [];
|
|
40
|
+
for (const targetLanguage of options.targetLanguages) {
|
|
41
|
+
if (!(0, deepl_api_1.isSupportedLanguage)(targetLanguage)) {
|
|
42
|
+
console.log(chalk.yellow(`Language "${targetLanguage}" is not supported for automatic localization`));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
languages.push(targetLanguage);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (languages.length !== options.targetLanguages.length) {
|
|
49
|
+
console.log('Supported languages: ' + deepl_api_1.supportedLanguages.join(', '));
|
|
50
|
+
}
|
|
51
|
+
const existingTranslations = new Map();
|
|
52
|
+
for (const targetLanguage of languages) {
|
|
53
|
+
try {
|
|
54
|
+
const targetPath = this.translationFileName(sourceFile, targetLanguage);
|
|
55
|
+
existingTranslations.set(targetLanguage, await fs.readJson(targetPath));
|
|
56
|
+
}
|
|
57
|
+
catch (_b) {
|
|
58
|
+
existingTranslations.set(targetLanguage, {});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
await Promise.all(languages.map(language => this.translateLanguage(source, existingTranslations.get(language), language, options)));
|
|
62
|
+
for (const targetLanguage of languages) {
|
|
63
|
+
const targetPath = this.translationFileName(sourceFile, targetLanguage);
|
|
64
|
+
try {
|
|
65
|
+
const translation = existingTranslations.get(targetLanguage);
|
|
66
|
+
await fs.writeJson(targetPath, (0, _1.sortLocalization)(translation), { spaces: 2 });
|
|
67
|
+
}
|
|
68
|
+
catch (_c) {
|
|
69
|
+
console.error(chalk.red(`Error writing translated file to '${targetPath}'`));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
translationFileName(original, language) {
|
|
74
|
+
const directory = path.dirname(original);
|
|
75
|
+
const fileName = path.basename(original, '.json');
|
|
76
|
+
return path.join(directory, `${fileName}.${language.toLowerCase()}.json`);
|
|
77
|
+
}
|
|
78
|
+
async translateLanguage(source, target, targetLanguage, options) {
|
|
79
|
+
var _a;
|
|
80
|
+
const map = this.buildLocalizationMap(source, target);
|
|
81
|
+
if (map.text.length > 0) {
|
|
82
|
+
try {
|
|
83
|
+
const translationResponse = await this.localizationFn({
|
|
84
|
+
auth_key: options.authKey,
|
|
85
|
+
free_api: options.freeApi,
|
|
86
|
+
target_lang: targetLanguage.toUpperCase(),
|
|
87
|
+
source_lang: (_a = options.sourceLanguage) === null || _a === void 0 ? void 0 : _a.toUpperCase(),
|
|
88
|
+
text: map.text
|
|
89
|
+
});
|
|
90
|
+
translationResponse.translations.forEach(({ text }, i) => {
|
|
91
|
+
map.localize(i, text);
|
|
92
|
+
});
|
|
93
|
+
console.log(chalk.green(`Successfully translated ${map.text.length} value${map.text.length > 1 ? 's' : ''} for language "${targetLanguage}"`));
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
console.log(chalk.red(`Could not translate into language "${targetLanguage}"`), e);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
console.log(`No translation necessary for language "${targetLanguage}"`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
buildLocalizationMap(source, target) {
|
|
104
|
+
const functionMap = new Map();
|
|
105
|
+
const text = [];
|
|
106
|
+
const process = (s, t) => {
|
|
107
|
+
// Delete all extra keys in the target translation first
|
|
108
|
+
for (const key of Object.keys(t)) {
|
|
109
|
+
if (!(key in s)) {
|
|
110
|
+
delete t[key];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const [key, value] of Object.entries(s)) {
|
|
114
|
+
if (!(key in t)) {
|
|
115
|
+
if (typeof value === 'string') {
|
|
116
|
+
functionMap.set(text.length, translation => t[key] = translation);
|
|
117
|
+
text.push(value);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
const newLocalization = {};
|
|
121
|
+
t[key] = newLocalization;
|
|
122
|
+
process(value, newLocalization);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else if (typeof value === 'object') {
|
|
126
|
+
if (typeof t[key] === 'string') {
|
|
127
|
+
t[key] = {};
|
|
128
|
+
}
|
|
129
|
+
process(value, t[key]);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
process(source, target);
|
|
134
|
+
return {
|
|
135
|
+
text,
|
|
136
|
+
localize: (index, value) => functionMap.get(index)(value)
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.LocalizationManager = LocalizationManager;
|
|
141
|
+
//# sourceMappingURL=localization-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization-manager.js","sourceRoot":"","sources":["../src/localization-manager.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,2EAA2E;AAC3E,gFAAgF;;;AAEhF,+BAA+B;AAC/B,+BAA+B;AAC/B,6BAA6B;AAC7B,wBAAqC;AAErC,2CAA6G;AAY7G,MAAa,mBAAmB;IAE5B,YAAoB,iBAAiB,iBAAK;QAAtB,mBAAc,GAAd,cAAc,CAAQ;IAAI,CAAC;IAE/C,KAAK,CAAC,QAAQ,CAAC,OAA4B;QACvC,IAAI,MAAM,GAAiB,EAAE,CAAC;QAC9B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;QAClD,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC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+
// *****************************************************************************
|
|
3
|
+
// Copyright (C) 2021 TypeFox and others.
|
|
4
|
+
//
|
|
5
|
+
// This program and the accompanying materials are made available under the
|
|
6
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
7
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
8
|
+
//
|
|
9
|
+
// This Source Code may also be made available under the following Secondary
|
|
10
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
11
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
12
|
+
// with the GNU Classpath Exception which is available at
|
|
13
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
14
|
+
//
|
|
15
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
|
|
16
|
+
// *****************************************************************************
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
const assert = require("assert");
|
|
19
|
+
const localization_manager_1 = require("./localization-manager");
|
|
20
|
+
describe('localization-manager#translateLanguage', () => {
|
|
21
|
+
async function mockLocalization(parameters) {
|
|
22
|
+
return {
|
|
23
|
+
translations: parameters.text.map(value => ({
|
|
24
|
+
detected_source_language: '',
|
|
25
|
+
text: `[${value}]`
|
|
26
|
+
}))
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const manager = new localization_manager_1.LocalizationManager(mockLocalization);
|
|
30
|
+
const defaultOptions = {
|
|
31
|
+
authKey: '',
|
|
32
|
+
freeApi: false,
|
|
33
|
+
sourceFile: '',
|
|
34
|
+
targetLanguages: ['EN']
|
|
35
|
+
};
|
|
36
|
+
it('should translate a single value', async () => {
|
|
37
|
+
const input = {
|
|
38
|
+
key: 'value'
|
|
39
|
+
};
|
|
40
|
+
const target = {};
|
|
41
|
+
await manager.translateLanguage(input, target, 'EN', defaultOptions);
|
|
42
|
+
assert.deepStrictEqual(target, {
|
|
43
|
+
key: '[value]'
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
it('should translate nested values', async () => {
|
|
47
|
+
const input = {
|
|
48
|
+
a: {
|
|
49
|
+
b: 'b'
|
|
50
|
+
},
|
|
51
|
+
c: 'c'
|
|
52
|
+
};
|
|
53
|
+
const target = {};
|
|
54
|
+
await manager.translateLanguage(input, target, 'EN', defaultOptions);
|
|
55
|
+
assert.deepStrictEqual(target, {
|
|
56
|
+
a: {
|
|
57
|
+
b: '[b]'
|
|
58
|
+
},
|
|
59
|
+
c: '[c]'
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it('should not override existing targets', async () => {
|
|
63
|
+
const input = {
|
|
64
|
+
a: 'a'
|
|
65
|
+
};
|
|
66
|
+
const target = {
|
|
67
|
+
a: 'b'
|
|
68
|
+
};
|
|
69
|
+
await manager.translateLanguage(input, target, 'EN', defaultOptions);
|
|
70
|
+
assert.deepStrictEqual(target, {
|
|
71
|
+
a: 'b'
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
//# sourceMappingURL=localization-manager.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization-manager.spec.js","sourceRoot":"","sources":["../src/localization-manager.spec.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,2EAA2E;AAC3E,gFAAgF;;AAEhF,iCAAiC;AAEjC,iEAAkF;AAElF,QAAQ,CAAC,wCAAwC,EAAE,GAAG,EAAE;IAEpD,KAAK,UAAU,gBAAgB,CAAC,UAA2B;QACvD,OAAO;YACH,YAAY,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;gBACxC,wBAAwB,EAAE,EAAE;gBAC5B,IAAI,EAAE,IAAI,KAAK,GAAG;a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AK,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC;QACrE,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE;YAC3B,CAAC,EAAE,GAAG;SACT,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theia/localization-manager",
|
|
3
|
-
"version": "1.23.0
|
|
3
|
+
"version": "1.23.0",
|
|
4
4
|
"description": "Theia localization manager API.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -29,17 +29,20 @@
|
|
|
29
29
|
"watch": "theiaext watch"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
+
"@types/bent": "^7.0.1",
|
|
32
33
|
"@types/fs-extra": "^4.0.2",
|
|
34
|
+
"bent": "^7.1.0",
|
|
35
|
+
"chalk": "4.0.0",
|
|
33
36
|
"deepmerge": "^4.2.2",
|
|
34
37
|
"fs-extra": "^4.0.2",
|
|
35
38
|
"glob": "^7.2.0",
|
|
36
|
-
"typescript": "
|
|
39
|
+
"typescript": "~4.5.5"
|
|
37
40
|
},
|
|
38
41
|
"devDependencies": {
|
|
39
|
-
"@theia/ext-scripts": "1.
|
|
42
|
+
"@theia/ext-scripts": "1.23.0"
|
|
40
43
|
},
|
|
41
44
|
"nyc": {
|
|
42
45
|
"extends": "../../configs/nyc.json"
|
|
43
46
|
},
|
|
44
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "58c402c326f161778b59130578a37240fd6dc863"
|
|
45
48
|
}
|
package/src/common.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2021 TypeFox and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
export interface Localization {
|
|
18
|
+
[key: string]: string | Localization
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function sortLocalization(localization: Localization): Localization {
|
|
22
|
+
return Object.keys(localization).sort().reduce((result: Localization, key: string) => {
|
|
23
|
+
const value = localization[key];
|
|
24
|
+
result[key] = typeof value === 'string' ? value : sortLocalization(value);
|
|
25
|
+
return result;
|
|
26
|
+
}, {});
|
|
27
|
+
}
|
package/src/deepl-api.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2021 TypeFox and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import * as bent from 'bent';
|
|
18
|
+
|
|
19
|
+
const post = bent('POST', 'json', 200);
|
|
20
|
+
// 50 is the maximum amount of translations per request
|
|
21
|
+
const deeplLimit = 50;
|
|
22
|
+
|
|
23
|
+
export async function deepl(
|
|
24
|
+
parameters: DeeplParameters
|
|
25
|
+
): Promise<DeeplResponse> {
|
|
26
|
+
coerceLanguage(parameters);
|
|
27
|
+
const sub_domain = parameters.free_api ? 'api-free' : 'api';
|
|
28
|
+
const textChunks: string[][] = [];
|
|
29
|
+
const textArray = [...parameters.text];
|
|
30
|
+
while (textArray.length > 0) {
|
|
31
|
+
textChunks.push(textArray.splice(0, deeplLimit));
|
|
32
|
+
}
|
|
33
|
+
const responses: DeeplResponse[] = await Promise.all(textChunks.map(chunk => {
|
|
34
|
+
const parameterCopy: DeeplParameters = { ...parameters, text: chunk };
|
|
35
|
+
return post(`https://${sub_domain}.deepl.com/v2/translate`, Buffer.from(toFormData(parameterCopy)), {
|
|
36
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
37
|
+
'User-Agent': 'Theia-Localization-Manager'
|
|
38
|
+
});
|
|
39
|
+
}));
|
|
40
|
+
const mergedResponse: DeeplResponse = { translations: [] };
|
|
41
|
+
for (const response of responses) {
|
|
42
|
+
mergedResponse.translations.push(...response.translations);
|
|
43
|
+
}
|
|
44
|
+
for (const translation of mergedResponse.translations) {
|
|
45
|
+
translation.text = coerceTranslation(translation.text);
|
|
46
|
+
}
|
|
47
|
+
return mergedResponse;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Coerces the target language into a form expected by Deepl.
|
|
52
|
+
*
|
|
53
|
+
* Currently only replaces `ZH-CN` with `ZH`
|
|
54
|
+
*/
|
|
55
|
+
function coerceLanguage(parameters: DeeplParameters): void {
|
|
56
|
+
if (parameters.target_lang === 'ZH-CN') {
|
|
57
|
+
parameters.target_lang = 'ZH';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Coerces translated text into a form expected by VSCode/Theia.
|
|
63
|
+
*
|
|
64
|
+
* Replaces certain full-width characters with their ascii counter-part.
|
|
65
|
+
*/
|
|
66
|
+
function coerceTranslation(text: string): string {
|
|
67
|
+
return text
|
|
68
|
+
.replace(/\uff08/g, '(')
|
|
69
|
+
.replace(/\uff09/g, ')')
|
|
70
|
+
.replace(/\uff0c/g, ',')
|
|
71
|
+
.replace(/\uff1a/g, ':')
|
|
72
|
+
.replace(/\uff1b/g, ';')
|
|
73
|
+
.replace(/\uff1f/g, '?');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function toFormData(parameters: DeeplParameters): string {
|
|
77
|
+
const str: string[] = [];
|
|
78
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
79
|
+
if (typeof value === 'string') {
|
|
80
|
+
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(value.toString()));
|
|
81
|
+
} else if (Array.isArray(value)) {
|
|
82
|
+
for (const item of value) {
|
|
83
|
+
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(item.toString()));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return str.join('&');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type DeeplLanguage =
|
|
91
|
+
| 'BG'
|
|
92
|
+
| 'CS'
|
|
93
|
+
| 'DA'
|
|
94
|
+
| 'DE'
|
|
95
|
+
| 'EL'
|
|
96
|
+
| 'EN-GB'
|
|
97
|
+
| 'EN-US'
|
|
98
|
+
| 'EN'
|
|
99
|
+
| 'ES'
|
|
100
|
+
| 'ET'
|
|
101
|
+
| 'FI'
|
|
102
|
+
| 'FR'
|
|
103
|
+
| 'HU'
|
|
104
|
+
| 'IT'
|
|
105
|
+
| 'JA'
|
|
106
|
+
| 'LT'
|
|
107
|
+
| 'LV'
|
|
108
|
+
| 'NL'
|
|
109
|
+
| 'PL'
|
|
110
|
+
| 'PT-PT'
|
|
111
|
+
| 'PT-BR'
|
|
112
|
+
| 'PT'
|
|
113
|
+
| 'RO'
|
|
114
|
+
| 'RU'
|
|
115
|
+
| 'SK'
|
|
116
|
+
| 'SL'
|
|
117
|
+
| 'SV'
|
|
118
|
+
| 'ZH-CN'
|
|
119
|
+
| 'ZH';
|
|
120
|
+
|
|
121
|
+
export const supportedLanguages = [
|
|
122
|
+
'BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'IT',
|
|
123
|
+
'JA', 'LT', 'LV', 'NL', 'PL', 'PT-PT', 'PT-BR', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'ZH-CN'
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
export function isSupportedLanguage(language: string): language is DeeplLanguage {
|
|
127
|
+
return supportedLanguages.includes(language.toUpperCase());
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface DeeplParameters {
|
|
131
|
+
free_api: Boolean
|
|
132
|
+
auth_key: string
|
|
133
|
+
text: string[]
|
|
134
|
+
source_lang?: DeeplLanguage
|
|
135
|
+
target_lang: DeeplLanguage
|
|
136
|
+
split_sentences?: '0' | '1' | 'nonewlines'
|
|
137
|
+
preserve_formatting?: '0' | '1'
|
|
138
|
+
formality?: 'default' | 'more' | 'less'
|
|
139
|
+
tag_handling?: string[]
|
|
140
|
+
non_splitting_tags?: string[]
|
|
141
|
+
outline_detection?: string
|
|
142
|
+
splitting_tags?: string[]
|
|
143
|
+
ignore_tags?: string[]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface DeeplResponse {
|
|
147
|
+
translations: DeeplTranslation[]
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface DeeplTranslation {
|
|
151
|
+
detected_source_language: string
|
|
152
|
+
text: string
|
|
153
|
+
}
|