@theia/localization-manager 1.53.0-next.55 → 1.53.0-next.64
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/localization-extractor.spec.js +26 -26
- package/package.json +2 -2
- package/src/common.ts +27 -27
- package/src/deepl-api.ts +168 -168
- package/src/index.ts +19 -19
- package/src/localization-extractor.spec.ts +151 -151
- package/src/localization-extractor.ts +431 -431
- package/src/localization-manager.spec.ts +91 -91
- package/src/localization-manager.ts +168 -168
package/README.md
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
<div align='center'>
|
|
2
|
-
|
|
3
|
-
<br />
|
|
4
|
-
|
|
5
|
-
<img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
|
|
6
|
-
|
|
7
|
-
<h2>ECLIPSE THEIA - LOCALIZATION-MANAGER</h2>
|
|
8
|
-
|
|
9
|
-
<hr />
|
|
10
|
-
|
|
11
|
-
</div>
|
|
12
|
-
|
|
13
|
-
## Description
|
|
14
|
-
|
|
15
|
-
The `@theia/localization-manager` package is used easily create localizations of Theia and Theia extensions for different languages. It has two main use cases.
|
|
16
|
-
|
|
17
|
-
First, it allows to extract localization keys and default values from `nls.localize` calls within the codebase using the `nls-extract` Theia-CLI command. Take this code for example:
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
const hi = nls.localize('greetings/hi', 'Hello');
|
|
21
|
-
const bye = nls.localize('greetings/bye', 'Bye');
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
It will be converted into this JSON file (`nls.json`):
|
|
25
|
-
|
|
26
|
-
```json
|
|
27
|
-
{
|
|
28
|
-
"greetings": {
|
|
29
|
-
"hi": "Hello",
|
|
30
|
-
"bye": "Bye"
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
Afterwards, any manual or automatic translation approach can be used to translate this file into other languages. These JSON files are supposed to be picked up by `LocalizationContribution`s.
|
|
36
|
-
|
|
37
|
-
Additionally, Theia provides a simple way to translate the generated JSON files out of the box using the [DeepL API](https://www.deepl.com/docs-api). For this, a [DeepL free or pro account](https://www.deepl.com/pro) is needed. Using the `nls-localize` command of the Theia-CLI, a target file can be translated into different languages. For example, when calling the command using the previous JSON file with the `fr` (french) language, the following `nls.fr.json` file will be created in the same directory as the translation source:
|
|
38
|
-
|
|
39
|
-
```json
|
|
40
|
-
{
|
|
41
|
-
"greetings": {
|
|
42
|
-
"hi": "Bonjour",
|
|
43
|
-
"bye": "Au revoir"
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
Only JSON entries without corresponding translations are translated using DeepL. This ensures that manual changes to the translated files aren't overwritten and only new translation entries are actually sent to DeepL.
|
|
50
|
-
|
|
51
|
-
Use `theia nls-localize --help` for more information on how to use the command and supply DeepL API keys.
|
|
52
|
-
|
|
53
|
-
For more information, see the [internationalization documentation](https://theia-ide.org/docs/i18n/).
|
|
54
|
-
|
|
55
|
-
## Additional Information
|
|
56
|
-
|
|
57
|
-
- [Theia - GitHub](https://github.com/eclipse-theia/theia)
|
|
58
|
-
- [Theia - Website](https://theia-ide.org/)
|
|
59
|
-
|
|
60
|
-
## License
|
|
61
|
-
|
|
62
|
-
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
|
|
63
|
-
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
|
|
64
|
-
|
|
65
|
-
## Trademark
|
|
66
|
-
"Theia" is a trademark of the Eclipse Foundation
|
|
67
|
-
https://www.eclipse.org/theia
|
|
1
|
+
<div align='center'>
|
|
2
|
+
|
|
3
|
+
<br />
|
|
4
|
+
|
|
5
|
+
<img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
|
|
6
|
+
|
|
7
|
+
<h2>ECLIPSE THEIA - LOCALIZATION-MANAGER</h2>
|
|
8
|
+
|
|
9
|
+
<hr />
|
|
10
|
+
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
## Description
|
|
14
|
+
|
|
15
|
+
The `@theia/localization-manager` package is used easily create localizations of Theia and Theia extensions for different languages. It has two main use cases.
|
|
16
|
+
|
|
17
|
+
First, it allows to extract localization keys and default values from `nls.localize` calls within the codebase using the `nls-extract` Theia-CLI command. Take this code for example:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const hi = nls.localize('greetings/hi', 'Hello');
|
|
21
|
+
const bye = nls.localize('greetings/bye', 'Bye');
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
It will be converted into this JSON file (`nls.json`):
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"greetings": {
|
|
29
|
+
"hi": "Hello",
|
|
30
|
+
"bye": "Bye"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Afterwards, any manual or automatic translation approach can be used to translate this file into other languages. These JSON files are supposed to be picked up by `LocalizationContribution`s.
|
|
36
|
+
|
|
37
|
+
Additionally, Theia provides a simple way to translate the generated JSON files out of the box using the [DeepL API](https://www.deepl.com/docs-api). For this, a [DeepL free or pro account](https://www.deepl.com/pro) is needed. Using the `nls-localize` command of the Theia-CLI, a target file can be translated into different languages. For example, when calling the command using the previous JSON file with the `fr` (french) language, the following `nls.fr.json` file will be created in the same directory as the translation source:
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"greetings": {
|
|
42
|
+
"hi": "Bonjour",
|
|
43
|
+
"bye": "Au revoir"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
Only JSON entries without corresponding translations are translated using DeepL. This ensures that manual changes to the translated files aren't overwritten and only new translation entries are actually sent to DeepL.
|
|
50
|
+
|
|
51
|
+
Use `theia nls-localize --help` for more information on how to use the command and supply DeepL API keys.
|
|
52
|
+
|
|
53
|
+
For more information, see the [internationalization documentation](https://theia-ide.org/docs/i18n/).
|
|
54
|
+
|
|
55
|
+
## Additional Information
|
|
56
|
+
|
|
57
|
+
- [Theia - GitHub](https://github.com/eclipse-theia/theia)
|
|
58
|
+
- [Theia - Website](https://theia-ide.org/)
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
|
|
63
|
+
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
|
|
64
|
+
|
|
65
|
+
## Trademark
|
|
66
|
+
"Theia" is a trademark of the Eclipse Foundation
|
|
67
|
+
https://www.eclipse.org/theia
|
|
@@ -35,15 +35,15 @@ describe('correctly extracts from file content', () => {
|
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
it('should extract IDs from Command.toLocalizedCommand() call', async () => {
|
|
38
|
-
const content = `
|
|
39
|
-
Command.toLocalizedCommand({
|
|
40
|
-
id: 'command-id1',
|
|
41
|
-
label: 'command-label1'
|
|
42
|
-
});
|
|
43
|
-
Command.toLocalizedCommand({
|
|
44
|
-
id: 'command-id2',
|
|
45
|
-
label: 'command-label2'
|
|
46
|
-
}, 'command-key');
|
|
38
|
+
const content = `
|
|
39
|
+
Command.toLocalizedCommand({
|
|
40
|
+
id: 'command-id1',
|
|
41
|
+
label: 'command-label1'
|
|
42
|
+
});
|
|
43
|
+
Command.toLocalizedCommand({
|
|
44
|
+
id: 'command-id2',
|
|
45
|
+
label: 'command-label2'
|
|
46
|
+
}, 'command-key');
|
|
47
47
|
`;
|
|
48
48
|
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
49
49
|
'command-id1': 'command-label1',
|
|
@@ -51,11 +51,11 @@ describe('correctly extracts from file content', () => {
|
|
|
51
51
|
});
|
|
52
52
|
});
|
|
53
53
|
it('should extract category from Command.toLocalizedCommand() call', async () => {
|
|
54
|
-
const content = `
|
|
55
|
-
Command.toLocalizedCommand({
|
|
56
|
-
id: 'id',
|
|
57
|
-
label: 'label',
|
|
58
|
-
category: 'category'
|
|
54
|
+
const content = `
|
|
55
|
+
Command.toLocalizedCommand({
|
|
56
|
+
id: 'id',
|
|
57
|
+
label: 'label',
|
|
58
|
+
category: 'category'
|
|
59
59
|
}, undefined, 'category-key');`;
|
|
60
60
|
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
61
61
|
'id': 'label',
|
|
@@ -63,9 +63,9 @@ describe('correctly extracts from file content', () => {
|
|
|
63
63
|
});
|
|
64
64
|
});
|
|
65
65
|
it('should merge different nls.localize() calls', async () => {
|
|
66
|
-
const content = `
|
|
67
|
-
nls.localize('nested/key1', 'value1');
|
|
68
|
-
nls.localize('nested/key2', 'value2');
|
|
66
|
+
const content = `
|
|
67
|
+
nls.localize('nested/key1', 'value1');
|
|
68
|
+
nls.localize('nested/key2', 'value2');
|
|
69
69
|
`;
|
|
70
70
|
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
71
71
|
'nested': {
|
|
@@ -75,9 +75,9 @@ describe('correctly extracts from file content', () => {
|
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
it('should be able to resolve local references', async () => {
|
|
78
|
-
const content = `
|
|
79
|
-
const a = 'key';
|
|
80
|
-
nls.localize(a, 'value');
|
|
78
|
+
const content = `
|
|
79
|
+
const a = 'key';
|
|
80
|
+
nls.localize(a, 'value');
|
|
81
81
|
`;
|
|
82
82
|
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
83
83
|
'key': 'value'
|
|
@@ -100,9 +100,9 @@ describe('correctly extracts from file content', () => {
|
|
|
100
100
|
]);
|
|
101
101
|
});
|
|
102
102
|
it('should show error when trying to merge an object and a string', async () => {
|
|
103
|
-
const content = `
|
|
104
|
-
nls.localize('key', 'value');
|
|
105
|
-
nls.localize('key/nested', 'value');
|
|
103
|
+
const content = `
|
|
104
|
+
nls.localize('key', 'value');
|
|
105
|
+
nls.localize('key/nested', 'value');
|
|
106
106
|
`.trim();
|
|
107
107
|
const errors = [];
|
|
108
108
|
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {
|
|
@@ -113,9 +113,9 @@ describe('correctly extracts from file content', () => {
|
|
|
113
113
|
]);
|
|
114
114
|
});
|
|
115
115
|
it('should show error when trying to merge a string into an object', async () => {
|
|
116
|
-
const content = `
|
|
117
|
-
nls.localize('key/nested', 'value');
|
|
118
|
-
nls.localize('key', 'value');
|
|
116
|
+
const content = `
|
|
117
|
+
nls.localize('key/nested', 'value');
|
|
118
|
+
nls.localize('key', 'value');
|
|
119
119
|
`.trim();
|
|
120
120
|
const errors = [];
|
|
121
121
|
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theia/localization-manager",
|
|
3
|
-
"version": "1.53.0-next.
|
|
3
|
+
"version": "1.53.0-next.64+23b351d26",
|
|
4
4
|
"description": "Theia localization manager API.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"nyc": {
|
|
46
46
|
"extends": "../../configs/nyc.json"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "23b351d26346a2b5d6aca3ee81fba59c056132f7"
|
|
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,168 +1,168 @@
|
|
|
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-HANS';
|
|
58
|
-
} else if (parameters.target_lang === 'ZH-TW') {
|
|
59
|
-
parameters.target_lang = 'ZH-HANT';
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Coerces translated text into a form expected by VSCode/Theia.
|
|
65
|
-
*
|
|
66
|
-
* Replaces certain full-width characters with their ascii counter-part.
|
|
67
|
-
*/
|
|
68
|
-
function coerceTranslation(text: string): string {
|
|
69
|
-
return text
|
|
70
|
-
.replace(/\uff08/g, '(')
|
|
71
|
-
.replace(/\uff09/g, ')')
|
|
72
|
-
.replace(/\uff0c/g, ',')
|
|
73
|
-
.replace(/\uff1a/g, ':')
|
|
74
|
-
.replace(/\uff1b/g, ';')
|
|
75
|
-
.replace(/\uff1f/g, '?');
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function toFormData(parameters: DeeplParameters): string {
|
|
79
|
-
const str: string[] = [];
|
|
80
|
-
for (const [key, value] of Object.entries(parameters)) {
|
|
81
|
-
if (typeof value === 'string') {
|
|
82
|
-
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(value.toString()));
|
|
83
|
-
} else if (Array.isArray(value)) {
|
|
84
|
-
for (const item of value) {
|
|
85
|
-
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(item.toString()));
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return str.join('&');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export type DeeplLanguage =
|
|
93
|
-
| 'BG'
|
|
94
|
-
| 'CS'
|
|
95
|
-
| 'DA'
|
|
96
|
-
| 'DE'
|
|
97
|
-
| 'EL'
|
|
98
|
-
| 'EN-GB'
|
|
99
|
-
| 'EN-US'
|
|
100
|
-
| 'EN'
|
|
101
|
-
| 'ES'
|
|
102
|
-
| 'ET'
|
|
103
|
-
| 'FI'
|
|
104
|
-
| 'FR'
|
|
105
|
-
| 'HU'
|
|
106
|
-
| 'ID'
|
|
107
|
-
| 'IT'
|
|
108
|
-
| 'JA'
|
|
109
|
-
| 'KO'
|
|
110
|
-
| 'LT'
|
|
111
|
-
| 'LV'
|
|
112
|
-
| 'NB'
|
|
113
|
-
| 'NL'
|
|
114
|
-
| 'PL'
|
|
115
|
-
| 'PT-PT'
|
|
116
|
-
| 'PT-BR'
|
|
117
|
-
| 'PT'
|
|
118
|
-
| 'RO'
|
|
119
|
-
| 'RU'
|
|
120
|
-
| 'SK'
|
|
121
|
-
| 'SL'
|
|
122
|
-
| 'SV'
|
|
123
|
-
| 'TR'
|
|
124
|
-
| 'UK'
|
|
125
|
-
| 'ZH-CN'
|
|
126
|
-
| 'ZH-TW'
|
|
127
|
-
| 'ZH-HANS'
|
|
128
|
-
| 'ZH-HANT'
|
|
129
|
-
| 'ZH';
|
|
130
|
-
|
|
131
|
-
export const supportedLanguages = [
|
|
132
|
-
'BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'ID', 'IT',
|
|
133
|
-
'JA', 'KO', 'LT', 'LV', 'NL', 'PL', 'PT-PT', 'PT-BR', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'TR', 'UK', 'ZH-CN', 'ZH-TW'
|
|
134
|
-
];
|
|
135
|
-
|
|
136
|
-
// From https://code.visualstudio.com/docs/getstarted/locales#_available-locales
|
|
137
|
-
export const defaultLanguages = [
|
|
138
|
-
'ZH-CN', 'ZH-TW', 'FR', 'DE', 'IT', 'ES', 'JA', 'KO', 'RU', 'PT-BR', 'TR', 'PL', 'CS', 'HU'
|
|
139
|
-
] as const;
|
|
140
|
-
|
|
141
|
-
export function isSupportedLanguage(language: string): language is DeeplLanguage {
|
|
142
|
-
return supportedLanguages.includes(language.toUpperCase());
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export interface DeeplParameters {
|
|
146
|
-
free_api: Boolean
|
|
147
|
-
auth_key: string
|
|
148
|
-
text: string[]
|
|
149
|
-
source_lang?: DeeplLanguage
|
|
150
|
-
target_lang: DeeplLanguage
|
|
151
|
-
split_sentences?: '0' | '1' | 'nonewlines'
|
|
152
|
-
preserve_formatting?: '0' | '1'
|
|
153
|
-
formality?: 'default' | 'more' | 'less'
|
|
154
|
-
tag_handling?: string[]
|
|
155
|
-
non_splitting_tags?: string[]
|
|
156
|
-
outline_detection?: string
|
|
157
|
-
splitting_tags?: string[]
|
|
158
|
-
ignore_tags?: string[]
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export interface DeeplResponse {
|
|
162
|
-
translations: DeeplTranslation[]
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export interface DeeplTranslation {
|
|
166
|
-
detected_source_language: string
|
|
167
|
-
text: string
|
|
168
|
-
}
|
|
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-HANS';
|
|
58
|
+
} else if (parameters.target_lang === 'ZH-TW') {
|
|
59
|
+
parameters.target_lang = 'ZH-HANT';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Coerces translated text into a form expected by VSCode/Theia.
|
|
65
|
+
*
|
|
66
|
+
* Replaces certain full-width characters with their ascii counter-part.
|
|
67
|
+
*/
|
|
68
|
+
function coerceTranslation(text: string): string {
|
|
69
|
+
return text
|
|
70
|
+
.replace(/\uff08/g, '(')
|
|
71
|
+
.replace(/\uff09/g, ')')
|
|
72
|
+
.replace(/\uff0c/g, ',')
|
|
73
|
+
.replace(/\uff1a/g, ':')
|
|
74
|
+
.replace(/\uff1b/g, ';')
|
|
75
|
+
.replace(/\uff1f/g, '?');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function toFormData(parameters: DeeplParameters): string {
|
|
79
|
+
const str: string[] = [];
|
|
80
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
81
|
+
if (typeof value === 'string') {
|
|
82
|
+
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(value.toString()));
|
|
83
|
+
} else if (Array.isArray(value)) {
|
|
84
|
+
for (const item of value) {
|
|
85
|
+
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(item.toString()));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return str.join('&');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export type DeeplLanguage =
|
|
93
|
+
| 'BG'
|
|
94
|
+
| 'CS'
|
|
95
|
+
| 'DA'
|
|
96
|
+
| 'DE'
|
|
97
|
+
| 'EL'
|
|
98
|
+
| 'EN-GB'
|
|
99
|
+
| 'EN-US'
|
|
100
|
+
| 'EN'
|
|
101
|
+
| 'ES'
|
|
102
|
+
| 'ET'
|
|
103
|
+
| 'FI'
|
|
104
|
+
| 'FR'
|
|
105
|
+
| 'HU'
|
|
106
|
+
| 'ID'
|
|
107
|
+
| 'IT'
|
|
108
|
+
| 'JA'
|
|
109
|
+
| 'KO'
|
|
110
|
+
| 'LT'
|
|
111
|
+
| 'LV'
|
|
112
|
+
| 'NB'
|
|
113
|
+
| 'NL'
|
|
114
|
+
| 'PL'
|
|
115
|
+
| 'PT-PT'
|
|
116
|
+
| 'PT-BR'
|
|
117
|
+
| 'PT'
|
|
118
|
+
| 'RO'
|
|
119
|
+
| 'RU'
|
|
120
|
+
| 'SK'
|
|
121
|
+
| 'SL'
|
|
122
|
+
| 'SV'
|
|
123
|
+
| 'TR'
|
|
124
|
+
| 'UK'
|
|
125
|
+
| 'ZH-CN'
|
|
126
|
+
| 'ZH-TW'
|
|
127
|
+
| 'ZH-HANS'
|
|
128
|
+
| 'ZH-HANT'
|
|
129
|
+
| 'ZH';
|
|
130
|
+
|
|
131
|
+
export const supportedLanguages = [
|
|
132
|
+
'BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'ID', 'IT',
|
|
133
|
+
'JA', 'KO', 'LT', 'LV', 'NL', 'PL', 'PT-PT', 'PT-BR', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'TR', 'UK', 'ZH-CN', 'ZH-TW'
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
// From https://code.visualstudio.com/docs/getstarted/locales#_available-locales
|
|
137
|
+
export const defaultLanguages = [
|
|
138
|
+
'ZH-CN', 'ZH-TW', 'FR', 'DE', 'IT', 'ES', 'JA', 'KO', 'RU', 'PT-BR', 'TR', 'PL', 'CS', 'HU'
|
|
139
|
+
] as const;
|
|
140
|
+
|
|
141
|
+
export function isSupportedLanguage(language: string): language is DeeplLanguage {
|
|
142
|
+
return supportedLanguages.includes(language.toUpperCase());
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface DeeplParameters {
|
|
146
|
+
free_api: Boolean
|
|
147
|
+
auth_key: string
|
|
148
|
+
text: string[]
|
|
149
|
+
source_lang?: DeeplLanguage
|
|
150
|
+
target_lang: DeeplLanguage
|
|
151
|
+
split_sentences?: '0' | '1' | 'nonewlines'
|
|
152
|
+
preserve_formatting?: '0' | '1'
|
|
153
|
+
formality?: 'default' | 'more' | 'less'
|
|
154
|
+
tag_handling?: string[]
|
|
155
|
+
non_splitting_tags?: string[]
|
|
156
|
+
outline_detection?: string
|
|
157
|
+
splitting_tags?: string[]
|
|
158
|
+
ignore_tags?: string[]
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface DeeplResponse {
|
|
162
|
+
translations: DeeplTranslation[]
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface DeeplTranslation {
|
|
166
|
+
detected_source_language: string
|
|
167
|
+
text: string
|
|
168
|
+
}
|