@theia/localization-manager 1.67.0-next.56 → 1.67.0-next.59
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/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 +28 -0
- package/lib/deepl-api.d.ts.map +1 -0
- package/lib/deepl-api.js +122 -0
- package/lib/deepl-api.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +22 -0
- package/lib/index.js.map +1 -0
- package/lib/localization-extractor.d.ts +14 -0
- package/lib/localization-extractor.d.ts.map +1 -0
- package/lib/localization-extractor.js +378 -0
- package/lib/localization-extractor.js.map +1 -0
- package/lib/localization-extractor.spec.d.ts +2 -0
- package/lib/localization-extractor.spec.d.ts.map +1 -0
- package/lib/localization-extractor.spec.js +139 -0
- package/lib/localization-extractor.spec.js.map +1 -0
- package/lib/localization-manager.d.ts +25 -0
- package/lib/localization-manager.d.ts.map +1 -0
- package/lib/localization-manager.js +157 -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 +85 -0
- package/lib/localization-manager.spec.js.map +1 -0
- package/package.json +2 -2
package/lib/common.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAgBA,MAAM,WAAW,YAAY;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,YAAY,CAAA;CACvC;AAED,wBAAgB,gBAAgB,CAAC,YAAY,EAAE,YAAY,GAAG,YAAY,CAMzE"}
|
package/lib/common.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
exports.sortLocalization = void 0;
|
|
19
|
+
function sortLocalization(localization) {
|
|
20
|
+
return Object.keys(localization).sort().reduce((result, key) => {
|
|
21
|
+
const value = localization[key];
|
|
22
|
+
result[key] = typeof value === 'string' ? value : sortLocalization(value);
|
|
23
|
+
return result;
|
|
24
|
+
}, {});
|
|
25
|
+
}
|
|
26
|
+
exports.sortLocalization = sortLocalization;
|
|
27
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.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,gFAAgF;AAChF,gFAAgF;;;AAMhF,SAAgB,gBAAgB,CAAC,YAA0B;IACvD,OAAO,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC,MAAoB,EAAE,GAAW,EAAE,EAAE;QACjF,MAAM,KAAK,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;QAChC,MAAM,CAAC,GAAG,CAAC,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;QAC1E,OAAO,MAAM,CAAC;IAClB,CAAC,EAAE,EAAE,CAAC,CAAC;AACX,CAAC;AAND,4CAMC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare function deepl(parameters: DeeplParameters): Promise<DeeplResponse>;
|
|
2
|
+
export type DeeplLanguage = 'BG' | 'CS' | 'DA' | 'DE' | 'EL' | 'EN-GB' | 'EN-US' | 'EN' | 'ES' | 'ET' | 'FI' | 'FR' | 'HU' | 'ID' | 'IT' | 'JA' | 'KO' | 'LT' | 'LV' | 'NB' | 'NL' | 'PL' | 'PT-PT' | 'PT-BR' | 'PT' | 'RO' | 'RU' | 'SK' | 'SL' | 'SV' | 'TR' | 'UK' | 'ZH-CN' | 'ZH-TW' | 'ZH-HANS' | 'ZH-HANT' | 'ZH';
|
|
3
|
+
export declare const supportedLanguages: string[];
|
|
4
|
+
export declare const defaultLanguages: readonly ["ZH-CN", "ZH-TW", "FR", "DE", "IT", "ES", "JA", "KO", "RU", "PT-BR", "TR", "PL", "CS", "HU"];
|
|
5
|
+
export declare function isSupportedLanguage(language: string): language is DeeplLanguage;
|
|
6
|
+
export interface DeeplParameters {
|
|
7
|
+
free_api: Boolean;
|
|
8
|
+
auth_key: string;
|
|
9
|
+
text: string[];
|
|
10
|
+
source_lang?: DeeplLanguage;
|
|
11
|
+
target_lang: DeeplLanguage;
|
|
12
|
+
split_sentences?: '0' | '1' | 'nonewlines';
|
|
13
|
+
preserve_formatting?: '0' | '1';
|
|
14
|
+
formality?: 'default' | 'more' | 'less';
|
|
15
|
+
tag_handling?: string[];
|
|
16
|
+
non_splitting_tags?: string[];
|
|
17
|
+
outline_detection?: string;
|
|
18
|
+
splitting_tags?: string[];
|
|
19
|
+
ignore_tags?: string[];
|
|
20
|
+
}
|
|
21
|
+
export interface DeeplResponse {
|
|
22
|
+
translations: DeeplTranslation[];
|
|
23
|
+
}
|
|
24
|
+
export interface DeeplTranslation {
|
|
25
|
+
detected_source_language: string;
|
|
26
|
+
text: string;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=deepl-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepl-api.d.ts","sourceRoot":"","sources":["../src/deepl-api.ts"],"names":[],"mappings":"AA4BA,wBAAsB,KAAK,CACvB,UAAU,EAAE,eAAe,GAC5B,OAAO,CAAC,aAAa,CAAC,CAsBxB;AA4DD,MAAM,MAAM,aAAa,GACnB,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,OAAO,GACP,OAAO,GACP,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,OAAO,GACP,OAAO,GACP,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,OAAO,GACP,OAAO,GACP,SAAS,GACT,SAAS,GACT,IAAI,CAAC;AAEX,eAAO,MAAM,kBAAkB,UAG9B,CAAC;AAGF,eAAO,MAAM,gBAAgB,wGAEnB,CAAC;AAEX,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,QAAQ,IAAI,aAAa,CAE/E;AAED,MAAM,WAAW,eAAe;IAC5B,QAAQ,EAAE,OAAO,CAAA;IACjB,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,EAAE,CAAA;IACd,WAAW,CAAC,EAAE,aAAa,CAAA;IAC3B,WAAW,EAAE,aAAa,CAAA;IAC1B,eAAe,CAAC,EAAE,GAAG,GAAG,GAAG,GAAG,YAAY,CAAA;IAC1C,mBAAmB,CAAC,EAAE,GAAG,GAAG,GAAG,CAAA;IAC/B,SAAS,CAAC,EAAE,SAAS,GAAG,MAAM,GAAG,MAAM,CAAA;IACvC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;IACvB,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAA;IAC7B,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAA;IACzB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAA;CACzB;AAED,MAAM,WAAW,aAAa;IAC1B,YAAY,EAAE,gBAAgB,EAAE,CAAA;CACnC;AAED,MAAM,WAAW,gBAAgB;IAC7B,wBAAwB,EAAE,MAAM,CAAA;IAChC,IAAI,EAAE,MAAM,CAAA;CACf"}
|
package/lib/deepl-api.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
exports.isSupportedLanguage = exports.defaultLanguages = exports.supportedLanguages = exports.deepl = void 0;
|
|
19
|
+
const bent = require("bent");
|
|
20
|
+
const limiter_1 = require("limiter");
|
|
21
|
+
const post = bent('POST', 'json', 200);
|
|
22
|
+
// 50 is the maximum amount of translations per request
|
|
23
|
+
const deeplLimit = 50;
|
|
24
|
+
const rateLimiter = new limiter_1.RateLimiter({
|
|
25
|
+
tokensPerInterval: 10,
|
|
26
|
+
interval: 'second',
|
|
27
|
+
fireImmediately: true
|
|
28
|
+
});
|
|
29
|
+
async function deepl(parameters) {
|
|
30
|
+
coerceLanguage(parameters);
|
|
31
|
+
const sub_domain = parameters.free_api ? 'api-free' : 'api';
|
|
32
|
+
const textChunks = [];
|
|
33
|
+
const textArray = [...parameters.text];
|
|
34
|
+
while (textArray.length > 0) {
|
|
35
|
+
textChunks.push(textArray.splice(0, deeplLimit));
|
|
36
|
+
}
|
|
37
|
+
const responses = await Promise.all(textChunks.map(async (chunk) => {
|
|
38
|
+
const parameterCopy = { ...parameters, text: chunk };
|
|
39
|
+
const url = `https://${sub_domain}.deepl.com/v2/translate`;
|
|
40
|
+
const buffer = Buffer.from(toFormData(parameterCopy));
|
|
41
|
+
return postWithRetry(url, buffer, 1);
|
|
42
|
+
}));
|
|
43
|
+
const mergedResponse = { translations: [] };
|
|
44
|
+
for (const response of responses) {
|
|
45
|
+
mergedResponse.translations.push(...response.translations);
|
|
46
|
+
}
|
|
47
|
+
for (const translation of mergedResponse.translations) {
|
|
48
|
+
translation.text = coerceTranslation(translation.text);
|
|
49
|
+
}
|
|
50
|
+
return mergedResponse;
|
|
51
|
+
}
|
|
52
|
+
exports.deepl = deepl;
|
|
53
|
+
async function postWithRetry(url, buffer, attempt) {
|
|
54
|
+
try {
|
|
55
|
+
await rateLimiter.removeTokens(Math.min(attempt, 10));
|
|
56
|
+
const response = await post(url, buffer, {
|
|
57
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
58
|
+
'User-Agent': 'Theia-Localization-Manager'
|
|
59
|
+
});
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
if ('message' in e && typeof e.message === 'string' && e.message.includes('Too Many Requests')) {
|
|
64
|
+
return postWithRetry(url, buffer, attempt + 1);
|
|
65
|
+
}
|
|
66
|
+
throw e;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Coerces the target language into a form expected by Deepl.
|
|
71
|
+
*
|
|
72
|
+
* Currently only replaces `ZH-CN` with `ZH`
|
|
73
|
+
*/
|
|
74
|
+
function coerceLanguage(parameters) {
|
|
75
|
+
if (parameters.target_lang === 'ZH-CN') {
|
|
76
|
+
parameters.target_lang = 'ZH-HANS';
|
|
77
|
+
}
|
|
78
|
+
else if (parameters.target_lang === 'ZH-TW') {
|
|
79
|
+
parameters.target_lang = 'ZH-HANT';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Coerces translated text into a form expected by VSCode/Theia.
|
|
84
|
+
*
|
|
85
|
+
* Replaces certain full-width characters with their ascii counter-part.
|
|
86
|
+
*/
|
|
87
|
+
function coerceTranslation(text) {
|
|
88
|
+
return text
|
|
89
|
+
.replace(/\uff08/g, '(')
|
|
90
|
+
.replace(/\uff09/g, ')')
|
|
91
|
+
.replace(/\uff0c/g, ',')
|
|
92
|
+
.replace(/\uff1a/g, ':')
|
|
93
|
+
.replace(/\uff1b/g, ';')
|
|
94
|
+
.replace(/\uff1f/g, '?');
|
|
95
|
+
}
|
|
96
|
+
function toFormData(parameters) {
|
|
97
|
+
const str = [];
|
|
98
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
99
|
+
if (typeof value === 'string') {
|
|
100
|
+
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(value.toString()));
|
|
101
|
+
}
|
|
102
|
+
else if (Array.isArray(value)) {
|
|
103
|
+
for (const item of value) {
|
|
104
|
+
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(item.toString()));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return str.join('&');
|
|
109
|
+
}
|
|
110
|
+
exports.supportedLanguages = [
|
|
111
|
+
'BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'ID', 'IT',
|
|
112
|
+
'JA', 'KO', 'LT', 'LV', 'NL', 'PL', 'PT-PT', 'PT-BR', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'TR', 'UK', 'ZH-CN', 'ZH-TW'
|
|
113
|
+
];
|
|
114
|
+
// From https://code.visualstudio.com/docs/getstarted/locales#_available-locales
|
|
115
|
+
exports.defaultLanguages = [
|
|
116
|
+
'ZH-CN', 'ZH-TW', 'FR', 'DE', 'IT', 'ES', 'JA', 'KO', 'RU', 'PT-BR', 'TR', 'PL', 'CS', 'HU'
|
|
117
|
+
];
|
|
118
|
+
function isSupportedLanguage(language) {
|
|
119
|
+
return exports.supportedLanguages.includes(language.toUpperCase());
|
|
120
|
+
}
|
|
121
|
+
exports.isSupportedLanguage = isSupportedLanguage;
|
|
122
|
+
//# sourceMappingURL=deepl-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepl-api.js","sourceRoot":"","sources":["../src/deepl-api.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,gFAAgF;AAChF,gFAAgF;;;AAEhF,6BAA6B;AAC7B,qCAAsC;AAEtC,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;AACvC,uDAAuD;AACvD,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,M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E,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO;CACzH,CAAC;AAEF,gFAAgF;AACnE,QAAA,gBAAgB,GAAG;IAC5B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;CACrF,CAAC;AAEX,SAAgB,mBAAmB,CAAC,QAAgB;IAChD,OAAO,0BAAkB,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;AAC/D,CAAC;AAFD,kDAEC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgBA,cAAc,UAAU,CAAC;AACzB,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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 tslib_1 = require("tslib");
|
|
19
|
+
tslib_1.__exportStar(require("./common"), exports);
|
|
20
|
+
tslib_1.__exportStar(require("./localization-extractor"), exports);
|
|
21
|
+
tslib_1.__exportStar(require("./localization-manager"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.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,gFAAgF;AAChF,gFAAgF;;;AAEhF,mDAAyB;AACzB,mEAAyC;AACzC,iEAAuC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Localization } from './common';
|
|
2
|
+
export interface ExtractionOptions {
|
|
3
|
+
root?: string;
|
|
4
|
+
output?: string;
|
|
5
|
+
exclude?: string;
|
|
6
|
+
logs?: string;
|
|
7
|
+
/** List of globs matching the files to extract from. */
|
|
8
|
+
files?: string[];
|
|
9
|
+
merge?: boolean;
|
|
10
|
+
quiet?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function extract(options: ExtractionOptions): Promise<void>;
|
|
13
|
+
export declare function extractFromFile(file: string, content: string, errors?: string[], options?: ExtractionOptions): Promise<Localization>;
|
|
14
|
+
//# sourceMappingURL=localization-extractor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization-extractor.d.ts","sourceRoot":"","sources":["../src/localization-extractor.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,YAAY,EAAoB,MAAM,UAAU,CAAC;AAI1D,MAAM,WAAW,iBAAiB;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,KAAK,CAAC,EAAE,OAAO,CAAA;CAClB;AA6CD,wBAAsB,OAAO,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CA4BvE;AAED,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,YAAY,CAAC,CAyC1I"}
|
|
@@ -0,0 +1,378 @@
|
|
|
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
|
+
exports.extractFromFile = exports.extract = void 0;
|
|
19
|
+
const fs = require("fs-extra");
|
|
20
|
+
const ts = require("typescript");
|
|
21
|
+
const os = require("os");
|
|
22
|
+
const path = require("path");
|
|
23
|
+
const glob_1 = require("glob");
|
|
24
|
+
const util_1 = require("util");
|
|
25
|
+
const deepmerge = require("deepmerge");
|
|
26
|
+
const common_1 = require("./common");
|
|
27
|
+
const globPromise = (0, util_1.promisify)(glob_1.glob);
|
|
28
|
+
class SingleFileServiceHost {
|
|
29
|
+
constructor(options, filename, contents) {
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.filename = filename;
|
|
32
|
+
this.getCompilationSettings = () => this.options;
|
|
33
|
+
this.getScriptFileNames = () => [this.filename];
|
|
34
|
+
this.getScriptVersion = () => '1';
|
|
35
|
+
this.getScriptSnapshot = (name) => name === this.filename ? this.file : this.lib;
|
|
36
|
+
this.getCurrentDirectory = () => '';
|
|
37
|
+
this.getDefaultLibFileName = () => 'lib.d.ts';
|
|
38
|
+
this.file = ts.ScriptSnapshot.fromString(contents);
|
|
39
|
+
this.lib = ts.ScriptSnapshot.fromString('');
|
|
40
|
+
}
|
|
41
|
+
readFile(file, encoding) {
|
|
42
|
+
if (file === this.filename) {
|
|
43
|
+
return this.file.getText(0, this.file.getLength());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
fileExists(file) {
|
|
47
|
+
return this.filename === file;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class TypeScriptError extends Error {
|
|
51
|
+
constructor(message, node) {
|
|
52
|
+
super(buildErrorMessage(message, node));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function buildErrorMessage(message, node) {
|
|
56
|
+
const source = node.getSourceFile();
|
|
57
|
+
const sourcePath = source.fileName;
|
|
58
|
+
const pos = source.getLineAndCharacterOfPosition(node.pos);
|
|
59
|
+
return `${sourcePath}(${pos.line + 1},${pos.character + 1}): ${message}`;
|
|
60
|
+
}
|
|
61
|
+
const tsOptions = {
|
|
62
|
+
allowJs: true
|
|
63
|
+
};
|
|
64
|
+
async function extract(options) {
|
|
65
|
+
var _a, _b, _c;
|
|
66
|
+
const cwd = path.resolve(process.env.INIT_CWD || process.cwd(), (_a = options.root) !== null && _a !== void 0 ? _a : '');
|
|
67
|
+
const files = [];
|
|
68
|
+
await Promise.all(((_b = options.files) !== null && _b !== void 0 ? _b : ['**/src/**/*.{ts,tsx}']).map(async (pattern) => files.push(...await globPromise(pattern, { cwd }))));
|
|
69
|
+
let localization = {};
|
|
70
|
+
const errors = [];
|
|
71
|
+
for (const file of files) {
|
|
72
|
+
const filePath = path.resolve(cwd, file);
|
|
73
|
+
const fileName = path.relative(cwd, file).split(path.sep).join('/');
|
|
74
|
+
const content = await fs.readFile(filePath, 'utf8');
|
|
75
|
+
const fileLocalization = await extractFromFile(fileName, content, errors, options);
|
|
76
|
+
localization = deepmerge(localization, fileLocalization);
|
|
77
|
+
}
|
|
78
|
+
if (errors.length > 0 && options.logs) {
|
|
79
|
+
await fs.writeFile(options.logs, errors.join(os.EOL));
|
|
80
|
+
}
|
|
81
|
+
const out = path.resolve(process.env.INIT_CWD || process.cwd(), (_c = options.output) !== null && _c !== void 0 ? _c : '');
|
|
82
|
+
if (options.merge && await fs.pathExists(out)) {
|
|
83
|
+
const existing = await fs.readJson(out);
|
|
84
|
+
localization = deepmerge(existing, localization);
|
|
85
|
+
}
|
|
86
|
+
localization = (0, common_1.sortLocalization)(localization);
|
|
87
|
+
await fs.mkdirs(path.dirname(out));
|
|
88
|
+
await fs.writeJson(out, localization, {
|
|
89
|
+
spaces: 2
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
exports.extract = extract;
|
|
93
|
+
async function extractFromFile(file, content, errors, options) {
|
|
94
|
+
const serviceHost = new SingleFileServiceHost(tsOptions, file, content);
|
|
95
|
+
const service = ts.createLanguageService(serviceHost);
|
|
96
|
+
const sourceFile = service.getProgram().getSourceFile(file);
|
|
97
|
+
const localization = {};
|
|
98
|
+
const localizationCalls = collect(sourceFile, node => isLocalizeCall(node));
|
|
99
|
+
for (const call of localizationCalls) {
|
|
100
|
+
try {
|
|
101
|
+
const extracted = extractFromLocalizeCall(call, options);
|
|
102
|
+
if (extracted) {
|
|
103
|
+
insert(localization, extracted);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
const tsError = err;
|
|
108
|
+
errors === null || errors === void 0 ? void 0 : errors.push(tsError.message);
|
|
109
|
+
if (!(options === null || options === void 0 ? void 0 : options.quiet)) {
|
|
110
|
+
console.log(tsError.message);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const localizedCommands = collect(sourceFile, node => isCommandLocalizeUtility(node));
|
|
115
|
+
for (const command of localizedCommands) {
|
|
116
|
+
try {
|
|
117
|
+
const extracted = extractFromLocalizedCommandCall(command, errors, options);
|
|
118
|
+
const label = extracted.label;
|
|
119
|
+
const category = extracted.category;
|
|
120
|
+
if (!isExcluded(options, label[0])) {
|
|
121
|
+
insert(localization, label);
|
|
122
|
+
}
|
|
123
|
+
if (category && !isExcluded(options, category[0])) {
|
|
124
|
+
insert(localization, category);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
const tsError = err;
|
|
129
|
+
errors === null || errors === void 0 ? void 0 : errors.push(tsError.message);
|
|
130
|
+
if (!(options === null || options === void 0 ? void 0 : options.quiet)) {
|
|
131
|
+
console.log(tsError.message);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return localization;
|
|
136
|
+
}
|
|
137
|
+
exports.extractFromFile = extractFromFile;
|
|
138
|
+
function isExcluded(options, key) {
|
|
139
|
+
return !!(options === null || options === void 0 ? void 0 : options.exclude) && key.startsWith(options.exclude);
|
|
140
|
+
}
|
|
141
|
+
function insert(localization, values) {
|
|
142
|
+
const key = values[0];
|
|
143
|
+
const value = values[1];
|
|
144
|
+
const node = values[2];
|
|
145
|
+
const parts = key.split('/');
|
|
146
|
+
parts.forEach((part, i) => {
|
|
147
|
+
let entry = localization[part];
|
|
148
|
+
if (i === parts.length - 1) {
|
|
149
|
+
if (typeof entry === 'object') {
|
|
150
|
+
throw new TypeScriptError(`Multiple translation keys already exist at '${key}'`, node);
|
|
151
|
+
}
|
|
152
|
+
localization[part] = value;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
if (typeof entry === 'string') {
|
|
156
|
+
throw new TypeScriptError(`String entry already exists at '${parts.splice(0, i + 1).join('/')}'`, node);
|
|
157
|
+
}
|
|
158
|
+
if (!entry) {
|
|
159
|
+
entry = {};
|
|
160
|
+
}
|
|
161
|
+
localization[part] = entry;
|
|
162
|
+
localization = entry;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
function collect(n, fn) {
|
|
167
|
+
const result = [];
|
|
168
|
+
function loop(node) {
|
|
169
|
+
const stepResult = fn(node);
|
|
170
|
+
if (stepResult) {
|
|
171
|
+
result.push(node);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
ts.forEachChild(node, loop);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
loop(n);
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
function isLocalizeCall(node) {
|
|
181
|
+
if (!ts.isCallExpression(node)) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
return node.expression.getText() === 'nls.localize';
|
|
185
|
+
}
|
|
186
|
+
function extractFromLocalizeCall(node, options) {
|
|
187
|
+
if (!ts.isCallExpression(node)) {
|
|
188
|
+
throw new TypeScriptError('Invalid node type', node);
|
|
189
|
+
}
|
|
190
|
+
const args = node.arguments;
|
|
191
|
+
if (args.length < 2) {
|
|
192
|
+
throw new TypeScriptError('Localize call needs at least 2 arguments', node);
|
|
193
|
+
}
|
|
194
|
+
const key = extractString(args[0]);
|
|
195
|
+
const value = extractString(args[1]);
|
|
196
|
+
if (isExcluded(options, key)) {
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
return [key, value, args[1]];
|
|
200
|
+
}
|
|
201
|
+
function extractFromLocalizedCommandCall(node, errors, options) {
|
|
202
|
+
if (!ts.isCallExpression(node)) {
|
|
203
|
+
throw new TypeScriptError('Invalid node type', node);
|
|
204
|
+
}
|
|
205
|
+
const args = node.arguments;
|
|
206
|
+
if (args.length < 1) {
|
|
207
|
+
throw new TypeScriptError('Command localization call needs at least one argument', node);
|
|
208
|
+
}
|
|
209
|
+
const commandObj = args[0];
|
|
210
|
+
if (!ts.isObjectLiteralExpression(commandObj)) {
|
|
211
|
+
throw new TypeScriptError('First argument of "toLocalizedCommand" needs to be an object literal', node);
|
|
212
|
+
}
|
|
213
|
+
const properties = commandObj.properties;
|
|
214
|
+
const propertyMap = new Map();
|
|
215
|
+
const relevantProps = ['id', 'label', 'category'];
|
|
216
|
+
let labelNode = node;
|
|
217
|
+
for (const property of properties) {
|
|
218
|
+
if (!property.name) {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (!ts.isPropertyAssignment(property)) {
|
|
222
|
+
throw new TypeScriptError('Only property assignments in "toLocalizedCommand" are allowed', property);
|
|
223
|
+
}
|
|
224
|
+
if (!ts.isIdentifier(property.name)) {
|
|
225
|
+
throw new TypeScriptError('Only identifiers are allowed as property names in "toLocalizedCommand"', property);
|
|
226
|
+
}
|
|
227
|
+
const name = property.name.text;
|
|
228
|
+
if (!relevantProps.includes(property.name.text)) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (property.name.text === 'label') {
|
|
232
|
+
labelNode = property.initializer;
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
const value = extractString(property.initializer);
|
|
236
|
+
propertyMap.set(name, value);
|
|
237
|
+
}
|
|
238
|
+
catch (err) {
|
|
239
|
+
const tsError = err;
|
|
240
|
+
errors === null || errors === void 0 ? void 0 : errors.push(tsError.message);
|
|
241
|
+
if (!(options === null || options === void 0 ? void 0 : options.quiet)) {
|
|
242
|
+
console.log(tsError.message);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
let labelKey = propertyMap.get('id');
|
|
247
|
+
let categoryKey = undefined;
|
|
248
|
+
let categoryNode;
|
|
249
|
+
// We have an explicit label translation key
|
|
250
|
+
if (args.length > 1) {
|
|
251
|
+
try {
|
|
252
|
+
const labelOverrideKey = extractStringOrUndefined(args[1]);
|
|
253
|
+
if (labelOverrideKey) {
|
|
254
|
+
labelKey = labelOverrideKey;
|
|
255
|
+
labelNode = args[1];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
catch (err) {
|
|
259
|
+
const tsError = err;
|
|
260
|
+
errors === null || errors === void 0 ? void 0 : errors.push(tsError.message);
|
|
261
|
+
if (!(options === null || options === void 0 ? void 0 : options.quiet)) {
|
|
262
|
+
console.log(tsError.message);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// We have an explicit category translation key
|
|
267
|
+
if (args.length > 2) {
|
|
268
|
+
try {
|
|
269
|
+
categoryKey = extractStringOrUndefined(args[2]);
|
|
270
|
+
categoryNode = args[2];
|
|
271
|
+
}
|
|
272
|
+
catch (err) {
|
|
273
|
+
const tsError = err;
|
|
274
|
+
errors === null || errors === void 0 ? void 0 : errors.push(tsError.message);
|
|
275
|
+
if (!(options === null || options === void 0 ? void 0 : options.quiet)) {
|
|
276
|
+
console.log(tsError.message);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (!labelKey) {
|
|
281
|
+
throw new TypeScriptError('No label key found', node);
|
|
282
|
+
}
|
|
283
|
+
if (!propertyMap.get('label')) {
|
|
284
|
+
throw new TypeScriptError('No default label found', node);
|
|
285
|
+
}
|
|
286
|
+
let categoryLocalization = undefined;
|
|
287
|
+
const categoryLabel = propertyMap.get('category');
|
|
288
|
+
if (categoryKey && categoryLabel && categoryNode) {
|
|
289
|
+
categoryLocalization = [categoryKey, categoryLabel, categoryNode];
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
label: [labelKey, propertyMap.get('label'), labelNode],
|
|
293
|
+
category: categoryLocalization
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
function extractStringOrUndefined(node) {
|
|
297
|
+
if (node.getText() === 'undefined') {
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
300
|
+
return extractString(node);
|
|
301
|
+
}
|
|
302
|
+
function extractString(node) {
|
|
303
|
+
if (ts.isIdentifier(node)) {
|
|
304
|
+
const reference = followReference(node);
|
|
305
|
+
if (!reference) {
|
|
306
|
+
throw new TypeScriptError(`Could not resolve reference to '${node.text}'`, node);
|
|
307
|
+
}
|
|
308
|
+
node = reference;
|
|
309
|
+
}
|
|
310
|
+
if (ts.isTemplateLiteral(node)) {
|
|
311
|
+
throw new TypeScriptError("Template literals are not supported for localization. Please use the additional arguments of the 'nls.localize' function to format strings", node);
|
|
312
|
+
}
|
|
313
|
+
if (!ts.isStringLiteralLike(node)) {
|
|
314
|
+
throw new TypeScriptError(`'${node.getText()}' is not a string constant`, node);
|
|
315
|
+
}
|
|
316
|
+
return unescapeString(node.text);
|
|
317
|
+
}
|
|
318
|
+
function followReference(node) {
|
|
319
|
+
const scope = collectScope(node);
|
|
320
|
+
const next = scope.get(node.text);
|
|
321
|
+
if (next && ts.isIdentifier(next)) {
|
|
322
|
+
return followReference(next);
|
|
323
|
+
}
|
|
324
|
+
return next;
|
|
325
|
+
}
|
|
326
|
+
function collectScope(node, map = new Map()) {
|
|
327
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
328
|
+
const locals = node['locals'];
|
|
329
|
+
if (locals) {
|
|
330
|
+
for (const [key, value] of locals.entries()) {
|
|
331
|
+
if (!map.has(key)) {
|
|
332
|
+
const declaration = value.valueDeclaration;
|
|
333
|
+
if (declaration && ts.isVariableDeclaration(declaration) && declaration.initializer) {
|
|
334
|
+
map.set(key, declaration.initializer);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (node.parent) {
|
|
340
|
+
collectScope(node.parent, map);
|
|
341
|
+
}
|
|
342
|
+
return map;
|
|
343
|
+
}
|
|
344
|
+
function isCommandLocalizeUtility(node) {
|
|
345
|
+
if (!ts.isCallExpression(node)) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
return node.expression.getText() === 'Command.toLocalizedCommand';
|
|
349
|
+
}
|
|
350
|
+
const unescapeMap = {
|
|
351
|
+
'\'': '\'',
|
|
352
|
+
'"': '"',
|
|
353
|
+
'\\': '\\',
|
|
354
|
+
'n': '\n',
|
|
355
|
+
'r': '\r',
|
|
356
|
+
't': '\t',
|
|
357
|
+
'b': '\b',
|
|
358
|
+
'f': '\f'
|
|
359
|
+
};
|
|
360
|
+
function unescapeString(str) {
|
|
361
|
+
const result = [];
|
|
362
|
+
for (let i = 0; i < str.length; i++) {
|
|
363
|
+
const ch = str.charAt(i);
|
|
364
|
+
if (ch === '\\') {
|
|
365
|
+
if (i + 1 < str.length) {
|
|
366
|
+
const replace = unescapeMap[str.charAt(i + 1)];
|
|
367
|
+
if (replace !== undefined) {
|
|
368
|
+
result.push(replace);
|
|
369
|
+
i++;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
result.push(ch);
|
|
375
|
+
}
|
|
376
|
+
return result.join('');
|
|
377
|
+
}
|
|
378
|
+
//# sourceMappingURL=localization-extractor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization-extractor.js","sourceRoot":"","sources":["../src/localization-extractor.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,gFAAgF;AAChF,gFAAgF;;;AAEhF,+BAA+B;AAC/B,iCAAiC;AACjC,yBAAyB;AACzB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AI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;CACZ,CAAC;AAEF,SAAS,cAAc,CAAC,GAAW;IAC/B,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,MAAM,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACzB,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC;YACd,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC;gBACrB,MAAM,OAAO,GAAG,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBAC/C,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;oBACxB,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;oBACrB,CAAC,EAAE,CAAC;oBACJ,SAAS;gBACb,CAAC;YACL,CAAC;QACL,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACpB,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization-extractor.spec.d.ts","sourceRoot":"","sources":["../src/localization-extractor.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,139 @@
|
|
|
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_extractor_1 = require("./localization-extractor");
|
|
20
|
+
const TEST_FILE = 'test.ts';
|
|
21
|
+
const quiet = { quiet: true };
|
|
22
|
+
describe('correctly extracts from file content', () => {
|
|
23
|
+
it('should extract from simple nls.localize() call', async () => {
|
|
24
|
+
const content = 'nls.localize("key", "value")';
|
|
25
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
26
|
+
'key': 'value'
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
it('should extract from nested nls.localize() call', async () => {
|
|
30
|
+
const content = 'nls.localize("nested/key", "value")';
|
|
31
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
32
|
+
'nested': {
|
|
33
|
+
'key': 'value'
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
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');
|
|
47
|
+
`;
|
|
48
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
49
|
+
'command-id1': 'command-label1',
|
|
50
|
+
'command-key': 'command-label2'
|
|
51
|
+
});
|
|
52
|
+
});
|
|
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'
|
|
59
|
+
}, undefined, 'category-key');`;
|
|
60
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
61
|
+
'id': 'label',
|
|
62
|
+
'category-key': 'category'
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
it('should merge different nls.localize() calls', async () => {
|
|
66
|
+
const content = `
|
|
67
|
+
nls.localize('nested/key1', 'value1');
|
|
68
|
+
nls.localize('nested/key2', 'value2');
|
|
69
|
+
`;
|
|
70
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
71
|
+
'nested': {
|
|
72
|
+
'key1': 'value1',
|
|
73
|
+
'key2': 'value2'
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
it('should be able to resolve local references', async () => {
|
|
78
|
+
const content = `
|
|
79
|
+
const a = 'key';
|
|
80
|
+
nls.localize(a, 'value');
|
|
81
|
+
`;
|
|
82
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content), {
|
|
83
|
+
'key': 'value'
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
it('should return an error when resolving is not successful', async () => {
|
|
87
|
+
const content = "nls.localize(a, 'value')";
|
|
88
|
+
const errors = [];
|
|
89
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {});
|
|
90
|
+
assert.deepStrictEqual(errors, [
|
|
91
|
+
"test.ts(1,14): Could not resolve reference to 'a'"
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
it('should return an error when resolving from an expression', async () => {
|
|
95
|
+
const content = "nls.localize(test.value, 'value');";
|
|
96
|
+
const errors = [];
|
|
97
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {});
|
|
98
|
+
assert.deepStrictEqual(errors, [
|
|
99
|
+
"test.ts(1,14): 'test.value' is not a string constant"
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
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');
|
|
106
|
+
`.trim();
|
|
107
|
+
const errors = [];
|
|
108
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {
|
|
109
|
+
'key': 'value'
|
|
110
|
+
});
|
|
111
|
+
assert.deepStrictEqual(errors, [
|
|
112
|
+
"test.ts(2,35): String entry already exists at 'key'"
|
|
113
|
+
]);
|
|
114
|
+
});
|
|
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');
|
|
119
|
+
`.trim();
|
|
120
|
+
const errors = [];
|
|
121
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {
|
|
122
|
+
'key': {
|
|
123
|
+
'nested': 'value'
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
assert.deepStrictEqual(errors, [
|
|
127
|
+
"test.ts(2,28): Multiple translation keys already exist at 'key'"
|
|
128
|
+
]);
|
|
129
|
+
});
|
|
130
|
+
it('should show error for template literals', async () => {
|
|
131
|
+
const content = 'nls.localize("key", `template literal value`)';
|
|
132
|
+
const errors = [];
|
|
133
|
+
assert.deepStrictEqual(await (0, localization_extractor_1.extractFromFile)(TEST_FILE, content, errors, quiet), {});
|
|
134
|
+
assert.deepStrictEqual(errors, [
|
|
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"
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
//# sourceMappingURL=localization-extractor.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,gFAAgF;AAChF,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,EAAE,OAAO,CAAC,EAAE;YAC9D,KAAK,EAAE,OAAO;SACjB,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,gDAAgD,EAAE,KAAK,IAAI,EAAE;QAC5D,MAAM,OAAO,GAAG,qCAAqC,CAAC;QACtD,MAAM,CAAC,eAAe,CAAC,MAAM,IAAA,wCAAe,EAAC,SAAS,EAAE,OAAO,CAAC,EAAE;YAC9D,QAAQ,EAAE;gBACN,KAAK,EAAE,OAAO;aACjB;SACJ,CAAC,CAAC;IACP,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,2DAA2D,EAAE,KAAK,IAAI,EAAE;QACvE,MAAM,OAAO,GAAG;;;;;;;;;SASf,CAAC;QACF,MAAM,C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AC;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,25 @@
|
|
|
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 type LocalizationFunction = (parameters: DeeplParameters) => Promise<string[]>;
|
|
11
|
+
export declare class LocalizationManager {
|
|
12
|
+
private localizationFn;
|
|
13
|
+
constructor(localizationFn?: typeof deepl);
|
|
14
|
+
localize(options: LocalizationOptions): Promise<boolean>;
|
|
15
|
+
protected translationFileName(original: string, language: string): string;
|
|
16
|
+
translateLanguage(source: Localization, target: Localization, targetLanguage: string, options: LocalizationOptions): Promise<boolean>;
|
|
17
|
+
protected addIgnoreTags(text: string): string;
|
|
18
|
+
protected removeIgnoreTags(text: string): string;
|
|
19
|
+
protected buildLocalizationMap(source: Localization, target: Localization): LocalizationMap;
|
|
20
|
+
}
|
|
21
|
+
export interface LocalizationMap {
|
|
22
|
+
text: string[];
|
|
23
|
+
localize: (index: number, value: string) => void;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=localization-manager.d.ts.map
|
|
@@ -0,0 +1 @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AAA;CACnD"}
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
exports.LocalizationManager = void 0;
|
|
19
|
+
const chalk = require("chalk");
|
|
20
|
+
const fs = require("fs-extra");
|
|
21
|
+
const path = require("path");
|
|
22
|
+
const common_1 = require("./common");
|
|
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 {
|
|
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 === 0) {
|
|
49
|
+
// No supported languages were found, default to all supported languages
|
|
50
|
+
console.log('No languages were specified, defaulting to all supported languages for VS Code');
|
|
51
|
+
languages.push(...deepl_api_1.defaultLanguages);
|
|
52
|
+
}
|
|
53
|
+
const existingTranslations = new Map();
|
|
54
|
+
for (const targetLanguage of languages) {
|
|
55
|
+
try {
|
|
56
|
+
const targetPath = this.translationFileName(sourceFile, targetLanguage);
|
|
57
|
+
existingTranslations.set(targetLanguage, await fs.readJson(targetPath));
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
existingTranslations.set(targetLanguage, {});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const results = await Promise.all(languages.map(language => this.translateLanguage(source, existingTranslations.get(language), language, options)));
|
|
64
|
+
let result = results.reduce((acc, val) => acc && val, true);
|
|
65
|
+
for (const targetLanguage of languages) {
|
|
66
|
+
const targetPath = this.translationFileName(sourceFile, targetLanguage);
|
|
67
|
+
try {
|
|
68
|
+
const translation = existingTranslations.get(targetLanguage);
|
|
69
|
+
await fs.writeJson(targetPath, (0, common_1.sortLocalization)(translation), { spaces: 2 });
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
console.error(chalk.red(`Error writing translated file to '${targetPath}'`));
|
|
73
|
+
result = false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
translationFileName(original, language) {
|
|
79
|
+
const directory = path.dirname(original);
|
|
80
|
+
const fileName = path.basename(original, '.json');
|
|
81
|
+
return path.join(directory, `${fileName}.${language.toLowerCase()}.json`);
|
|
82
|
+
}
|
|
83
|
+
async translateLanguage(source, target, targetLanguage, options) {
|
|
84
|
+
var _a;
|
|
85
|
+
const map = this.buildLocalizationMap(source, target);
|
|
86
|
+
if (map.text.length > 0) {
|
|
87
|
+
try {
|
|
88
|
+
const translationResponse = await this.localizationFn({
|
|
89
|
+
auth_key: options.authKey,
|
|
90
|
+
free_api: options.freeApi,
|
|
91
|
+
target_lang: targetLanguage.toUpperCase(),
|
|
92
|
+
source_lang: (_a = options.sourceLanguage) === null || _a === void 0 ? void 0 : _a.toUpperCase(),
|
|
93
|
+
text: map.text.map(e => this.addIgnoreTags(e)),
|
|
94
|
+
tag_handling: ['xml'],
|
|
95
|
+
ignore_tags: ['x']
|
|
96
|
+
});
|
|
97
|
+
translationResponse.translations.forEach(({ text }, i) => {
|
|
98
|
+
map.localize(i, this.removeIgnoreTags(text));
|
|
99
|
+
});
|
|
100
|
+
console.log(chalk.green(`Successfully translated ${map.text.length} value${map.text.length > 1 ? 's' : ''} for language "${targetLanguage}"`));
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
console.log(chalk.red(`Could not translate into language "${targetLanguage}"`), e);
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
console.log(`No translation necessary for language "${targetLanguage}"`);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
addIgnoreTags(text) {
|
|
114
|
+
return text.replace(/(\{\d*\})/g, '<x>$1</x>');
|
|
115
|
+
}
|
|
116
|
+
removeIgnoreTags(text) {
|
|
117
|
+
return text.replace(/<x>(\{\d+\})<\/x>/g, '$1');
|
|
118
|
+
}
|
|
119
|
+
buildLocalizationMap(source, target) {
|
|
120
|
+
const functionMap = new Map();
|
|
121
|
+
const text = [];
|
|
122
|
+
const process = (s, t) => {
|
|
123
|
+
// Delete all extra keys in the target translation first
|
|
124
|
+
for (const key of Object.keys(t)) {
|
|
125
|
+
if (!(key in s)) {
|
|
126
|
+
delete t[key];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
for (const [key, value] of Object.entries(s)) {
|
|
130
|
+
if (!(key in t)) {
|
|
131
|
+
if (typeof value === 'string') {
|
|
132
|
+
functionMap.set(text.length, translation => t[key] = translation);
|
|
133
|
+
text.push(value);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
const newLocalization = {};
|
|
137
|
+
t[key] = newLocalization;
|
|
138
|
+
process(value, newLocalization);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (typeof value === 'object') {
|
|
142
|
+
if (typeof t[key] === 'string') {
|
|
143
|
+
t[key] = {};
|
|
144
|
+
}
|
|
145
|
+
process(value, t[key]);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
process(source, target);
|
|
150
|
+
return {
|
|
151
|
+
text,
|
|
152
|
+
localize: (index, value) => functionMap.get(index)(value)
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.LocalizationManager = LocalizationManager;
|
|
157
|
+
//# 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,gFAAgF;AAChF,gFAAgF;;;AAEhF,+BAA+B;AAC/B,+BAA+B;AAC/B,6BAA6B;AAC7B,qCAA0D;AAC1D,2CAA2G;AAY3G,MAAa,mBAAmB;IAE5B,YAAoB,iBAAiB,iBAAK;QAAtB,mBAAc,GAAd,cAAc,CAAQ;IAAI,CAAC;IAE/C,KAAK,CAAC,QAAQ,CAAC,O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GAAG,IAAI,CAAC,CAAC,EAAE,CAAC;oBACd,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;wBAC5B,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC;wBAClE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;oBACrB,CAAC;yBAAM,CAAC;wBACJ,MAAM,eAAe,GAAiB,EAAE,CAAC;wBACzC,CAAC,CAAC,GAAG,CAAC,GAAG,eAAe,CAAC;wBACzB,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC;oBACpC,CAAC;gBACL,CAAC;qBAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;oBACnC,IAAI,OAAO,CAAC,CAAC,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;wBAC7B,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;oBAChB,CAAC;oBACD,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,CAAiB,CAAC,CAAC;gBAC3C,CAAC;YACL,CAAC;QACL,CAAC,CAAC;QAEF,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAExB,OAAO;YACH,IAAI;YACJ,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,KAAK,CAAC;SAC7D,CAAC;IACN,CAAC;CACJ;AAlID,kDAkIC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"localization-manager.spec.d.ts","sourceRoot":"","sources":["../src/localization-manager.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +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
|
+
});
|
|
85
|
+
//# 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,gFAAgF;AAChF,gFAAgF;;AAEhF,iCAAiC;AAEjC,iEAAkF;AAElF,QAAQ,CAAC,wCAAwC,EAAE,GAAG,EAAE;IAEpD,KAAK,UAAU,gBAAgB,CAAC,UAA2B;QAC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EAAE;QAClD,MAAM,KAAK,GAAG;YACV,CAAC,EAAE,GAAG;SACT,CAAC;QACF,MAAM,MAAM,GAAG;YACX,CAAC,EAAE,GAAG;SACT,CAAC;QACF,MAAM,OAAO,CAAC,iBAAiB,CAAC,KAAK,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;IAEH,EAAE,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;QAC7C,MAAM,KAAK,GAAG;YACV,GAAG,EAAE,SAAS;SACjB,CAAC;QACF,MAAM,MAAM,GAAG,EAAE,CAAC;QAClB,MAAM,OAAO,CAAC,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC;QACrE,MAAM,CAAC,eAAe,CAAC,MAAM,EAAE;YAC3B,GAAG,EAAE,WAAW;SACnB,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.67.0-next.
|
|
3
|
+
"version": "1.67.0-next.59+3f14297ea",
|
|
4
4
|
"description": "Theia localization manager API.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"nyc": {
|
|
47
47
|
"extends": "../../configs/nyc.json"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "3f14297ea2edcdb1fffd74afee0613e70b43e125"
|
|
50
50
|
}
|