@theia/localization-manager 1.45.0 → 1.46.0-next.72

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.
@@ -1,2 +1,2 @@
1
- export {};
1
+ export {};
2
2
  //# sourceMappingURL=localization-extractor.spec.d.ts.map
@@ -1,139 +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
- });
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
139
  //# sourceMappingURL=localization-extractor.spec.js.map
@@ -1,25 +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 declare type LocalizationFunction = (parameters: DeeplParameters) => Promise<string[]>;
11
- export declare class LocalizationManager {
12
- private localizationFn;
13
- constructor(localizationFn?: typeof deepl);
14
- localize(options: LocalizationOptions): Promise<void>;
15
- protected translationFileName(original: string, language: string): string;
16
- translateLanguage(source: Localization, target: Localization, targetLanguage: string, options: LocalizationOptions): Promise<void>;
17
- protected 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
- }
1
+ import { Localization } from './common';
2
+ import { deepl, DeeplParameters } from './deepl-api';
3
+ export interface LocalizationOptions {
4
+ freeApi: Boolean;
5
+ authKey: string;
6
+ sourceFile: string;
7
+ sourceLanguage?: string;
8
+ targetLanguages: string[];
9
+ }
10
+ export declare type LocalizationFunction = (parameters: DeeplParameters) => Promise<string[]>;
11
+ export declare class LocalizationManager {
12
+ private localizationFn;
13
+ constructor(localizationFn?: typeof deepl);
14
+ localize(options: LocalizationOptions): Promise<void>;
15
+ protected translationFileName(original: string, language: string): string;
16
+ translateLanguage(source: Localization, target: Localization, targetLanguage: string, options: LocalizationOptions): Promise<void>;
17
+ protected 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
25
  //# sourceMappingURL=localization-manager.d.ts.map
@@ -1,149 +1,149 @@
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 !== options.targetLanguages.length) {
49
- console.log('Supported languages: ' + deepl_api_1.supportedLanguages.join(', '));
50
- }
51
- const existingTranslations = new Map();
52
- for (const targetLanguage of languages) {
53
- try {
54
- const targetPath = this.translationFileName(sourceFile, targetLanguage);
55
- existingTranslations.set(targetLanguage, await fs.readJson(targetPath));
56
- }
57
- catch {
58
- existingTranslations.set(targetLanguage, {});
59
- }
60
- }
61
- await Promise.all(languages.map(language => this.translateLanguage(source, existingTranslations.get(language), language, options)));
62
- for (const targetLanguage of languages) {
63
- const targetPath = this.translationFileName(sourceFile, targetLanguage);
64
- try {
65
- const translation = existingTranslations.get(targetLanguage);
66
- await fs.writeJson(targetPath, (0, common_1.sortLocalization)(translation), { spaces: 2 });
67
- }
68
- catch {
69
- console.error(chalk.red(`Error writing translated file to '${targetPath}'`));
70
- }
71
- }
72
- }
73
- translationFileName(original, language) {
74
- const directory = path.dirname(original);
75
- const fileName = path.basename(original, '.json');
76
- return path.join(directory, `${fileName}.${language.toLowerCase()}.json`);
77
- }
78
- async translateLanguage(source, target, targetLanguage, options) {
79
- var _a;
80
- const map = this.buildLocalizationMap(source, target);
81
- if (map.text.length > 0) {
82
- try {
83
- const translationResponse = await this.localizationFn({
84
- auth_key: options.authKey,
85
- free_api: options.freeApi,
86
- target_lang: targetLanguage.toUpperCase(),
87
- source_lang: (_a = options.sourceLanguage) === null || _a === void 0 ? void 0 : _a.toUpperCase(),
88
- text: map.text.map(e => this.addIgnoreTags(e)),
89
- tag_handling: ['xml'],
90
- ignore_tags: ['x']
91
- });
92
- translationResponse.translations.forEach(({ text }, i) => {
93
- map.localize(i, this.removeIgnoreTags(text));
94
- });
95
- console.log(chalk.green(`Successfully translated ${map.text.length} value${map.text.length > 1 ? 's' : ''} for language "${targetLanguage}"`));
96
- }
97
- catch (e) {
98
- console.log(chalk.red(`Could not translate into language "${targetLanguage}"`), e);
99
- }
100
- }
101
- else {
102
- console.log(`No translation necessary for language "${targetLanguage}"`);
103
- }
104
- }
105
- addIgnoreTags(text) {
106
- return text.replace(/(\{\d*\})/g, '<x>$1</x>');
107
- }
108
- removeIgnoreTags(text) {
109
- return text.replace(/<x>(\{\d+\})<\/x>/g, '$1');
110
- }
111
- buildLocalizationMap(source, target) {
112
- const functionMap = new Map();
113
- const text = [];
114
- const process = (s, t) => {
115
- // Delete all extra keys in the target translation first
116
- for (const key of Object.keys(t)) {
117
- if (!(key in s)) {
118
- delete t[key];
119
- }
120
- }
121
- for (const [key, value] of Object.entries(s)) {
122
- if (!(key in t)) {
123
- if (typeof value === 'string') {
124
- functionMap.set(text.length, translation => t[key] = translation);
125
- text.push(value);
126
- }
127
- else {
128
- const newLocalization = {};
129
- t[key] = newLocalization;
130
- process(value, newLocalization);
131
- }
132
- }
133
- else if (typeof value === 'object') {
134
- if (typeof t[key] === 'string') {
135
- t[key] = {};
136
- }
137
- process(value, t[key]);
138
- }
139
- }
140
- };
141
- process(source, target);
142
- return {
143
- text,
144
- localize: (index, value) => functionMap.get(index)(value)
145
- };
146
- }
147
- }
148
- exports.LocalizationManager = LocalizationManager;
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 !== options.targetLanguages.length) {
49
+ console.log('Supported languages: ' + deepl_api_1.supportedLanguages.join(', '));
50
+ }
51
+ const existingTranslations = new Map();
52
+ for (const targetLanguage of languages) {
53
+ try {
54
+ const targetPath = this.translationFileName(sourceFile, targetLanguage);
55
+ existingTranslations.set(targetLanguage, await fs.readJson(targetPath));
56
+ }
57
+ catch {
58
+ existingTranslations.set(targetLanguage, {});
59
+ }
60
+ }
61
+ await Promise.all(languages.map(language => this.translateLanguage(source, existingTranslations.get(language), language, options)));
62
+ for (const targetLanguage of languages) {
63
+ const targetPath = this.translationFileName(sourceFile, targetLanguage);
64
+ try {
65
+ const translation = existingTranslations.get(targetLanguage);
66
+ await fs.writeJson(targetPath, (0, common_1.sortLocalization)(translation), { spaces: 2 });
67
+ }
68
+ catch {
69
+ console.error(chalk.red(`Error writing translated file to '${targetPath}'`));
70
+ }
71
+ }
72
+ }
73
+ translationFileName(original, language) {
74
+ const directory = path.dirname(original);
75
+ const fileName = path.basename(original, '.json');
76
+ return path.join(directory, `${fileName}.${language.toLowerCase()}.json`);
77
+ }
78
+ async translateLanguage(source, target, targetLanguage, options) {
79
+ var _a;
80
+ const map = this.buildLocalizationMap(source, target);
81
+ if (map.text.length > 0) {
82
+ try {
83
+ const translationResponse = await this.localizationFn({
84
+ auth_key: options.authKey,
85
+ free_api: options.freeApi,
86
+ target_lang: targetLanguage.toUpperCase(),
87
+ source_lang: (_a = options.sourceLanguage) === null || _a === void 0 ? void 0 : _a.toUpperCase(),
88
+ text: map.text.map(e => this.addIgnoreTags(e)),
89
+ tag_handling: ['xml'],
90
+ ignore_tags: ['x']
91
+ });
92
+ translationResponse.translations.forEach(({ text }, i) => {
93
+ map.localize(i, this.removeIgnoreTags(text));
94
+ });
95
+ console.log(chalk.green(`Successfully translated ${map.text.length} value${map.text.length > 1 ? 's' : ''} for language "${targetLanguage}"`));
96
+ }
97
+ catch (e) {
98
+ console.log(chalk.red(`Could not translate into language "${targetLanguage}"`), e);
99
+ }
100
+ }
101
+ else {
102
+ console.log(`No translation necessary for language "${targetLanguage}"`);
103
+ }
104
+ }
105
+ addIgnoreTags(text) {
106
+ return text.replace(/(\{\d*\})/g, '<x>$1</x>');
107
+ }
108
+ removeIgnoreTags(text) {
109
+ return text.replace(/<x>(\{\d+\})<\/x>/g, '$1');
110
+ }
111
+ buildLocalizationMap(source, target) {
112
+ const functionMap = new Map();
113
+ const text = [];
114
+ const process = (s, t) => {
115
+ // Delete all extra keys in the target translation first
116
+ for (const key of Object.keys(t)) {
117
+ if (!(key in s)) {
118
+ delete t[key];
119
+ }
120
+ }
121
+ for (const [key, value] of Object.entries(s)) {
122
+ if (!(key in t)) {
123
+ if (typeof value === 'string') {
124
+ functionMap.set(text.length, translation => t[key] = translation);
125
+ text.push(value);
126
+ }
127
+ else {
128
+ const newLocalization = {};
129
+ t[key] = newLocalization;
130
+ process(value, newLocalization);
131
+ }
132
+ }
133
+ else if (typeof value === 'object') {
134
+ if (typeof t[key] === 'string') {
135
+ t[key] = {};
136
+ }
137
+ process(value, t[key]);
138
+ }
139
+ }
140
+ };
141
+ process(source, target);
142
+ return {
143
+ text,
144
+ localize: (index, value) => functionMap.get(index)(value)
145
+ };
146
+ }
147
+ }
148
+ exports.LocalizationManager = LocalizationManager;
149
149
  //# sourceMappingURL=localization-manager.js.map
@@ -1,2 +1,2 @@
1
- export {};
1
+ export {};
2
2
  //# sourceMappingURL=localization-manager.spec.d.ts.map