@theia/localization-manager 1.48.1 → 1.48.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/README.md +67 -67
- package/lib/common.d.ts +4 -4
- package/lib/common.js +26 -26
- package/lib/deepl-api.d.ts +26 -26
- package/lib/deepl-api.js +93 -93
- package/lib/index.d.ts +3 -3
- package/lib/index.js +21 -21
- package/lib/localization-extractor.d.ts +13 -13
- package/lib/localization-extractor.js +368 -368
- package/lib/localization-extractor.spec.d.ts +1 -1
- package/lib/localization-extractor.spec.js +138 -138
- package/lib/localization-manager.d.ts +24 -24
- package/lib/localization-manager.js +148 -148
- package/lib/localization-manager.spec.d.ts +1 -1
- package/lib/localization-manager.spec.js +84 -84
- package/package.json +3 -3
- package/src/common.ts +27 -27
- package/src/deepl-api.ts +153 -153
- package/src/index.ts +19 -19
- package/src/localization-extractor.spec.ts +151 -151
- package/src/localization-extractor.ts +422 -422
- package/src/localization-manager.spec.ts +91 -91
- package/src/localization-manager.ts +160 -160
|
@@ -1,85 +1,85 @@
|
|
|
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-only 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
|
-
it('should keep placeholders intact', async () => {
|
|
75
|
-
const input = {
|
|
76
|
-
key: '{1} {0}'
|
|
77
|
-
};
|
|
78
|
-
const target = {};
|
|
79
|
-
await manager.translateLanguage(input, target, 'EN', defaultOptions);
|
|
80
|
-
assert.deepStrictEqual(target, {
|
|
81
|
-
key: '[{1} {0}]'
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
});
|
|
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-only 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
|
+
it('should keep placeholders intact', async () => {
|
|
75
|
+
const input = {
|
|
76
|
+
key: '{1} {0}'
|
|
77
|
+
};
|
|
78
|
+
const target = {};
|
|
79
|
+
await manager.translateLanguage(input, target, 'EN', defaultOptions);
|
|
80
|
+
assert.deepStrictEqual(target, {
|
|
81
|
+
key: '[{1} {0}]'
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
85
|
//# sourceMappingURL=localization-manager.spec.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theia/localization-manager",
|
|
3
|
-
"version": "1.48.
|
|
3
|
+
"version": "1.48.2",
|
|
4
4
|
"description": "Theia localization manager API.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -40,10 +40,10 @@
|
|
|
40
40
|
"typescript": "~4.5.5"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@theia/ext-scripts": "1.48.
|
|
43
|
+
"@theia/ext-scripts": "1.48.2"
|
|
44
44
|
},
|
|
45
45
|
"nyc": {
|
|
46
46
|
"extends": "../../configs/nyc.json"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "0e9583d043dde303de7a0f86f68439659f8827b0"
|
|
49
49
|
}
|
package/src/common.ts
CHANGED
|
@@ -1,27 +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-only 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
|
-
}
|
|
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-only 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
CHANGED
|
@@ -1,153 +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-only 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
|
-
}
|
|
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-only 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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
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-only WITH Classpath-exception-2.0
|
|
15
|
-
// *****************************************************************************
|
|
16
|
-
|
|
17
|
-
export * from './common';
|
|
18
|
-
export * from './localization-extractor';
|
|
19
|
-
export * from './localization-manager';
|
|
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-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
export * from './common';
|
|
18
|
+
export * from './localization-extractor';
|
|
19
|
+
export * from './localization-manager';
|