auto-translate-json-library 1.2.9 → 1.3.1
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/build/package.json +5 -5
- package/build/src/bin/cli.d.ts +2 -0
- package/build/src/{cli.js → bin/cli.js} +27 -3
- package/package.json +5 -5
- package/.vscode/settings.json +0 -5
- package/biome.json +0 -19
- package/build/src/README.MD +0 -141
- package/build/src/cli.d.ts +0 -1
- package/build/src/package.json +0 -68
- package/src/aws.ts +0 -133
- package/src/azure.ts +0 -169
- package/src/cli.ts +0 -156
- package/src/config.ts +0 -40
- package/src/deepl.ts +0 -80
- package/src/files.ts +0 -82
- package/src/folderFiles.ts +0 -50
- package/src/google.ts +0 -62
- package/src/index.ts +0 -159
- package/src/lib.ts +0 -219
- package/src/openai.ts +0 -167
- package/src/translate.interface.ts +0 -13
- package/src/util.ts +0 -57
- package/tests/translations/en.json +0 -22
- package/tests/translations/es.json +0 -0
- package/tests/translations/fr.json +0 -21
- package/tests/translations/sr.json +0 -21
- package/tests/translations-folder/de/common.json +0 -9
- package/tests/translations-folder/de/settings.json +0 -3
- package/tests/translations-folder/en/common.json +0 -9
- package/tests/translations-folder/en/settings.json +0 -3
- package/tsconfig.json +0 -104
package/build/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-translate-json-library",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
|
|
5
5
|
"main": "./build/src/index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"
|
|
7
|
+
"auto-translate-json-library": "./build/src/bin/cli.js",
|
|
8
|
+
"atj": "./build/src/bin/cli.js"
|
|
8
9
|
},
|
|
9
10
|
"types": "./build/src/index.d.ts",
|
|
10
11
|
"scripts": {
|
|
@@ -15,8 +16,7 @@
|
|
|
15
16
|
"lint-fix-all": "biome check --apply-unsafe ./src",
|
|
16
17
|
"format": "biome format ./src",
|
|
17
18
|
"format-fix": "biome format ./src --write",
|
|
18
|
-
"start": "ts-node src/index.ts"
|
|
19
|
-
"publish": "npm run build "
|
|
19
|
+
"start": "ts-node src/index.ts"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
22
22
|
"translate",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@biomejs/biome": "1.2.2",
|
|
50
50
|
"@topce/typescript": "5.3.1",
|
|
51
51
|
"@types/minimist": "1.2.3",
|
|
52
|
-
"@types/node": "20.
|
|
52
|
+
"@types/node": "20.8.0",
|
|
53
53
|
"@types/yargs": "17.0.26",
|
|
54
54
|
"ts-node": "10.9.1"
|
|
55
55
|
},
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
"use strict";
|
|
2
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
@@ -6,10 +7,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
7
|
const path_1 = __importDefault(require("path"));
|
|
7
8
|
const minimist_1 = __importDefault(require("minimist"));
|
|
8
9
|
const picocolors_1 = __importDefault(require("picocolors"));
|
|
9
|
-
const package_json_1 = require("
|
|
10
|
-
const lib_1 = require("
|
|
10
|
+
const package_json_1 = require("../../package.json");
|
|
11
|
+
const lib_1 = require("../lib");
|
|
11
12
|
require("dotenv").config();
|
|
13
|
+
// Define a function to display the help message
|
|
14
|
+
function displayHelp() {
|
|
15
|
+
console.log(`🔨 Auto translate json cli v${package_json_1.version}`);
|
|
16
|
+
console.log(`Usage: atj [options] <inputPath>`);
|
|
17
|
+
console.log("");
|
|
18
|
+
console.log("Options:");
|
|
19
|
+
console.log(" --help, -h Display this help message");
|
|
20
|
+
console.log(" --mode, -m <mode> Specify the translation mode (file, folder, etc.)");
|
|
21
|
+
console.log(" --engine, -e <engine> Specify the translation engine (google, aws, etc.)");
|
|
22
|
+
console.log(" --sourceLocale, -s <locale> Specify the source locale");
|
|
23
|
+
console.log(" --keepTranslations, --no-keepTranslations Keep or retranslate existing translations");
|
|
24
|
+
console.log(" --keepExtraTranslations, --no-keepExtraTranslations Keep or remove extra translations");
|
|
25
|
+
console.log("");
|
|
26
|
+
}
|
|
12
27
|
const arguments_ = process.argv.slice(2);
|
|
28
|
+
if (arguments_.length === 0) {
|
|
29
|
+
displayHelp();
|
|
30
|
+
process.exit(0);
|
|
31
|
+
}
|
|
13
32
|
const flags = (0, minimist_1.default)(arguments_, {
|
|
14
33
|
alias: {
|
|
15
34
|
mode: ["m"],
|
|
@@ -17,9 +36,10 @@ const flags = (0, minimist_1.default)(arguments_, {
|
|
|
17
36
|
keepTranslations: ["kt"],
|
|
18
37
|
keepExtraTranslations: ["ket"],
|
|
19
38
|
sourceLocale: ["s"],
|
|
39
|
+
help: ["h"],
|
|
20
40
|
},
|
|
21
41
|
string: ["mode", "engine", "sourceLocale"],
|
|
22
|
-
boolean: ["keepTranslations", "keepExtraTranslations"],
|
|
42
|
+
boolean: ["keepTranslations", "keepExtraTranslations", "help"],
|
|
23
43
|
default: {
|
|
24
44
|
engine: "google",
|
|
25
45
|
sourceLocale: "en",
|
|
@@ -29,6 +49,10 @@ const flags = (0, minimist_1.default)(arguments_, {
|
|
|
29
49
|
},
|
|
30
50
|
});
|
|
31
51
|
console.log(picocolors_1.default.green(`🔨 Auto translate json cli v${package_json_1.version}`));
|
|
52
|
+
if (flags.help) {
|
|
53
|
+
displayHelp();
|
|
54
|
+
process.exit(0);
|
|
55
|
+
}
|
|
32
56
|
const inputPath = flags._[0];
|
|
33
57
|
const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations } = flags;
|
|
34
58
|
const config = {};
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-translate-json-library",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
|
|
5
5
|
"main": "./build/src/index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"
|
|
7
|
+
"auto-translate-json-library": "./build/src/bin/cli.js",
|
|
8
|
+
"atj": "./build/src/bin/cli.js"
|
|
8
9
|
},
|
|
9
10
|
"types": "./build/src/index.d.ts",
|
|
10
11
|
"scripts": {
|
|
@@ -15,8 +16,7 @@
|
|
|
15
16
|
"lint-fix-all": "biome check --apply-unsafe ./src",
|
|
16
17
|
"format": "biome format ./src",
|
|
17
18
|
"format-fix": "biome format ./src --write",
|
|
18
|
-
"start": "ts-node src/index.ts"
|
|
19
|
-
"publish": "npm run build "
|
|
19
|
+
"start": "ts-node src/index.ts"
|
|
20
20
|
},
|
|
21
21
|
"keywords": [
|
|
22
22
|
"translate",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@biomejs/biome": "1.2.2",
|
|
50
50
|
"@topce/typescript": "5.3.1",
|
|
51
51
|
"@types/minimist": "1.2.3",
|
|
52
|
-
"@types/node": "20.
|
|
52
|
+
"@types/node": "20.8.0",
|
|
53
53
|
"@types/yargs": "17.0.26",
|
|
54
54
|
"ts-node": "10.9.1"
|
|
55
55
|
},
|
package/.vscode/settings.json
DELETED
package/biome.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"formatter": {
|
|
3
|
-
"indentStyle": "space"
|
|
4
|
-
},
|
|
5
|
-
"linter": {
|
|
6
|
-
"enabled": true,
|
|
7
|
-
"rules": {
|
|
8
|
-
"recommended": true,
|
|
9
|
-
"suspicious": {
|
|
10
|
-
"noExplicitAny": "error"
|
|
11
|
-
},
|
|
12
|
-
"style": {
|
|
13
|
-
"useConst": "error",
|
|
14
|
-
"noParameterAssign": "off",
|
|
15
|
-
"useTemplate":"error"
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
package/build/src/README.MD
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
# About project
|
|
2
|
-
|
|
3
|
-
This project is used in, (and refactored from) excellent vs code extension
|
|
4
|
-
Auto Translate JSON
|
|
5
|
-
<https://marketplace.visualstudio.com/items?itemName=JeffJorczak.auto-translate-json>
|
|
6
|
-
|
|
7
|
-
It open posibility to use Auto Taransation JSON
|
|
8
|
-
not just in vs code but as a library and command line interface
|
|
9
|
-
|
|
10
|
-
# Use as library
|
|
11
|
-
|
|
12
|
-
```shell
|
|
13
|
-
npm i auto-translate-json-library
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
```Typescript
|
|
17
|
-
import { translate, Configuration } from 'auto-translate-json-library';
|
|
18
|
-
|
|
19
|
-
let config: Configuration = {} as Configuration;
|
|
20
|
-
|
|
21
|
-
// set google api key
|
|
22
|
-
config.translationKeyInfo = {
|
|
23
|
-
kind: 'google',
|
|
24
|
-
apiKey: googleApiKey
|
|
25
|
-
};
|
|
26
|
-
const pivotTranslation="./en.json";
|
|
27
|
-
await translate(pivotTranslation, config) ;
|
|
28
|
-
// translation for example fr.json in same folder as pivotTranslation should contain the translations
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
# Use as command line tool
|
|
32
|
-
|
|
33
|
-
first install it
|
|
34
|
-
|
|
35
|
-
```shell
|
|
36
|
-
npm i auto-translate-json-library
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
do not forget to set translation engine parameters in environment variables or .env file
|
|
40
|
-
|
|
41
|
-
```shell
|
|
42
|
-
|
|
43
|
-
```shell
|
|
44
|
-
node node_modules\auto-translate-json-library\index.js --pivotTranslation=./translations/en.json
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
if you clone repo you can use the following commands
|
|
48
|
-
|
|
49
|
-
```shell
|
|
50
|
-
npm i
|
|
51
|
-
npm run build
|
|
52
|
-
node .\build\src\index.js --pivotTranslation=./tests/translations/en.json
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
```
|
|
56
|
-
# CLI PARAMETERS
|
|
57
|
-
|
|
58
|
-
mode,
|
|
59
|
-
file or folder,
|
|
60
|
-
default is file
|
|
61
|
-
|
|
62
|
-
startDelimiter,
|
|
63
|
-
start delimiter,
|
|
64
|
-
default is {
|
|
65
|
-
|
|
66
|
-
endDelimiter,
|
|
67
|
-
end delimiter,
|
|
68
|
-
default is }
|
|
69
|
-
|
|
70
|
-
keepExtraTranslations,
|
|
71
|
-
keep extra translations (not present in pivot translation),
|
|
72
|
-
default is keep,
|
|
73
|
-
it could be keep or remove
|
|
74
|
-
|
|
75
|
-
keepTranslations,
|
|
76
|
-
keep existing translations,
|
|
77
|
-
default is keep
|
|
78
|
-
it could be keep or retranslate
|
|
79
|
-
|
|
80
|
-
sourceLocale
|
|
81
|
-
source locale
|
|
82
|
-
it could be en or fr ...
|
|
83
|
-
if specified it must match with pivot translation
|
|
84
|
-
|
|
85
|
-
pivotTranslation
|
|
86
|
-
pivot translation file that will be used as source for translations
|
|
87
|
-
|
|
88
|
-
ignorePrefix
|
|
89
|
-
ignore translation that key starts with ignore prefix
|
|
90
|
-
|
|
91
|
-
# ENVIRONMENT VARIABLES
|
|
92
|
-
|
|
93
|
-
google parameters
|
|
94
|
-
ATJ_GOOGLE_API_KEY=
|
|
95
|
-
|
|
96
|
-
aws parameters
|
|
97
|
-
ATJ_AWS_ACCESS_KEY_ID=
|
|
98
|
-
ATJ_AWS_SECRET_ACCESS_KEY=
|
|
99
|
-
ATJ_AWS_REGION=
|
|
100
|
-
|
|
101
|
-
azure parameters
|
|
102
|
-
ATJ_AZURE_SECRET_KEY=
|
|
103
|
-
ATJ_AZURE_REGION=
|
|
104
|
-
|
|
105
|
-
deepl pro parameters
|
|
106
|
-
ATJ_DEEPL_PRO_SECRET_KEY=
|
|
107
|
-
deepl free parameters
|
|
108
|
-
ATJ_DEEPL_FREE_SECRET_KEY=
|
|
109
|
-
|
|
110
|
-
OpenAI parameters
|
|
111
|
-
ATJ_OPEN_AI_SECRET_KEY=
|
|
112
|
-
|
|
113
|
-
start delimiter
|
|
114
|
-
ATJ_START_DELIMITER={{
|
|
115
|
-
end delimiter
|
|
116
|
-
ATJ_END_DELIMITER=}}
|
|
117
|
-
|
|
118
|
-
file or folder
|
|
119
|
-
ATJ_MODE=file
|
|
120
|
-
|
|
121
|
-
source locale
|
|
122
|
-
ATJ_SOURCE_LOCALE=en
|
|
123
|
-
|
|
124
|
-
keep or retranslate existing translations
|
|
125
|
-
ATJ_KEEP_TRANSLATIONS=keep
|
|
126
|
-
|
|
127
|
-
keep or remove extra translations
|
|
128
|
-
ATJ_KEEP_EXTRA_TRANSLATIONS=keep
|
|
129
|
-
|
|
130
|
-
ignore prefix translation keys to ignore
|
|
131
|
-
ATJ_IGNORE_PREFIX=
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
You can also use .env file to store environment variables
|
|
135
|
-
|
|
136
|
-
There is also improved version of CLI for example:
|
|
137
|
-
|
|
138
|
-
```shell
|
|
139
|
-
atj tests/translations/en.json -e google -m file
|
|
140
|
-
```
|
|
141
|
-
since version 1.2.5
|
package/build/src/cli.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/build/src/package.json
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "auto-translate-json-library",
|
|
3
|
-
"version": "1.2.8",
|
|
4
|
-
"description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
|
|
5
|
-
"main": "./index.js",
|
|
6
|
-
"bin": {
|
|
7
|
-
"atj": "./cli.js"
|
|
8
|
-
},
|
|
9
|
-
"types": "./index.d.ts",
|
|
10
|
-
"scripts": {
|
|
11
|
-
"prepare": "tsc",
|
|
12
|
-
"build": "tsc",
|
|
13
|
-
"lint": "biome check ./src",
|
|
14
|
-
"lint-fix": "biome check ./src --apply",
|
|
15
|
-
"lint-fix-all": "biome check --apply-unsafe ./src",
|
|
16
|
-
"format": "biome format ./src",
|
|
17
|
-
"format-fix": "biome format ./src --write",
|
|
18
|
-
"start": "ts-node src/index.ts",
|
|
19
|
-
"publish": "npm run build "
|
|
20
|
-
},
|
|
21
|
-
"keywords": [
|
|
22
|
-
"translate",
|
|
23
|
-
"language",
|
|
24
|
-
"json",
|
|
25
|
-
"flutter",
|
|
26
|
-
"angular",
|
|
27
|
-
"ngx-translate",
|
|
28
|
-
"transloco",
|
|
29
|
-
"auto-translate-json",
|
|
30
|
-
"google",
|
|
31
|
-
"Azure",
|
|
32
|
-
"DeepL",
|
|
33
|
-
"OpenAI"
|
|
34
|
-
],
|
|
35
|
-
"author": "codegrue",
|
|
36
|
-
"repository": {
|
|
37
|
-
"type": "git",
|
|
38
|
-
"url": "https://github.com/topce/auto-translate-json-library"
|
|
39
|
-
},
|
|
40
|
-
"contributors": [
|
|
41
|
-
"codegrue",
|
|
42
|
-
"gajo357",
|
|
43
|
-
"topce",
|
|
44
|
-
"BenGMiles",
|
|
45
|
-
"stonega"
|
|
46
|
-
],
|
|
47
|
-
"license": "MIT",
|
|
48
|
-
"devDependencies": {
|
|
49
|
-
"@biomejs/biome": "1.2.2",
|
|
50
|
-
"@topce/typescript": "5.3.1",
|
|
51
|
-
"@types/minimist": "1.2.3",
|
|
52
|
-
"@types/node": "20.7.2",
|
|
53
|
-
"@types/yargs": "17.0.26",
|
|
54
|
-
"ts-node": "10.9.1"
|
|
55
|
-
},
|
|
56
|
-
"dependencies": {
|
|
57
|
-
"@aws-sdk/client-translate": "3.421.0",
|
|
58
|
-
"@google-cloud/translate": "8.0.2",
|
|
59
|
-
"axios": "1.5.1",
|
|
60
|
-
"dotenv": "16.3.1",
|
|
61
|
-
"form-data": "4.0.0",
|
|
62
|
-
"minimist": "1.2.8",
|
|
63
|
-
"openai": "4.11.0",
|
|
64
|
-
"picocolors": "1.0.0",
|
|
65
|
-
"uuid": "9.0.1",
|
|
66
|
-
"yargs": "17.7.2"
|
|
67
|
-
}
|
|
68
|
-
}
|
package/src/aws.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
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.error(error);
|
|
126
|
-
return "";
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
130
|
-
|
|
131
|
-
return result;
|
|
132
|
-
}
|
|
133
|
-
}
|
package/src/azure.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
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
|
-
*/
|