auto-translate-json-library 1.0.2 → 1.0.5

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/README.MD ADDED
@@ -0,0 +1,113 @@
1
+ # Use as library
2
+
3
+ ```shell
4
+ npm i auto-translate-json-library
5
+ ```
6
+
7
+ ```Typescript
8
+ import { translate, Configuration } from 'auto-translate-json-library';
9
+
10
+ let config: Configuration = {} as Configuration;
11
+
12
+ // set google api key
13
+ config.translationKeyInfo = {
14
+ kind: 'google',
15
+ apiKey: googleApiKey
16
+ };
17
+ const pivotTranslation="./en.json";
18
+ await translate(pivotTranslation, config) ;
19
+ // translation for example fr.json in same folder as pivotTranslation should contain the translations
20
+ ```
21
+
22
+ # Use as command line tool
23
+
24
+ first install it
25
+
26
+ ```shell
27
+ npm i auto-translate-json-library
28
+ ```
29
+
30
+ do not forget to set translation engine parameters in environment variables or .env file
31
+
32
+ ```shell
33
+
34
+ ```shell
35
+ node node_modules\auto-translate-json-library\index.js --pivotTranslation=./translations/en.json
36
+ ```
37
+
38
+ if you clone repo you can use the following command
39
+
40
+ ```shell
41
+ npm start --pivotTranslation=./test/en.json
42
+ ```
43
+
44
+ ```
45
+ # CLI PARAMETERS
46
+
47
+ mode,
48
+ file or folder,
49
+ default is file
50
+
51
+ startDelimiter,
52
+ start delimiter,
53
+ default is {
54
+
55
+ endDelimiter,
56
+ end delimiter,
57
+ default is }
58
+
59
+ keepExtraTranslations,
60
+ keep extra translations (not present in pivot translation),
61
+ default is keep,
62
+ it could be keep or remove
63
+
64
+ keepTranslations,
65
+ keep existing translations,
66
+ default is keep
67
+ it could be keep or retranslate
68
+
69
+ sourceLocale
70
+ source locale
71
+ it could be en or fr ...
72
+ if specified it must match with pivot translation
73
+
74
+ pivotTranslation
75
+ pivot translation file that will be used as source for translations
76
+
77
+ # ENVIRONMENT VARIABLES
78
+
79
+ google parameters
80
+ ATJ_GOOGLE_API_KEY=
81
+
82
+ aws parameters
83
+ ATJ_AWS_ACCESS_KEY_ID=
84
+ ATJ_AWS_SECRET_ACCESS_KEY=
85
+ ATJ_AWS_REGION=
86
+
87
+ azure parameters
88
+ ATJ_AZURE_SECRET_KEY=
89
+ ATJ_AZURE_REGION=
90
+
91
+ deepl pro parameters
92
+ ATJ_DEEPL_PRO_SECRET_KEY=
93
+ deepl free parameters
94
+ ATJ_DEEPL_FREE_SECRET_KEY=
95
+
96
+ start delimiter
97
+ ATJ_START_DELIMITER={{
98
+ end delimiter
99
+ ATJ_END_DELIMITER=}}
100
+
101
+ file or folder
102
+ ATJ_MODE=file
103
+
104
+ source locale
105
+ ATJ_SOURCE_LOCALE=en
106
+
107
+ keep or retranslate existing translations
108
+ ATJ_KEEP_TRANSLATIONS=keep
109
+
110
+ keep or remove extra translations
111
+ ATJ_KEEP_EXTRA_TRANSLATIONS=keep
112
+
113
+ You can also use .env file to store environment variables
package/aws.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { ITranslate } from './translate.interface';
2
+ export declare class AWSTranslate implements ITranslate {
3
+ private accessKeyId;
4
+ private secretAccessKey;
5
+ private regions;
6
+ private client;
7
+ constructor(accessKeyId: string, secretAccessKey: string, regions: string);
8
+ isValidLocale(targetLocale: string): boolean;
9
+ translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
10
+ }
package/aws.js ADDED
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AWSTranslate = void 0;
13
+ const client_translate_1 = require("@aws-sdk/client-translate");
14
+ const util_1 = require("./util");
15
+ const supportedLanguages = [
16
+ 'af',
17
+ 'sq',
18
+ 'am',
19
+ 'ar',
20
+ 'hy',
21
+ 'az',
22
+ 'bn',
23
+ 'bs',
24
+ 'bg',
25
+ 'ca',
26
+ 'zh',
27
+ 'zh-TW',
28
+ 'hr',
29
+ 'cs',
30
+ 'da',
31
+ 'fa-AF',
32
+ 'nl',
33
+ 'en',
34
+ 'et',
35
+ 'fa',
36
+ 'tl',
37
+ 'fi',
38
+ 'fr',
39
+ 'fr-CA',
40
+ 'ka',
41
+ 'de',
42
+ 'el',
43
+ 'gu',
44
+ 'ht',
45
+ 'ha',
46
+ 'he',
47
+ 'hi',
48
+ 'hu',
49
+ 'is',
50
+ 'id',
51
+ 'ga',
52
+ 'it',
53
+ 'ja',
54
+ 'kn',
55
+ 'kk',
56
+ 'ko',
57
+ 'lv',
58
+ 'lt',
59
+ 'mk',
60
+ 'ms',
61
+ 'ml',
62
+ 'mt',
63
+ 'mr',
64
+ 'mn',
65
+ 'no',
66
+ 'ps',
67
+ 'pl',
68
+ 'pt',
69
+ 'pt-PT',
70
+ 'pa',
71
+ 'ro',
72
+ 'ru',
73
+ 'sr',
74
+ 'si',
75
+ 'sk',
76
+ 'sl',
77
+ 'so',
78
+ 'es',
79
+ 'es-MX',
80
+ 'sw',
81
+ 'sv',
82
+ 'ta',
83
+ 'te',
84
+ 'th',
85
+ 'tr',
86
+ 'uk',
87
+ 'ur',
88
+ 'uz',
89
+ 'vi',
90
+ 'cy'
91
+ ];
92
+ class AWSTranslate {
93
+ constructor(accessKeyId, secretAccessKey, regions) {
94
+ this.accessKeyId = accessKeyId;
95
+ this.secretAccessKey = secretAccessKey;
96
+ this.regions = regions;
97
+ this.client = new client_translate_1.Translate({
98
+ credentials: {
99
+ accessKeyId: this.accessKeyId,
100
+ secretAccessKey: this.secretAccessKey
101
+ },
102
+ region: this.regions
103
+ });
104
+ }
105
+ isValidLocale(targetLocale) {
106
+ return supportedLanguages.includes(targetLocale);
107
+ }
108
+ translateText(text, sourceLocale, targetLocale) {
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ let args;
111
+ ({ args, text } = util_1.Util.replaceContextVariables(text));
112
+ let result = '';
113
+ const params = {
114
+ SourceLanguageCode: sourceLocale,
115
+ TargetLanguageCode: targetLocale,
116
+ Text: text
117
+ };
118
+ try {
119
+ const translation = yield this.client.translateText(params);
120
+ result = translation.TranslatedText;
121
+ }
122
+ catch (error) {
123
+ console.log(error);
124
+ return '';
125
+ }
126
+ result = util_1.Util.replaceArgumentsWithNumbers(args, result);
127
+ return result;
128
+ });
129
+ }
130
+ }
131
+ exports.AWSTranslate = AWSTranslate;
package/azure.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { ITranslate } from './translate.interface';
2
+ export declare class AzureTranslate implements ITranslate {
3
+ private subscriptionKey;
4
+ private subscriptionRegion;
5
+ private endpoint;
6
+ constructor(subscriptionKey: string, subscriptionRegion: string);
7
+ isValidLocale(targetLocale: string): boolean;
8
+ translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
9
+ }
package/azure.js ADDED
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AzureTranslate = void 0;
13
+ const util_1 = require("./util");
14
+ const axios = require('axios').default;
15
+ const { v4: uuidv4 } = require('uuid');
16
+ const supportedLanguages = [
17
+ 'af',
18
+ 'sq',
19
+ 'am',
20
+ 'ar',
21
+ 'hy',
22
+ 'as',
23
+ 'az',
24
+ 'bn',
25
+ 'ba',
26
+ 'bs',
27
+ 'bg',
28
+ 'yue',
29
+ 'ca',
30
+ 'lzh',
31
+ 'zh-Hans',
32
+ 'zh-Hant',
33
+ 'hr',
34
+ 'cs',
35
+ 'da',
36
+ 'prs',
37
+ 'dv',
38
+ 'nl',
39
+ 'en',
40
+ 'et',
41
+ 'fj',
42
+ 'fil',
43
+ 'fi',
44
+ 'fr',
45
+ 'fr-ca',
46
+ 'ka',
47
+ 'de',
48
+ 'el',
49
+ 'gu',
50
+ 'ht',
51
+ 'he',
52
+ 'hi',
53
+ 'mww',
54
+ 'hu',
55
+ 'is',
56
+ 'id',
57
+ 'iu',
58
+ 'ga',
59
+ 'it',
60
+ 'ja',
61
+ 'kn',
62
+ 'kk',
63
+ 'km',
64
+ 'tlh-Latn',
65
+ 'tlh-Piqd',
66
+ 'ko',
67
+ 'ku',
68
+ 'kmr',
69
+ 'ky',
70
+ 'lo',
71
+ 'lv',
72
+ 'lt',
73
+ 'mk',
74
+ 'mg',
75
+ 'ms',
76
+ 'ml',
77
+ 'mt',
78
+ 'mi',
79
+ 'mr',
80
+ 'mn-Cyrl',
81
+ 'mn-Mong',
82
+ 'my',
83
+ 'ne',
84
+ 'nb',
85
+ 'or',
86
+ 'ps',
87
+ 'fa',
88
+ 'pl',
89
+ 'pt',
90
+ 'pt-pt',
91
+ 'pa',
92
+ 'otq',
93
+ 'ro',
94
+ 'ru',
95
+ 'sm',
96
+ 'sr-Cyrl',
97
+ 'sr-Latn',
98
+ 'sk',
99
+ 'sl',
100
+ 'es',
101
+ 'sw',
102
+ 'sv',
103
+ 'ty',
104
+ 'ta',
105
+ 'tt',
106
+ 'te',
107
+ 'th',
108
+ 'bo',
109
+ 'ti',
110
+ 'to',
111
+ 'tr',
112
+ 'tk',
113
+ 'uk',
114
+ 'ur',
115
+ 'ug',
116
+ 'uz',
117
+ 'vi',
118
+ 'cy',
119
+ 'yua'
120
+ ];
121
+ class AzureTranslate {
122
+ constructor(subscriptionKey, subscriptionRegion) {
123
+ this.subscriptionKey = subscriptionKey;
124
+ this.subscriptionRegion = subscriptionRegion;
125
+ this.endpoint = 'https://api.cognitive.microsofttranslator.com';
126
+ }
127
+ isValidLocale(targetLocale) {
128
+ return supportedLanguages.includes(targetLocale);
129
+ }
130
+ translateText(text, sourceLocale, targetLocale) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ let args;
133
+ ({ args, text } = util_1.Util.replaceContextVariables(text));
134
+ let result = '';
135
+ const response = yield axios({
136
+ baseURL: this.endpoint,
137
+ url: '/translate',
138
+ method: 'post',
139
+ headers: {
140
+ 'Ocp-Apim-Subscription-Key': this.subscriptionKey,
141
+ 'Ocp-Apim-Subscription-Region': this.subscriptionRegion,
142
+ 'Content-type': 'application/json',
143
+ 'X-ClientTraceId': uuidv4().toString()
144
+ },
145
+ params: {
146
+ 'api-version': '3.0',
147
+ from: sourceLocale,
148
+ to: [targetLocale]
149
+ },
150
+ data: [
151
+ {
152
+ text: text
153
+ }
154
+ ],
155
+ responseType: 'json'
156
+ });
157
+ result = response.data[0].translations[0].text;
158
+ result = util_1.Util.replaceArgumentsWithNumbers(args, result);
159
+ return result;
160
+ });
161
+ }
162
+ }
163
+ exports.AzureTranslate = AzureTranslate;
164
+ // https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-translator?tabs=nodejs#translate-text
165
+ // Add your location, also known as region. The default is global.
166
+ // This is required if using a Cognitive Services resource.
167
+ /*
168
+
169
+ */
package/config.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ export declare type Configuration = {
2
+ translationKeyInfo: GoogleTranslationKey | AwsTranslationKey | AzureTranslationKey | DeepLProTranslationKey | DeepLFreeTranslationKey;
3
+ startDelimiter: string;
4
+ endDelimiter: string;
5
+ mode: 'file' | 'folder';
6
+ sourceLocale: string;
7
+ keepTranslations: 'keep' | 'retranslate';
8
+ keepExtraTranslations: 'keep' | 'remove';
9
+ };
10
+ declare type GoogleTranslationKey = {
11
+ kind: 'google';
12
+ apiKey: string;
13
+ };
14
+ declare type AwsTranslationKey = {
15
+ kind: 'aws';
16
+ accessKeyId: string;
17
+ secretAccessKey: string;
18
+ region: string;
19
+ };
20
+ declare type AzureTranslationKey = {
21
+ kind: 'azure';
22
+ secretKey: string;
23
+ region: string;
24
+ };
25
+ declare type DeepLProTranslationKey = {
26
+ kind: 'deepLPro';
27
+ secretKey: string;
28
+ };
29
+ declare type DeepLFreeTranslationKey = {
30
+ kind: 'deepLFree';
31
+ secretKey: string;
32
+ };
33
+ export {};
package/config.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/deepl.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { ITranslate } from './translate.interface';
2
+ export declare class DeepLTranslate implements ITranslate {
3
+ private subscriptionKey;
4
+ private type;
5
+ private endpoint;
6
+ constructor(subscriptionKey: string, type: 'free' | 'pro');
7
+ isValidLocale(targetLocale: string): boolean;
8
+ translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
9
+ }
package/deepl.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.DeepLTranslate = void 0;
13
+ const FormData = require("form-data");
14
+ const axios = require('axios').default;
15
+ const util_1 = require("./util");
16
+ const supportedLanguages = [
17
+ 'BG',
18
+ 'CS',
19
+ 'DA',
20
+ 'DE',
21
+ 'EL',
22
+ 'EN-GB',
23
+ 'EN-US',
24
+ 'EN',
25
+ 'ES',
26
+ 'ET',
27
+ 'FI',
28
+ 'FR',
29
+ 'HU',
30
+ 'IT',
31
+ 'JA',
32
+ 'LT',
33
+ 'LV',
34
+ 'NL',
35
+ 'PL',
36
+ 'PT-PT',
37
+ 'PT-BR',
38
+ 'PT',
39
+ 'RO',
40
+ 'RU',
41
+ 'SK',
42
+ 'SL',
43
+ 'SV',
44
+ 'ZH'
45
+ ];
46
+ class DeepLTranslate {
47
+ constructor(subscriptionKey, type) {
48
+ this.subscriptionKey = subscriptionKey;
49
+ this.type = type;
50
+ this.endpoint = 'https://api.deepl.com';
51
+ if (this.type === 'free') {
52
+ this.endpoint = 'https://api.free.deepl.com';
53
+ }
54
+ }
55
+ isValidLocale(targetLocale) {
56
+ return supportedLanguages.includes(targetLocale.toUpperCase());
57
+ }
58
+ translateText(text, sourceLocale, targetLocale) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ let args;
61
+ ({ args, text } = util_1.Util.replaceContextVariables(text));
62
+ let result = '';
63
+ const formData = new FormData();
64
+ formData.append('auth_key', this.subscriptionKey);
65
+ formData.append('text', text);
66
+ formData.append('source_lang', sourceLocale.toUpperCase());
67
+ formData.append('target_lang', targetLocale.toUpperCase());
68
+ const response = yield axios({
69
+ baseURL: this.endpoint,
70
+ url: 'v2/translate',
71
+ method: 'post',
72
+ data: formData,
73
+ responseType: 'json'
74
+ });
75
+ result = response.translations[1].text;
76
+ result = util_1.Util.replaceArgumentsWithNumbers(args, result);
77
+ return result;
78
+ });
79
+ }
80
+ }
81
+ exports.DeepLTranslate = DeepLTranslate;
82
+ // https://www.deepl.com/docs-api/translating-text/example/