auto-translate-json-library 1.2.7 → 1.2.9
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/.vscode/settings.json +5 -0
- package/biome.json +19 -0
- package/build/package.json +68 -0
- package/build/src/README.MD +141 -0
- package/build/src/package.json +68 -0
- package/package.json +66 -66
- package/src/aws.ts +133 -0
- package/src/azure.ts +169 -0
- package/src/cli.ts +156 -0
- package/src/config.ts +40 -0
- package/src/deepl.ts +80 -0
- package/src/files.ts +82 -0
- package/src/folderFiles.ts +50 -0
- package/src/google.ts +62 -0
- package/src/index.ts +159 -0
- package/src/lib.ts +219 -0
- package/src/openai.ts +167 -0
- package/src/translate.interface.ts +13 -0
- package/src/util.ts +57 -0
- package/tests/translations/en.json +22 -0
- package/tests/translations/es.json +0 -0
- package/tests/translations/fr.json +21 -0
- package/tests/translations/sr.json +21 -0
- package/tests/translations-folder/de/common.json +9 -0
- package/tests/translations-folder/de/settings.json +3 -0
- package/tests/translations-folder/en/common.json +9 -0
- package/tests/translations-folder/en/settings.json +3 -0
- package/tsconfig.json +104 -0
- package/bin/cli.js +0 -2
- /package/{README.md → README.MD} +0 -0
- /package/{aws.d.ts → build/src/aws.d.ts} +0 -0
- /package/{aws.js → build/src/aws.js} +0 -0
- /package/{azure.d.ts → build/src/azure.d.ts} +0 -0
- /package/{azure.js → build/src/azure.js} +0 -0
- /package/{cli.d.ts → build/src/cli.d.ts} +0 -0
- /package/{cli.js → build/src/cli.js} +0 -0
- /package/{config.d.ts → build/src/config.d.ts} +0 -0
- /package/{config.js → build/src/config.js} +0 -0
- /package/{deepl.d.ts → build/src/deepl.d.ts} +0 -0
- /package/{deepl.js → build/src/deepl.js} +0 -0
- /package/{files.d.ts → build/src/files.d.ts} +0 -0
- /package/{files.js → build/src/files.js} +0 -0
- /package/{folderFiles.d.ts → build/src/folderFiles.d.ts} +0 -0
- /package/{folderFiles.js → build/src/folderFiles.js} +0 -0
- /package/{google.d.ts → build/src/google.d.ts} +0 -0
- /package/{google.js → build/src/google.js} +0 -0
- /package/{index.d.ts → build/src/index.d.ts} +0 -0
- /package/{index.js → build/src/index.js} +0 -0
- /package/{lib.d.ts → build/src/lib.d.ts} +0 -0
- /package/{lib.js → build/src/lib.js} +0 -0
- /package/{openai.d.ts → build/src/openai.d.ts} +0 -0
- /package/{openai.js → build/src/openai.js} +0 -0
- /package/{translate.interface.d.ts → build/src/translate.interface.d.ts} +0 -0
- /package/{translate.interface.js → build/src/translate.interface.js} +0 -0
- /package/{util.d.ts → build/src/util.d.ts} +0 -0
- /package/{util.js → build/src/util.js} +0 -0
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/cli.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import minimist from "minimist";
|
|
3
|
+
import c from "picocolors";
|
|
4
|
+
import { version } from "../package.json";
|
|
5
|
+
import { Configuration } from "./config";
|
|
6
|
+
import { translate } from "./lib";
|
|
7
|
+
|
|
8
|
+
require("dotenv").config();
|
|
9
|
+
|
|
10
|
+
const arguments_ = process.argv.slice(2);
|
|
11
|
+
const flags = minimist(arguments_, {
|
|
12
|
+
alias: {
|
|
13
|
+
mode: ["m"],
|
|
14
|
+
engine: ["e"],
|
|
15
|
+
keepTranslations: ["kt"],
|
|
16
|
+
keepExtraTranslations: ["ket"],
|
|
17
|
+
sourceLocale: ["s"],
|
|
18
|
+
},
|
|
19
|
+
string: ["mode", "engine", "sourceLocale"],
|
|
20
|
+
boolean: ["keepTranslations", "keepExtraTranslations"],
|
|
21
|
+
default: {
|
|
22
|
+
engine: "google",
|
|
23
|
+
sourceLocale: "en",
|
|
24
|
+
keepTranslations: true,
|
|
25
|
+
keepExtraTranslations: true,
|
|
26
|
+
mode: "file",
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
console.log(c.green(`🔨 Auto translate json cli v${version}`));
|
|
30
|
+
|
|
31
|
+
const inputPath = flags._[0];
|
|
32
|
+
const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations } =
|
|
33
|
+
flags;
|
|
34
|
+
|
|
35
|
+
const config: Configuration = {} as Configuration;
|
|
36
|
+
|
|
37
|
+
// set translate engine config
|
|
38
|
+
switch (engine) {
|
|
39
|
+
case "google": {
|
|
40
|
+
const googleApiKey = process.env.ATJ_GOOGLE_API_KEY;
|
|
41
|
+
if (!googleApiKey) {
|
|
42
|
+
console.error(
|
|
43
|
+
c.red(
|
|
44
|
+
"❌ Google api key not found in environment variable ATJ_GOOGLE_API_KEY",
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
config.translationKeyInfo = {
|
|
50
|
+
kind: "google",
|
|
51
|
+
apiKey: googleApiKey,
|
|
52
|
+
};
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case "aws": {
|
|
56
|
+
const awsAccessKeyId = process.env.ATJ_AWS_ACCESS_KEY_ID;
|
|
57
|
+
const awsSecretAccessKey = process.env.ATJ_AWS_SECRET_ACCESS_KEY;
|
|
58
|
+
const awsRegion = process.env.ATJ_AWS_REGION;
|
|
59
|
+
if (!awsAccessKeyId || !awsSecretAccessKey || !awsRegion) {
|
|
60
|
+
console.error(
|
|
61
|
+
c.red(
|
|
62
|
+
"❌ AWS credentials not found in environment variables ATJ_AWS_ACCESS_KEY_ID, ATJ_AWS_SECRET_ACCESS_KEY, ATJ_AWS_REGION",
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
config.translationKeyInfo = {
|
|
68
|
+
kind: "aws",
|
|
69
|
+
accessKeyId: awsAccessKeyId,
|
|
70
|
+
secretAccessKey: awsSecretAccessKey,
|
|
71
|
+
region: awsRegion,
|
|
72
|
+
};
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case "azure": {
|
|
76
|
+
const azureSecretKey = process.env.ATJ_AZURE_SECRET_KEY;
|
|
77
|
+
const azureRegion = process.env.ATJ_AZURE_REGION;
|
|
78
|
+
if (!azureSecretKey || !azureRegion) {
|
|
79
|
+
console.error(
|
|
80
|
+
c.red(
|
|
81
|
+
"❌ Azure credentials not found in environment variables ATJ_AZURE_SECRET_KEY, ATJ_AZURE_REGION",
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
}
|
|
86
|
+
config.translationKeyInfo = {
|
|
87
|
+
kind: "azure",
|
|
88
|
+
secretKey: azureSecretKey,
|
|
89
|
+
region: azureRegion,
|
|
90
|
+
};
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "openai": {
|
|
94
|
+
const openaiApiKey = process.env.ATJ_OPENAI_API_KEY;
|
|
95
|
+
if (!openaiApiKey) {
|
|
96
|
+
console.error(
|
|
97
|
+
c.red(
|
|
98
|
+
"❌ Openai api key not found in environment variable ATJ_OPENAI_API_KEY",
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
process.exit(1);
|
|
102
|
+
}
|
|
103
|
+
config.translationKeyInfo = {
|
|
104
|
+
kind: "openai",
|
|
105
|
+
apiKey: openaiApiKey,
|
|
106
|
+
};
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case "deepLPro": {
|
|
110
|
+
const deepLProSecretKey = process.env.ATJ_DEEPL_PRO_SECRET_KEY;
|
|
111
|
+
if (!deepLProSecretKey) {
|
|
112
|
+
console.log(
|
|
113
|
+
c.red(
|
|
114
|
+
"❌ DeepL pro api key not found in environment variable ATJ_DEEPL_PRO_SECRET_KEY",
|
|
115
|
+
),
|
|
116
|
+
);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
config.translationKeyInfo = {
|
|
120
|
+
kind: "deepLPro",
|
|
121
|
+
secretKey: deepLProSecretKey,
|
|
122
|
+
};
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case "deepLFree": {
|
|
126
|
+
const deepLFreeSecretKey = process.env.ATJ_DEEPL_FREE_SECRET_KEY;
|
|
127
|
+
if (!deepLFreeSecretKey) {
|
|
128
|
+
console.error(
|
|
129
|
+
c.red(
|
|
130
|
+
"❌ DeepL free api key not found in environment variable ATJ_DEEPL_FREE_SECRET_KEY",
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
process.exit(1);
|
|
134
|
+
}
|
|
135
|
+
config.translationKeyInfo = {
|
|
136
|
+
kind: "deepLFree",
|
|
137
|
+
secretKey: deepLFreeSecretKey,
|
|
138
|
+
};
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
default:
|
|
142
|
+
console.error(c.red(`❌ Engine ${engine} not supported`));
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
config.sourceLocale = sourceLocale;
|
|
147
|
+
config.keepTranslations = keepTranslations ? "keep" : "retranslate";
|
|
148
|
+
config.keepExtraTranslations = keepExtraTranslations ? "keep" : "remove";
|
|
149
|
+
config.mode = mode;
|
|
150
|
+
const sourcePath = path.join(process.cwd(), inputPath);
|
|
151
|
+
console.log(c.green(`🌐 Translating ${sourcePath}`));
|
|
152
|
+
|
|
153
|
+
translate(sourcePath, config).catch((error) => {
|
|
154
|
+
console.error(c.red("❌ Translate error:\n"), error);
|
|
155
|
+
process.exit(1);
|
|
156
|
+
});
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type Configuration = {
|
|
2
|
+
translationKeyInfo:
|
|
3
|
+
| GoogleTranslationKey
|
|
4
|
+
| AwsTranslationKey
|
|
5
|
+
| AzureTranslationKey
|
|
6
|
+
| DeepLProTranslationKey
|
|
7
|
+
| DeepLFreeTranslationKey
|
|
8
|
+
| OpenAITranslationKey;
|
|
9
|
+
startDelimiter: string;
|
|
10
|
+
endDelimiter: string;
|
|
11
|
+
mode: "file" | "folder";
|
|
12
|
+
sourceLocale: string;
|
|
13
|
+
keepTranslations: "keep" | "retranslate";
|
|
14
|
+
keepExtraTranslations: "keep" | "remove";
|
|
15
|
+
ignorePrefix: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type GoogleTranslationKey = { kind: "google"; apiKey: string };
|
|
19
|
+
type AwsTranslationKey = {
|
|
20
|
+
kind: "aws";
|
|
21
|
+
accessKeyId: string;
|
|
22
|
+
secretAccessKey: string;
|
|
23
|
+
region: string;
|
|
24
|
+
};
|
|
25
|
+
type AzureTranslationKey = {
|
|
26
|
+
kind: "azure";
|
|
27
|
+
secretKey: string;
|
|
28
|
+
region: string;
|
|
29
|
+
};
|
|
30
|
+
type DeepLProTranslationKey = {
|
|
31
|
+
kind: "deepLPro";
|
|
32
|
+
secretKey: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type DeepLFreeTranslationKey = {
|
|
36
|
+
kind: "deepLFree";
|
|
37
|
+
secretKey: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type OpenAITranslationKey = { kind: "openai"; apiKey: string };
|
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,82 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { TranslationFile } from "./translate.interface";
|
|
4
|
+
|
|
5
|
+
export interface IFiles {
|
|
6
|
+
sourceLocale: string;
|
|
7
|
+
targetLocales: Array<string>;
|
|
8
|
+
loadJsonFromLocale(locale: string): Promise<TranslationFile>;
|
|
9
|
+
saveJsonToLocale(locale: string, file: TranslationFile): void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const readFileAsync: (filename: string) => Promise<string> = (
|
|
13
|
+
filename: string,
|
|
14
|
+
) =>
|
|
15
|
+
new Promise((resolve, reject) => {
|
|
16
|
+
const exist = fs.existsSync(filename);
|
|
17
|
+
if (!exist) fs.writeFileSync(filename, "");
|
|
18
|
+
fs.readFile(filename, (error, data) => {
|
|
19
|
+
error ? reject(error) : resolve(data.toString());
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
export const loadJsonFromLocale: (
|
|
24
|
+
fileName: string,
|
|
25
|
+
) => Promise<TranslationFile> = async (fileName: string) => {
|
|
26
|
+
let data = await readFileAsync(fileName);
|
|
27
|
+
// handle empty files
|
|
28
|
+
if (!data) {
|
|
29
|
+
data = "{}";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return JSON.parse(data);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const saveJsonToLocale = (filename: string, file: TranslationFile) => {
|
|
36
|
+
const data = JSON.stringify(file, null, " ");
|
|
37
|
+
|
|
38
|
+
fs.writeFileSync(filename, data, "utf8");
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export class Files implements IFiles {
|
|
42
|
+
folderPath: string;
|
|
43
|
+
sourceLocale: string;
|
|
44
|
+
targetLocales: Array<string>;
|
|
45
|
+
|
|
46
|
+
constructor(filePath: string) {
|
|
47
|
+
this.folderPath = path.dirname(filePath);
|
|
48
|
+
const fileName = path.basename(filePath);
|
|
49
|
+
this.sourceLocale = this.getLocaleFromFilename(fileName);
|
|
50
|
+
this.targetLocales = this.getTargetLocales();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private getLocaleFromFilename(fileName: string): string {
|
|
54
|
+
return fileName.replace(".json", "");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private getTargetLocales(): string[] {
|
|
58
|
+
const locales = new Array();
|
|
59
|
+
|
|
60
|
+
const files = fs.readdirSync(this.folderPath);
|
|
61
|
+
|
|
62
|
+
files.forEach((file, _index, _array) => {
|
|
63
|
+
const locale = this.getLocaleFromFilename(file);
|
|
64
|
+
if (locale !== this.sourceLocale) {
|
|
65
|
+
locales.push(locale);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return locales;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
loadJsonFromLocale(locale: string): Promise<TranslationFile> {
|
|
73
|
+
const filename = `${this.folderPath}/${locale}.json`;
|
|
74
|
+
return loadJsonFromLocale(filename);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
saveJsonToLocale(locale: string, file: TranslationFile) {
|
|
78
|
+
const filename = `${this.folderPath}/${locale}.json`;
|
|
79
|
+
|
|
80
|
+
saveJsonToLocale(filename, file);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { IFiles, loadJsonFromLocale, saveJsonToLocale } from "./files";
|
|
4
|
+
import { TranslationFile } from "./translate.interface";
|
|
5
|
+
|
|
6
|
+
export class FolderFiles implements IFiles {
|
|
7
|
+
folderPath: string;
|
|
8
|
+
sourceLocale: string;
|
|
9
|
+
targetLocales: Array<string>;
|
|
10
|
+
fileName: string;
|
|
11
|
+
|
|
12
|
+
constructor(filePath: string) {
|
|
13
|
+
const localeDir = path.dirname(filePath);
|
|
14
|
+
this.folderPath = path.dirname(localeDir);
|
|
15
|
+
this.fileName = path.basename(filePath);
|
|
16
|
+
this.sourceLocale = path.basename(localeDir);
|
|
17
|
+
this.targetLocales = this.getTargetLocales();
|
|
18
|
+
|
|
19
|
+
console.log(
|
|
20
|
+
this.folderPath,
|
|
21
|
+
this.fileName,
|
|
22
|
+
this.sourceLocale,
|
|
23
|
+
this.targetLocales,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private getTargetLocales(): string[] {
|
|
28
|
+
const files = fs
|
|
29
|
+
.readdirSync(this.folderPath)
|
|
30
|
+
.map((folder, _index, _array) => path.basename(folder))
|
|
31
|
+
.map((locale, _index, _array) =>
|
|
32
|
+
locale !== this.sourceLocale ? locale : "",
|
|
33
|
+
)
|
|
34
|
+
.filter((x, _index, _array) => x); // do not want empty strings
|
|
35
|
+
|
|
36
|
+
return files;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private createFileName(locale: string): string {
|
|
40
|
+
return `${this.folderPath}/${locale}/${this.fileName}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
loadJsonFromLocale(locale: string) {
|
|
44
|
+
return loadJsonFromLocale(this.createFileName(locale));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
saveJsonToLocale(locale: string, file: TranslationFile) {
|
|
48
|
+
saveJsonToLocale(this.createFileName(locale), file);
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/google.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Translate } from "@google-cloud/translate/build/src/v2";
|
|
2
|
+
import { ITranslate } from "./translate.interface";
|
|
3
|
+
import { Util } from "./util";
|
|
4
|
+
|
|
5
|
+
export class GoogleTranslate implements ITranslate {
|
|
6
|
+
private constructor(
|
|
7
|
+
private googleTranslate: Translate,
|
|
8
|
+
private supportedLanguages: string[] = [],
|
|
9
|
+
) {}
|
|
10
|
+
|
|
11
|
+
static async initialize(apiKey: string): Promise<GoogleTranslate> {
|
|
12
|
+
const googleTranslate = new Translate({ key: apiKey });
|
|
13
|
+
const supportedLanguages = await GoogleTranslate.getSupportedLanguages(
|
|
14
|
+
googleTranslate,
|
|
15
|
+
);
|
|
16
|
+
return new GoogleTranslate(googleTranslate, supportedLanguages);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private static async getSupportedLanguages(
|
|
20
|
+
translate: Translate,
|
|
21
|
+
): Promise<string[]> {
|
|
22
|
+
const [languages] = await translate.getLanguages();
|
|
23
|
+
return languages.map((language, _index, _array) =>
|
|
24
|
+
language.code.toLowerCase(),
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
isValidLocale(targetLocale: string): boolean {
|
|
29
|
+
return this.supportedLanguages.includes(targetLocale);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async translateText(
|
|
33
|
+
text: string,
|
|
34
|
+
_sourceLocale: string,
|
|
35
|
+
targetLocale: string,
|
|
36
|
+
): Promise<string> {
|
|
37
|
+
let args;
|
|
38
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
39
|
+
|
|
40
|
+
let result = "";
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const response = await this.googleTranslate.translate(text, targetLocale);
|
|
44
|
+
result = response[0];
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (error instanceof Error) {
|
|
47
|
+
let message = error.message;
|
|
48
|
+
if (error.message === "Invalid Value") {
|
|
49
|
+
message = `Invalid Locale ${targetLocale}`;
|
|
50
|
+
}
|
|
51
|
+
console.error(message);
|
|
52
|
+
return message;
|
|
53
|
+
}
|
|
54
|
+
return "error";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// replace arguments with numbers
|
|
58
|
+
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
59
|
+
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
}
|