auto-translate-json-library 1.1.5 → 1.2.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/README.MD ADDED
@@ -0,0 +1,124 @@
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 commands
39
+
40
+ ```shell
41
+ npm run i
42
+ npm run build
43
+ node .\build\src\index.js --pivotTranslation=./tests/translations/en.json
44
+ ```
45
+
46
+ ```
47
+ # CLI PARAMETERS
48
+
49
+ mode,
50
+ file or folder,
51
+ default is file
52
+
53
+ startDelimiter,
54
+ start delimiter,
55
+ default is {
56
+
57
+ endDelimiter,
58
+ end delimiter,
59
+ default is }
60
+
61
+ keepExtraTranslations,
62
+ keep extra translations (not present in pivot translation),
63
+ default is keep,
64
+ it could be keep or remove
65
+
66
+ keepTranslations,
67
+ keep existing translations,
68
+ default is keep
69
+ it could be keep or retranslate
70
+
71
+ sourceLocale
72
+ source locale
73
+ it could be en or fr ...
74
+ if specified it must match with pivot translation
75
+
76
+ pivotTranslation
77
+ pivot translation file that will be used as source for translations
78
+
79
+ ignorePrefix
80
+ ignore translation that key starts with ignore prefix
81
+
82
+ # ENVIRONMENT VARIABLES
83
+
84
+ google parameters
85
+ ATJ_GOOGLE_API_KEY=
86
+
87
+ aws parameters
88
+ ATJ_AWS_ACCESS_KEY_ID=
89
+ ATJ_AWS_SECRET_ACCESS_KEY=
90
+ ATJ_AWS_REGION=
91
+
92
+ azure parameters
93
+ ATJ_AZURE_SECRET_KEY=
94
+ ATJ_AZURE_REGION=
95
+
96
+ deepl pro parameters
97
+ ATJ_DEEPL_PRO_SECRET_KEY=
98
+ deepl free parameters
99
+ ATJ_DEEPL_FREE_SECRET_KEY=
100
+
101
+ OpenAI parameters
102
+ ATJ_OPEN_AI_SECRET_KEY=
103
+
104
+ start delimiter
105
+ ATJ_START_DELIMITER={{
106
+ end delimiter
107
+ ATJ_END_DELIMITER=}}
108
+
109
+ file or folder
110
+ ATJ_MODE=file
111
+
112
+ source locale
113
+ ATJ_SOURCE_LOCALE=en
114
+
115
+ keep or retranslate existing translations
116
+ ATJ_KEEP_TRANSLATIONS=keep
117
+
118
+ keep or remove extra translations
119
+ ATJ_KEEP_EXTRA_TRANSLATIONS=keep
120
+
121
+ ignore prefix translation keys to ignore
122
+ ATJ_IGNORE_PREFIX=
123
+
124
+ You can also use .env file to store environment variables
package/aws.d.ts CHANGED
@@ -1,10 +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
- }
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 CHANGED
@@ -1,131 +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;
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 CHANGED
@@ -1,9 +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
- }
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
+ }