auto-translate-json-library 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +36 -0
- package/src/aws.ts +133 -0
- package/src/azure.ts +169 -0
- package/src/config.ts +37 -0
- package/src/deepl.ts +80 -0
- package/src/files.ts +79 -0
- package/src/folderFiles.ts +47 -0
- package/src/google.ts +162 -0
- package/src/index.ts +93 -0
- package/src/lib.ts +204 -0
- package/src/translate.interface.ts +8 -0
- package/src/util.ts +57 -0
- package/test/en.json +3 -0
- package/test/fr.json +3 -0
- package/test/sr.json +3 -0
- package/tsconfig.json +103 -0
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "auto-translate-json-library",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"prepare": "tsc",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"start": "ts-node src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"husky": {
|
|
15
|
+
"hooks": {
|
|
16
|
+
"pre-commit": "pretty-quick --staged"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^14.18.21",
|
|
21
|
+
"husky": "^8.0.1",
|
|
22
|
+
"prettier": "^2.7.1",
|
|
23
|
+
"pretty-quick": "^3.1.3",
|
|
24
|
+
"ts-node": "^10.8.1",
|
|
25
|
+
"typescript": "^4.7.4"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@aws-sdk/client-translate": "^3.118.0",
|
|
29
|
+
"@google-cloud/translate": "^6.3.1",
|
|
30
|
+
"axios": "^0.27.2",
|
|
31
|
+
"dotenv": "^16.0.1",
|
|
32
|
+
"form-data": "^4.0.0",
|
|
33
|
+
"uuid": "^8.3.2",
|
|
34
|
+
"yazl": "^2.5.1"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/aws.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { ITranslate } from './translate.interface';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Translate,
|
|
5
|
+
TranslateTextCommandInput
|
|
6
|
+
} from '@aws-sdk/client-translate';
|
|
7
|
+
import { Util } from './util';
|
|
8
|
+
|
|
9
|
+
const supportedLanguages = [
|
|
10
|
+
'af',
|
|
11
|
+
'sq',
|
|
12
|
+
'am',
|
|
13
|
+
'ar',
|
|
14
|
+
'hy',
|
|
15
|
+
'az',
|
|
16
|
+
'bn',
|
|
17
|
+
'bs',
|
|
18
|
+
'bg',
|
|
19
|
+
'ca',
|
|
20
|
+
'zh',
|
|
21
|
+
'zh-TW',
|
|
22
|
+
'hr',
|
|
23
|
+
'cs',
|
|
24
|
+
'da',
|
|
25
|
+
'fa-AF',
|
|
26
|
+
'nl',
|
|
27
|
+
'en',
|
|
28
|
+
'et',
|
|
29
|
+
'fa',
|
|
30
|
+
'tl',
|
|
31
|
+
'fi',
|
|
32
|
+
'fr',
|
|
33
|
+
'fr-CA',
|
|
34
|
+
'ka',
|
|
35
|
+
'de',
|
|
36
|
+
'el',
|
|
37
|
+
'gu',
|
|
38
|
+
'ht',
|
|
39
|
+
'ha',
|
|
40
|
+
'he',
|
|
41
|
+
'hi',
|
|
42
|
+
'hu',
|
|
43
|
+
'is',
|
|
44
|
+
'id',
|
|
45
|
+
'ga',
|
|
46
|
+
'it',
|
|
47
|
+
'ja',
|
|
48
|
+
'kn',
|
|
49
|
+
'kk',
|
|
50
|
+
'ko',
|
|
51
|
+
'lv',
|
|
52
|
+
'lt',
|
|
53
|
+
'mk',
|
|
54
|
+
'ms',
|
|
55
|
+
'ml',
|
|
56
|
+
'mt',
|
|
57
|
+
'mr',
|
|
58
|
+
'mn',
|
|
59
|
+
'no',
|
|
60
|
+
'ps',
|
|
61
|
+
'pl',
|
|
62
|
+
'pt',
|
|
63
|
+
'pt-PT',
|
|
64
|
+
'pa',
|
|
65
|
+
'ro',
|
|
66
|
+
'ru',
|
|
67
|
+
'sr',
|
|
68
|
+
'si',
|
|
69
|
+
'sk',
|
|
70
|
+
'sl',
|
|
71
|
+
'so',
|
|
72
|
+
'es',
|
|
73
|
+
'es-MX',
|
|
74
|
+
'sw',
|
|
75
|
+
'sv',
|
|
76
|
+
'ta',
|
|
77
|
+
'te',
|
|
78
|
+
'th',
|
|
79
|
+
'tr',
|
|
80
|
+
'uk',
|
|
81
|
+
'ur',
|
|
82
|
+
'uz',
|
|
83
|
+
'vi',
|
|
84
|
+
'cy'
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
export class AWSTranslate implements ITranslate {
|
|
88
|
+
private client: Translate;
|
|
89
|
+
constructor(
|
|
90
|
+
private accessKeyId: string,
|
|
91
|
+
private secretAccessKey: string,
|
|
92
|
+
private regions: string
|
|
93
|
+
) {
|
|
94
|
+
this.client = new Translate({
|
|
95
|
+
credentials: {
|
|
96
|
+
accessKeyId: this.accessKeyId,
|
|
97
|
+
secretAccessKey: this.secretAccessKey
|
|
98
|
+
},
|
|
99
|
+
region: this.regions
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
isValidLocale(targetLocale: string): boolean {
|
|
103
|
+
return supportedLanguages.includes(targetLocale);
|
|
104
|
+
}
|
|
105
|
+
async translateText(
|
|
106
|
+
text: string,
|
|
107
|
+
sourceLocale: string,
|
|
108
|
+
targetLocale: string
|
|
109
|
+
): Promise<string> {
|
|
110
|
+
let args;
|
|
111
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
112
|
+
|
|
113
|
+
let result = '';
|
|
114
|
+
|
|
115
|
+
const params: TranslateTextCommandInput = {
|
|
116
|
+
SourceLanguageCode: sourceLocale,
|
|
117
|
+
TargetLanguageCode: targetLocale,
|
|
118
|
+
Text: text
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
const translation = await this.client.translateText(params);
|
|
123
|
+
result = translation.TranslatedText as string;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.log(error);
|
|
126
|
+
return '';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
130
|
+
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
}
|
package/src/azure.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { ITranslate } from './translate.interface';
|
|
2
|
+
import { Util } from './util';
|
|
3
|
+
|
|
4
|
+
const axios = require('axios').default;
|
|
5
|
+
const { v4: uuidv4 } = require('uuid');
|
|
6
|
+
|
|
7
|
+
const supportedLanguages = [
|
|
8
|
+
'af',
|
|
9
|
+
'sq',
|
|
10
|
+
'am',
|
|
11
|
+
'ar',
|
|
12
|
+
'hy',
|
|
13
|
+
'as',
|
|
14
|
+
'az',
|
|
15
|
+
'bn',
|
|
16
|
+
'ba',
|
|
17
|
+
'bs',
|
|
18
|
+
'bg',
|
|
19
|
+
'yue',
|
|
20
|
+
'ca',
|
|
21
|
+
'lzh',
|
|
22
|
+
'zh-Hans',
|
|
23
|
+
'zh-Hant',
|
|
24
|
+
'hr',
|
|
25
|
+
'cs',
|
|
26
|
+
'da',
|
|
27
|
+
'prs',
|
|
28
|
+
'dv',
|
|
29
|
+
'nl',
|
|
30
|
+
'en',
|
|
31
|
+
'et',
|
|
32
|
+
'fj',
|
|
33
|
+
'fil',
|
|
34
|
+
'fi',
|
|
35
|
+
'fr',
|
|
36
|
+
'fr-ca',
|
|
37
|
+
'ka',
|
|
38
|
+
'de',
|
|
39
|
+
'el',
|
|
40
|
+
'gu',
|
|
41
|
+
'ht',
|
|
42
|
+
'he',
|
|
43
|
+
'hi',
|
|
44
|
+
'mww',
|
|
45
|
+
'hu',
|
|
46
|
+
'is',
|
|
47
|
+
'id',
|
|
48
|
+
'iu',
|
|
49
|
+
'ga',
|
|
50
|
+
'it',
|
|
51
|
+
'ja',
|
|
52
|
+
'kn',
|
|
53
|
+
'kk',
|
|
54
|
+
'km',
|
|
55
|
+
'tlh-Latn',
|
|
56
|
+
'tlh-Piqd',
|
|
57
|
+
'ko',
|
|
58
|
+
'ku',
|
|
59
|
+
'kmr',
|
|
60
|
+
'ky',
|
|
61
|
+
'lo',
|
|
62
|
+
'lv',
|
|
63
|
+
'lt',
|
|
64
|
+
'mk',
|
|
65
|
+
'mg',
|
|
66
|
+
'ms',
|
|
67
|
+
'ml',
|
|
68
|
+
'mt',
|
|
69
|
+
'mi',
|
|
70
|
+
'mr',
|
|
71
|
+
'mn-Cyrl',
|
|
72
|
+
'mn-Mong',
|
|
73
|
+
'my',
|
|
74
|
+
'ne',
|
|
75
|
+
'nb',
|
|
76
|
+
'or',
|
|
77
|
+
'ps',
|
|
78
|
+
'fa',
|
|
79
|
+
'pl',
|
|
80
|
+
'pt',
|
|
81
|
+
'pt-pt',
|
|
82
|
+
'pa',
|
|
83
|
+
'otq',
|
|
84
|
+
'ro',
|
|
85
|
+
'ru',
|
|
86
|
+
'sm',
|
|
87
|
+
'sr-Cyrl',
|
|
88
|
+
'sr-Latn',
|
|
89
|
+
'sk',
|
|
90
|
+
'sl',
|
|
91
|
+
'es',
|
|
92
|
+
'sw',
|
|
93
|
+
'sv',
|
|
94
|
+
'ty',
|
|
95
|
+
'ta',
|
|
96
|
+
'tt',
|
|
97
|
+
'te',
|
|
98
|
+
'th',
|
|
99
|
+
'bo',
|
|
100
|
+
'ti',
|
|
101
|
+
'to',
|
|
102
|
+
'tr',
|
|
103
|
+
'tk',
|
|
104
|
+
'uk',
|
|
105
|
+
'ur',
|
|
106
|
+
'ug',
|
|
107
|
+
'uz',
|
|
108
|
+
'vi',
|
|
109
|
+
'cy',
|
|
110
|
+
'yua'
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
export class AzureTranslate implements ITranslate {
|
|
114
|
+
private endpoint = 'https://api.cognitive.microsofttranslator.com';
|
|
115
|
+
constructor(
|
|
116
|
+
private subscriptionKey: string,
|
|
117
|
+
private subscriptionRegion: string
|
|
118
|
+
) {}
|
|
119
|
+
isValidLocale(targetLocale: string): boolean {
|
|
120
|
+
return supportedLanguages.includes(targetLocale);
|
|
121
|
+
}
|
|
122
|
+
async translateText(
|
|
123
|
+
text: string,
|
|
124
|
+
sourceLocale: string,
|
|
125
|
+
targetLocale: string
|
|
126
|
+
): Promise<string> {
|
|
127
|
+
let args;
|
|
128
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
129
|
+
|
|
130
|
+
let result = '';
|
|
131
|
+
|
|
132
|
+
const response = await axios({
|
|
133
|
+
baseURL: this.endpoint,
|
|
134
|
+
url: '/translate',
|
|
135
|
+
method: 'post',
|
|
136
|
+
headers: {
|
|
137
|
+
'Ocp-Apim-Subscription-Key': this.subscriptionKey,
|
|
138
|
+
'Ocp-Apim-Subscription-Region': this.subscriptionRegion,
|
|
139
|
+
'Content-type': 'application/json',
|
|
140
|
+
'X-ClientTraceId': uuidv4().toString()
|
|
141
|
+
},
|
|
142
|
+
params: {
|
|
143
|
+
'api-version': '3.0',
|
|
144
|
+
from: sourceLocale,
|
|
145
|
+
to: [targetLocale]
|
|
146
|
+
},
|
|
147
|
+
data: [
|
|
148
|
+
{
|
|
149
|
+
text: text
|
|
150
|
+
}
|
|
151
|
+
],
|
|
152
|
+
responseType: 'json'
|
|
153
|
+
});
|
|
154
|
+
result = response.data[0].translations[0].text;
|
|
155
|
+
|
|
156
|
+
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
157
|
+
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-translator?tabs=nodejs#translate-text
|
|
163
|
+
|
|
164
|
+
// Add your location, also known as region. The default is global.
|
|
165
|
+
// This is required if using a Cognitive Services resource.
|
|
166
|
+
|
|
167
|
+
/*
|
|
168
|
+
|
|
169
|
+
*/
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type Configuration = {
|
|
2
|
+
translationKeyInfo:
|
|
3
|
+
| GoogleTranslationKey
|
|
4
|
+
| AwsTranslationKey
|
|
5
|
+
| AzureTranslationKey
|
|
6
|
+
| DeepLProTranslationKey
|
|
7
|
+
| DeepLFreeTranslationKey;
|
|
8
|
+
startDelimiter: string;
|
|
9
|
+
endDelimiter: string;
|
|
10
|
+
mode: 'file' | 'folder';
|
|
11
|
+
sourceLocale: string;
|
|
12
|
+
keepTranslations: 'keep' | 'retranslate';
|
|
13
|
+
keepExtraTranslations: 'keep' | 'remove';
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type GoogleTranslationKey = { kind: 'google'; apiKey: string };
|
|
17
|
+
type AwsTranslationKey = {
|
|
18
|
+
kind: 'aws';
|
|
19
|
+
accessKeyId: string;
|
|
20
|
+
secretAccessKey: string;
|
|
21
|
+
region: string;
|
|
22
|
+
};
|
|
23
|
+
type AzureTranslationKey = {
|
|
24
|
+
kind: 'azure';
|
|
25
|
+
secretKey: string;
|
|
26
|
+
region: string;
|
|
27
|
+
};
|
|
28
|
+
type DeepLProTranslationKey = {
|
|
29
|
+
kind: 'deepLPro';
|
|
30
|
+
secretKey: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type DeepLFreeTranslationKey = {
|
|
34
|
+
kind: 'deepLFree';
|
|
35
|
+
secretKey: string;
|
|
36
|
+
};
|
|
37
|
+
|
package/src/deepl.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import FormData = require('form-data');
|
|
2
|
+
import { ITranslate } from './translate.interface';
|
|
3
|
+
|
|
4
|
+
const axios = require('axios').default;
|
|
5
|
+
import { Util } from './util';
|
|
6
|
+
|
|
7
|
+
const supportedLanguages = [
|
|
8
|
+
'BG',
|
|
9
|
+
'CS',
|
|
10
|
+
'DA',
|
|
11
|
+
'DE',
|
|
12
|
+
'EL',
|
|
13
|
+
'EN-GB',
|
|
14
|
+
'EN-US',
|
|
15
|
+
'EN',
|
|
16
|
+
'ES',
|
|
17
|
+
'ET',
|
|
18
|
+
'FI',
|
|
19
|
+
'FR',
|
|
20
|
+
'HU',
|
|
21
|
+
'IT',
|
|
22
|
+
'JA',
|
|
23
|
+
'LT',
|
|
24
|
+
'LV',
|
|
25
|
+
'NL',
|
|
26
|
+
'PL',
|
|
27
|
+
'PT-PT',
|
|
28
|
+
'PT-BR',
|
|
29
|
+
'PT',
|
|
30
|
+
'RO',
|
|
31
|
+
'RU',
|
|
32
|
+
'SK',
|
|
33
|
+
'SL',
|
|
34
|
+
'SV',
|
|
35
|
+
'ZH'
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
export class DeepLTranslate implements ITranslate {
|
|
39
|
+
private endpoint = 'https://api.deepl.com';
|
|
40
|
+
constructor(private subscriptionKey: string, private type: 'free' | 'pro') {
|
|
41
|
+
if (this.type === 'free') {
|
|
42
|
+
this.endpoint = 'https://api.free.deepl.com';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
isValidLocale(targetLocale: string): boolean {
|
|
46
|
+
return supportedLanguages.includes(targetLocale.toUpperCase());
|
|
47
|
+
}
|
|
48
|
+
async translateText(
|
|
49
|
+
text: string,
|
|
50
|
+
sourceLocale: string,
|
|
51
|
+
targetLocale: string
|
|
52
|
+
): Promise<string> {
|
|
53
|
+
let args;
|
|
54
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
55
|
+
|
|
56
|
+
let result = '';
|
|
57
|
+
|
|
58
|
+
const formData = new FormData();
|
|
59
|
+
formData.append('auth_key', this.subscriptionKey);
|
|
60
|
+
formData.append('text', text);
|
|
61
|
+
formData.append('source_lang', sourceLocale.toUpperCase());
|
|
62
|
+
formData.append('target_lang', targetLocale.toUpperCase());
|
|
63
|
+
|
|
64
|
+
const response = await axios({
|
|
65
|
+
baseURL: this.endpoint,
|
|
66
|
+
url: 'v2/translate',
|
|
67
|
+
method: 'post',
|
|
68
|
+
data: formData,
|
|
69
|
+
responseType: 'json'
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
result = response.translations[1].text;
|
|
73
|
+
|
|
74
|
+
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
75
|
+
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// https://www.deepl.com/docs-api/translating-text/example/
|
package/src/files.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
|
|
4
|
+
export interface IFiles {
|
|
5
|
+
sourceLocale: string;
|
|
6
|
+
targetLocales: Array<string>;
|
|
7
|
+
loadJsonFromLocale(locale: string): Promise<any>;
|
|
8
|
+
saveJsonToLocale(locale: string, file: any): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const readFileAsync: (filename: string) => Promise<string> = (
|
|
12
|
+
filename: string
|
|
13
|
+
) =>
|
|
14
|
+
new Promise((resolve, reject) => {
|
|
15
|
+
fs.readFile(filename, (error, data) => {
|
|
16
|
+
error ? reject(error) : resolve(data.toString());
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const loadJsonFromLocale: (fileName: string) => Promise<any> = async (
|
|
21
|
+
fileName: string
|
|
22
|
+
) => {
|
|
23
|
+
let data = await readFileAsync(fileName);
|
|
24
|
+
// handle empty files
|
|
25
|
+
if (!data) {
|
|
26
|
+
data = '{}';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return JSON.parse(data);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const saveJsonToLocale = (filename: string, file: any) => {
|
|
33
|
+
let data = JSON.stringify(file, null, ' ');
|
|
34
|
+
|
|
35
|
+
fs.writeFileSync(filename, data, 'utf8');
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export class Files implements IFiles {
|
|
39
|
+
folderPath: string;
|
|
40
|
+
sourceLocale: string;
|
|
41
|
+
targetLocales: Array<string>;
|
|
42
|
+
|
|
43
|
+
constructor(filePath: string) {
|
|
44
|
+
this.folderPath = path.dirname(filePath);
|
|
45
|
+
let fileName = path.basename(filePath);
|
|
46
|
+
this.sourceLocale = this.getLocaleFromFilename(fileName);
|
|
47
|
+
this.targetLocales = this.getTargetLocales();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private getLocaleFromFilename(fileName: string): string {
|
|
51
|
+
return fileName.replace('.json', '');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private getTargetLocales(): string[] {
|
|
55
|
+
let locales = new Array();
|
|
56
|
+
|
|
57
|
+
let files = fs.readdirSync(this.folderPath);
|
|
58
|
+
|
|
59
|
+
files.forEach((file) => {
|
|
60
|
+
let locale = this.getLocaleFromFilename(file);
|
|
61
|
+
if (locale !== this.sourceLocale) {
|
|
62
|
+
locales.push(locale);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return locales;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
loadJsonFromLocale(locale: string): Promise<any> {
|
|
70
|
+
let filename = this.folderPath + '/' + locale + '.json';
|
|
71
|
+
return loadJsonFromLocale(filename);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
saveJsonToLocale(locale: string, file: any) {
|
|
75
|
+
let filename = this.folderPath + '/' + locale + '.json';
|
|
76
|
+
|
|
77
|
+
saveJsonToLocale(filename, file);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import { IFiles, loadJsonFromLocale, saveJsonToLocale } from './files';
|
|
4
|
+
|
|
5
|
+
export class FolderFiles implements IFiles {
|
|
6
|
+
folderPath: string;
|
|
7
|
+
sourceLocale: string;
|
|
8
|
+
targetLocales: Array<string>;
|
|
9
|
+
fileName: string;
|
|
10
|
+
|
|
11
|
+
constructor(filePath: string) {
|
|
12
|
+
const localeDir = path.dirname(filePath);
|
|
13
|
+
this.folderPath = path.dirname(localeDir);
|
|
14
|
+
this.fileName = path.basename(filePath);
|
|
15
|
+
this.sourceLocale = path.basename(localeDir);
|
|
16
|
+
this.targetLocales = this.getTargetLocales();
|
|
17
|
+
|
|
18
|
+
console.log(
|
|
19
|
+
this.folderPath,
|
|
20
|
+
this.fileName,
|
|
21
|
+
this.sourceLocale,
|
|
22
|
+
this.targetLocales
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private getTargetLocales(): string[] {
|
|
27
|
+
let files = fs
|
|
28
|
+
.readdirSync(this.folderPath)
|
|
29
|
+
.map((folder) => path.basename(folder))
|
|
30
|
+
.map((locale) => (locale !== this.sourceLocale ? locale : ''))
|
|
31
|
+
.filter((x) => x); // do not want empty strings
|
|
32
|
+
|
|
33
|
+
return files;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private createFileName(locale: string): string {
|
|
37
|
+
return `${this.folderPath}/${locale}/${this.fileName}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
loadJsonFromLocale(locale: string) {
|
|
41
|
+
return loadJsonFromLocale(this.createFileName(locale));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
saveJsonToLocale(locale: string, file: any) {
|
|
45
|
+
saveJsonToLocale(this.createFileName(locale), file);
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/google.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { ITranslate } from './translate.interface';
|
|
2
|
+
|
|
3
|
+
//const {Translate} = require('@google-cloud/translate').v2;
|
|
4
|
+
import { Translate } from '@google-cloud/translate/build/src/v2';
|
|
5
|
+
import { Util } from './util';
|
|
6
|
+
const supportedLanguages = [
|
|
7
|
+
'af',
|
|
8
|
+
'sq',
|
|
9
|
+
'am',
|
|
10
|
+
'ar',
|
|
11
|
+
'hy',
|
|
12
|
+
'az',
|
|
13
|
+
'eu',
|
|
14
|
+
'be',
|
|
15
|
+
'bn',
|
|
16
|
+
'bs',
|
|
17
|
+
'bg',
|
|
18
|
+
'ca',
|
|
19
|
+
'ceb',
|
|
20
|
+
'zh-CN',
|
|
21
|
+
'zh',
|
|
22
|
+
'zh-TW',
|
|
23
|
+
'co',
|
|
24
|
+
'hr',
|
|
25
|
+
'cs',
|
|
26
|
+
'da',
|
|
27
|
+
'nl',
|
|
28
|
+
'en',
|
|
29
|
+
'eo',
|
|
30
|
+
'et',
|
|
31
|
+
'fi',
|
|
32
|
+
'fr',
|
|
33
|
+
'fy',
|
|
34
|
+
'gl',
|
|
35
|
+
'ka',
|
|
36
|
+
'de',
|
|
37
|
+
'el',
|
|
38
|
+
'gu',
|
|
39
|
+
'ht',
|
|
40
|
+
'ha',
|
|
41
|
+
'haw',
|
|
42
|
+
'he',
|
|
43
|
+
'iw',
|
|
44
|
+
'hi',
|
|
45
|
+
'hmn',
|
|
46
|
+
'hu',
|
|
47
|
+
'is',
|
|
48
|
+
'ig',
|
|
49
|
+
'id',
|
|
50
|
+
'ga',
|
|
51
|
+
'it',
|
|
52
|
+
'ja',
|
|
53
|
+
'jv',
|
|
54
|
+
'kn',
|
|
55
|
+
'kk',
|
|
56
|
+
'km',
|
|
57
|
+
'rw',
|
|
58
|
+
'ko',
|
|
59
|
+
'ku',
|
|
60
|
+
'ky',
|
|
61
|
+
'lo',
|
|
62
|
+
'lv',
|
|
63
|
+
'lt',
|
|
64
|
+
'lb',
|
|
65
|
+
'mk',
|
|
66
|
+
'mg',
|
|
67
|
+
'ms',
|
|
68
|
+
'ml',
|
|
69
|
+
'mt',
|
|
70
|
+
'mi',
|
|
71
|
+
'mr',
|
|
72
|
+
'mn',
|
|
73
|
+
'my',
|
|
74
|
+
'ne',
|
|
75
|
+
'no',
|
|
76
|
+
'ny',
|
|
77
|
+
'or',
|
|
78
|
+
'ps',
|
|
79
|
+
'fa',
|
|
80
|
+
'pl',
|
|
81
|
+
'pt',
|
|
82
|
+
'pa',
|
|
83
|
+
'ro',
|
|
84
|
+
'ru',
|
|
85
|
+
'sm',
|
|
86
|
+
'gd',
|
|
87
|
+
'sr',
|
|
88
|
+
'st',
|
|
89
|
+
'sn',
|
|
90
|
+
'sd',
|
|
91
|
+
'si',
|
|
92
|
+
'sk',
|
|
93
|
+
'sl',
|
|
94
|
+
'so',
|
|
95
|
+
'es',
|
|
96
|
+
'su',
|
|
97
|
+
'sw',
|
|
98
|
+
'sv',
|
|
99
|
+
'tl',
|
|
100
|
+
'tg',
|
|
101
|
+
'ta',
|
|
102
|
+
'tt',
|
|
103
|
+
'te',
|
|
104
|
+
'th',
|
|
105
|
+
'tr',
|
|
106
|
+
'tk',
|
|
107
|
+
'uk',
|
|
108
|
+
'ur',
|
|
109
|
+
'ug',
|
|
110
|
+
'uz',
|
|
111
|
+
'vi',
|
|
112
|
+
'cy',
|
|
113
|
+
'xh',
|
|
114
|
+
'yi',
|
|
115
|
+
'yo',
|
|
116
|
+
'zu'
|
|
117
|
+
];
|
|
118
|
+
export class GoogleTranslate implements ITranslate {
|
|
119
|
+
private apikey: string;
|
|
120
|
+
private googleTranslate: Translate;
|
|
121
|
+
|
|
122
|
+
constructor(apikey: string) {
|
|
123
|
+
this.apikey = apikey;
|
|
124
|
+
this.googleTranslate = new Translate({ key: this.apikey });
|
|
125
|
+
// this.googleTranslate.getSupportedLanguages();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
isValidLocale(targetLocale: string): boolean {
|
|
129
|
+
return supportedLanguages.includes(targetLocale);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async translateText(
|
|
133
|
+
text: string,
|
|
134
|
+
_sourceLocale: string,
|
|
135
|
+
targetLocale: string
|
|
136
|
+
): Promise<string> {
|
|
137
|
+
let args;
|
|
138
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
139
|
+
|
|
140
|
+
let result = '';
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
const response = await this.googleTranslate.translate(text, targetLocale);
|
|
144
|
+
result = response[0];
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof Error) {
|
|
147
|
+
let message = error.message;
|
|
148
|
+
if (error.message === 'Invalid Value') {
|
|
149
|
+
message = 'Invalid Locale ' + targetLocale;
|
|
150
|
+
}
|
|
151
|
+
console.log(message);
|
|
152
|
+
return message;
|
|
153
|
+
}
|
|
154
|
+
return 'error';
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// replace arguments with numbers
|
|
158
|
+
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
159
|
+
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Configuration } from './config';
|
|
2
|
+
import { translate } from './lib';
|
|
3
|
+
|
|
4
|
+
let config: Configuration = {} as Configuration;
|
|
5
|
+
require('dotenv').config();
|
|
6
|
+
if (process.env.ATJ_GOOGLE_API_KEY) {
|
|
7
|
+
config.translationKeyInfo = {
|
|
8
|
+
kind: 'google',
|
|
9
|
+
apiKey: process.env.ATJ_GOOGLE_API_KEY,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
if (
|
|
13
|
+
process.env.ATJ_AWS_ACCESS_KEY_ID &&
|
|
14
|
+
process.env.ATJ_AWS_SECRET_ACCESS_KEY &&
|
|
15
|
+
process.env.ATJ_AWS_REGION
|
|
16
|
+
) {
|
|
17
|
+
config.translationKeyInfo = {
|
|
18
|
+
kind: 'aws',
|
|
19
|
+
accessKeyId: process.env.ATJ_AWS_ACCESS_KEY_ID,
|
|
20
|
+
secretAccessKey: process.env.ATJ_AWS_SECRET_ACCESS_KEY,
|
|
21
|
+
region: process.env.ATJ_AWS_REGION,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (process.env.ATJ_AZURE_SECRET_KEY && process.env.ATJ_AZURE_REGION) {
|
|
25
|
+
config.translationKeyInfo = {
|
|
26
|
+
kind: 'azure',
|
|
27
|
+
secretKey: process.env.ATJ_AZURE_SECRET_KEY,
|
|
28
|
+
region: process.env.ATJ_AZURE_REGION,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (process.env.ATJ_DEEPL_PRO_SECRET_KEY) {
|
|
33
|
+
config.translationKeyInfo = {
|
|
34
|
+
kind: 'deepLPro',
|
|
35
|
+
secretKey: process.env.ATJ_DEEPL_PRO_SECRET_KEY,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (process.env.ATJ_DEEPL_FREE_SECRET_KEY) {
|
|
40
|
+
config.translationKeyInfo = {
|
|
41
|
+
kind: 'deepLFree',
|
|
42
|
+
secretKey: process.env.ATJ_DEEPL_FREE_SECRET_KEY,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (process.env.ATJ_START_DELIMITER) {
|
|
47
|
+
config.startDelimiter = process.env.ATJ_START_DELIMITER;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (process.env.ATJ_END_DELIMITER) {
|
|
51
|
+
config.endDelimiter = process.env.ATJ_END_DELIMITER;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (process.env.ATJ_END_DELIMITER) {
|
|
55
|
+
config.endDelimiter = process.env.ATJ_END_DELIMITER;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (
|
|
59
|
+
process.env.ATJ_MODE &&
|
|
60
|
+
(process.env.ATJ_MODE === 'file' || process.env.ATJ_MODE === 'folder')
|
|
61
|
+
) {
|
|
62
|
+
config.mode = process.env.ATJ_MODE;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (process.env.ATJ_SOURCE_LOCALE) {
|
|
66
|
+
config.sourceLocale = process.env.ATJ_SOURCE_LOCALE;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (
|
|
70
|
+
process.env.ATJ_KEEP_TRANSLATIONS &&
|
|
71
|
+
(process.env.ATJ_KEEP_TRANSLATIONS === 'keep' ||
|
|
72
|
+
process.env.ATJ_KEEP_TRANSLATIONS === 'retranslate')
|
|
73
|
+
) {
|
|
74
|
+
config.keepTranslations = process.env.ATJ_KEEP_TRANSLATIONS;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (
|
|
78
|
+
process.env.ATJ_KEEP_EXTRA_TRANSLATIONS &&
|
|
79
|
+
(process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === 'keep' ||
|
|
80
|
+
process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === 'remove')
|
|
81
|
+
) {
|
|
82
|
+
config.keepExtraTranslations = process.env.ATJ_KEEP_EXTRA_TRANSLATIONS;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
console.log(config);
|
|
86
|
+
var args = process.argv.slice(2);
|
|
87
|
+
console.log(args);
|
|
88
|
+
console.log(__dirname);
|
|
89
|
+
|
|
90
|
+
console.log(process.cwd());
|
|
91
|
+
if (args.length > 0) {
|
|
92
|
+
translate(args[0], config);
|
|
93
|
+
}
|
package/src/lib.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// The module 'vscode' contains the VS Code extensibility API
|
|
2
|
+
// Import the module and reference it with the alias vscode in your code below
|
|
3
|
+
import { Files, IFiles } from './files';
|
|
4
|
+
import { FolderFiles } from './folderFiles';
|
|
5
|
+
|
|
6
|
+
import { AWSTranslate } from './aws';
|
|
7
|
+
import { AzureTranslate } from './azure';
|
|
8
|
+
import { GoogleTranslate } from './google';
|
|
9
|
+
import { ITranslate } from './translate.interface';
|
|
10
|
+
import { Util } from './util';
|
|
11
|
+
import { DeepLTranslate } from './deepl';
|
|
12
|
+
import { Configuration } from './config';
|
|
13
|
+
|
|
14
|
+
const NAME = 'AutoTranslateJSON';
|
|
15
|
+
|
|
16
|
+
export async function translate(sourceFile: string ,config: Configuration ): Promise<void> {
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
// set the delimiters for named arguments
|
|
20
|
+
const startDelimiter = config.startDelimiter || '{';
|
|
21
|
+
if (startDelimiter) {
|
|
22
|
+
Util.startDelimiter = startDelimiter;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const endDelimiter = config.endDelimiter || '}';
|
|
26
|
+
if (endDelimiter) {
|
|
27
|
+
Util.endDelimiter = endDelimiter;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let translateEngine: ITranslate;
|
|
31
|
+
|
|
32
|
+
if (config.translationKeyInfo.kind === 'google') {
|
|
33
|
+
translateEngine = new GoogleTranslate(config.translationKeyInfo.apiKey);
|
|
34
|
+
} else if (config.translationKeyInfo.kind === 'aws') {
|
|
35
|
+
translateEngine = new AWSTranslate(
|
|
36
|
+
config.translationKeyInfo.accessKeyId,
|
|
37
|
+
config.translationKeyInfo.secretAccessKey,
|
|
38
|
+
config.translationKeyInfo.region
|
|
39
|
+
);
|
|
40
|
+
} else if (config.translationKeyInfo.kind==='azure') {
|
|
41
|
+
translateEngine = new AzureTranslate(config.translationKeyInfo.secretKey, config.translationKeyInfo.region);
|
|
42
|
+
} else if (config.translationKeyInfo.kind==='deepLFree') {
|
|
43
|
+
translateEngine = new DeepLTranslate(config.translationKeyInfo.secretKey, 'free');
|
|
44
|
+
} else if (config.translationKeyInfo.kind==='deepLPro') {
|
|
45
|
+
translateEngine = new DeepLTranslate(config.translationKeyInfo.secretKey, 'pro');
|
|
46
|
+
} else {
|
|
47
|
+
console.warn(
|
|
48
|
+
'You must provide a Google, AWS or Azure parameters first in the extension settings.'
|
|
49
|
+
);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
const fileMode =config.mode;
|
|
55
|
+
const files = readFiles(sourceFile, fileMode);
|
|
56
|
+
if (files === null) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// enforce source locale if provided in settings
|
|
61
|
+
if (config.sourceLocale !== files.sourceLocale) {
|
|
62
|
+
console.log(
|
|
63
|
+
'You must use the ' +
|
|
64
|
+
config.sourceLocale +
|
|
65
|
+
'.json file due to your Source Locale setting.'
|
|
66
|
+
);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const keepTranslations = config.keepTranslations === 'keep';
|
|
71
|
+
const keepExtras = config.keepExtraTranslations === 'keep';
|
|
72
|
+
|
|
73
|
+
// load source JSON
|
|
74
|
+
let source: any;
|
|
75
|
+
try {
|
|
76
|
+
source = await files.loadJsonFromLocale(files.sourceLocale);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error instanceof Error) {
|
|
79
|
+
console.log(error, 'Source file malformed');
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Iterate target Locales
|
|
85
|
+
files.targetLocales.forEach(async (targetLocale) => {
|
|
86
|
+
try {
|
|
87
|
+
const isValid = await translateEngine.isValidLocale(targetLocale);
|
|
88
|
+
if (!isValid) {
|
|
89
|
+
throw Error(targetLocale + ' is not supported. Skipping.');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const targetOriginal = await files.loadJsonFromLocale(targetLocale);
|
|
93
|
+
|
|
94
|
+
// Iterate source terms
|
|
95
|
+
const targetNew = await recurseNode(
|
|
96
|
+
source,
|
|
97
|
+
targetOriginal,
|
|
98
|
+
keepTranslations,
|
|
99
|
+
keepExtras,
|
|
100
|
+
files.sourceLocale,
|
|
101
|
+
targetLocale,
|
|
102
|
+
translateEngine
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// save target
|
|
106
|
+
files.saveJsonToLocale(targetLocale, targetNew);
|
|
107
|
+
|
|
108
|
+
const feedback = "Translated locale '" + targetLocale + "'";
|
|
109
|
+
console.log(feedback);
|
|
110
|
+
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error instanceof Error) {
|
|
113
|
+
console.error(error);
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
const readFiles: (
|
|
122
|
+
filePath: string,
|
|
123
|
+
mode: 'file' | 'folder'
|
|
124
|
+
) => IFiles | null = (filePath: string, mode: string) => {
|
|
125
|
+
try {
|
|
126
|
+
const files: IFiles =
|
|
127
|
+
mode === 'file' ? new Files(filePath) : new FolderFiles(filePath);
|
|
128
|
+
|
|
129
|
+
// log locale info
|
|
130
|
+
console.log('Source locale = ' + files.sourceLocale);
|
|
131
|
+
console.log('Target locales = ' + files.targetLocales);
|
|
132
|
+
|
|
133
|
+
return files;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error instanceof Error) {
|
|
136
|
+
console.error(error);
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
async function recurseNode(
|
|
143
|
+
source: any,
|
|
144
|
+
original: any,
|
|
145
|
+
keepTranslations: boolean | null,
|
|
146
|
+
keepExtras: boolean | null,
|
|
147
|
+
sourceLocale: string,
|
|
148
|
+
locale: string,
|
|
149
|
+
translateEngine: ITranslate,
|
|
150
|
+
isArray: boolean = false
|
|
151
|
+
): Promise<any> {
|
|
152
|
+
const destination: any = isArray ? [] : {};
|
|
153
|
+
|
|
154
|
+
// defaults
|
|
155
|
+
if (keepTranslations === null) {
|
|
156
|
+
keepTranslations = true;
|
|
157
|
+
}
|
|
158
|
+
if (keepExtras === null) {
|
|
159
|
+
keepExtras = true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const term in source) {
|
|
163
|
+
const node = source[term];
|
|
164
|
+
|
|
165
|
+
if (node instanceof Object && node !== null) {
|
|
166
|
+
destination[term] = await recurseNode(
|
|
167
|
+
node,
|
|
168
|
+
original[term] ?? {},
|
|
169
|
+
keepTranslations,
|
|
170
|
+
keepExtras,
|
|
171
|
+
sourceLocale,
|
|
172
|
+
locale,
|
|
173
|
+
translateEngine,
|
|
174
|
+
node instanceof Array
|
|
175
|
+
);
|
|
176
|
+
} else {
|
|
177
|
+
// if we already have a translation, keep it
|
|
178
|
+
if (keepTranslations && original[term]) {
|
|
179
|
+
destination[term] = original[term];
|
|
180
|
+
} else if (typeof node === 'number' || typeof node === 'boolean') {
|
|
181
|
+
// numbers and booleans do not need translations
|
|
182
|
+
destination[term] = node;
|
|
183
|
+
} else {
|
|
184
|
+
const translation = await translateEngine
|
|
185
|
+
.translateText(node, sourceLocale, locale)
|
|
186
|
+
.catch((err) =>console.error(err));
|
|
187
|
+
destination[term] = translation;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (keepExtras) {
|
|
193
|
+
// add back in any terms that were not in source
|
|
194
|
+
for (const term in original) {
|
|
195
|
+
if (!destination[term]) {
|
|
196
|
+
destination[term] = original[term];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return destination;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export class Util {
|
|
2
|
+
private static _startDelimiter = '{';
|
|
3
|
+
private static _endDelimiter = '}';
|
|
4
|
+
private static pattern = new RegExp(
|
|
5
|
+
Util._startDelimiter + '(.*?)' + Util._endDelimiter,
|
|
6
|
+
'g'
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
public static set startDelimiter(value: string) {
|
|
10
|
+
Util._startDelimiter = value;
|
|
11
|
+
// update regex
|
|
12
|
+
Util.pattern = new RegExp(
|
|
13
|
+
Util._startDelimiter + '(.*?)' + Util._endDelimiter,
|
|
14
|
+
'g'
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public static set endDelimiter(value: string) {
|
|
19
|
+
Util._endDelimiter = value;
|
|
20
|
+
// update regex
|
|
21
|
+
Util.pattern = new RegExp(
|
|
22
|
+
Util._startDelimiter + '(.*?)' + Util._endDelimiter,
|
|
23
|
+
'g'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
public static replaceArgumentsWithNumbers(
|
|
27
|
+
args: RegExpMatchArray | null,
|
|
28
|
+
result: string
|
|
29
|
+
) {
|
|
30
|
+
if (args) {
|
|
31
|
+
let i = 0;
|
|
32
|
+
for (let arg of args) {
|
|
33
|
+
result = result.replace(
|
|
34
|
+
Util._startDelimiter + i + Util._endDelimiter,
|
|
35
|
+
arg
|
|
36
|
+
);
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public static replaceContextVariables(text: string) {
|
|
44
|
+
|
|
45
|
+
const args = text.match(Util.pattern);
|
|
46
|
+
|
|
47
|
+
// replace arguments with numbers
|
|
48
|
+
if (args) {
|
|
49
|
+
let i = 0;
|
|
50
|
+
for (let arg of args) {
|
|
51
|
+
text = text.replace(arg, Util._startDelimiter + i + Util._endDelimiter);
|
|
52
|
+
i++;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { args, text };
|
|
56
|
+
}
|
|
57
|
+
}
|
package/test/en.json
ADDED
package/test/fr.json
ADDED
package/test/sr.json
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
39
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
40
|
+
|
|
41
|
+
/* JavaScript Support */
|
|
42
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
43
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
44
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
45
|
+
|
|
46
|
+
/* Emit */
|
|
47
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
48
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
49
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
50
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
51
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
52
|
+
"outDir": "build", /* Specify an output folder for all emitted files. */
|
|
53
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
54
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
55
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
56
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
57
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
58
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
59
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
60
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
61
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
62
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
63
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
64
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
65
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
66
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
67
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
68
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
69
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
70
|
+
|
|
71
|
+
/* Interop Constraints */
|
|
72
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
73
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
74
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
75
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
76
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
77
|
+
|
|
78
|
+
/* Type Checking */
|
|
79
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
80
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
81
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
82
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
83
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
84
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
85
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
86
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
87
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
88
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
89
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
90
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
91
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
92
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
93
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
94
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
95
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
96
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
97
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
98
|
+
|
|
99
|
+
/* Completeness */
|
|
100
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
101
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
102
|
+
}
|
|
103
|
+
}
|