auto-translate-json-library 1.2.0 → 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,4 +1,4 @@
1
- import { ITranslate } from './translate.interface';
1
+ import { ITranslate } from "./translate.interface";
2
2
  export declare class AWSTranslate implements ITranslate {
3
3
  private accessKeyId;
4
4
  private secretAccessKey;
package/aws.js CHANGED
@@ -13,81 +13,81 @@ exports.AWSTranslate = void 0;
13
13
  const client_translate_1 = require("@aws-sdk/client-translate");
14
14
  const util_1 = require("./util");
15
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'
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
91
  ];
92
92
  class AWSTranslate {
93
93
  constructor(accessKeyId, secretAccessKey, regions) {
@@ -97,9 +97,9 @@ class AWSTranslate {
97
97
  this.client = new client_translate_1.Translate({
98
98
  credentials: {
99
99
  accessKeyId: this.accessKeyId,
100
- secretAccessKey: this.secretAccessKey
100
+ secretAccessKey: this.secretAccessKey,
101
101
  },
102
- region: this.regions
102
+ region: this.regions,
103
103
  });
104
104
  }
105
105
  isValidLocale(targetLocale) {
@@ -109,11 +109,11 @@ class AWSTranslate {
109
109
  return __awaiter(this, void 0, void 0, function* () {
110
110
  let args;
111
111
  ({ args, text } = util_1.Util.replaceContextVariables(text));
112
- let result = '';
112
+ let result = "";
113
113
  const params = {
114
114
  SourceLanguageCode: sourceLocale,
115
115
  TargetLanguageCode: targetLocale,
116
- Text: text
116
+ Text: text,
117
117
  };
118
118
  try {
119
119
  const translation = yield this.client.translateText(params);
@@ -121,7 +121,7 @@ class AWSTranslate {
121
121
  }
122
122
  catch (error) {
123
123
  console.log(error);
124
- return '';
124
+ return "";
125
125
  }
126
126
  result = util_1.Util.replaceArgumentsWithNumbers(args, result);
127
127
  return result;
package/azure.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ITranslate } from './translate.interface';
1
+ import { ITranslate } from "./translate.interface";
2
2
  export declare class AzureTranslate implements ITranslate {
3
3
  private subscriptionKey;
4
4
  private subscriptionRegion;
package/azure.js CHANGED
@@ -11,118 +11,118 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AzureTranslate = void 0;
13
13
  const util_1 = require("./util");
14
- const axios = require('axios').default;
15
- const { v4: uuidv4 } = require('uuid');
14
+ const axios = require("axios").default;
15
+ const { v4: uuidv4 } = require("uuid");
16
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'
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
120
  ];
121
121
  class AzureTranslate {
122
122
  constructor(subscriptionKey, subscriptionRegion) {
123
123
  this.subscriptionKey = subscriptionKey;
124
124
  this.subscriptionRegion = subscriptionRegion;
125
- this.endpoint = 'https://api.cognitive.microsofttranslator.com';
125
+ this.endpoint = "https://api.cognitive.microsofttranslator.com";
126
126
  }
127
127
  isValidLocale(targetLocale) {
128
128
  return supportedLanguages.includes(targetLocale);
@@ -131,28 +131,28 @@ class AzureTranslate {
131
131
  return __awaiter(this, void 0, void 0, function* () {
132
132
  let args;
133
133
  ({ args, text } = util_1.Util.replaceContextVariables(text));
134
- let result = '';
134
+ let result = "";
135
135
  const response = yield axios({
136
136
  baseURL: this.endpoint,
137
- url: '/translate',
138
- method: 'post',
137
+ url: "/translate",
138
+ method: "post",
139
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()
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
144
  },
145
145
  params: {
146
- 'api-version': '3.0',
146
+ "api-version": "3.0",
147
147
  from: sourceLocale,
148
- to: [targetLocale]
148
+ to: [targetLocale],
149
149
  },
150
150
  data: [
151
151
  {
152
- text: text
153
- }
152
+ text: text,
153
+ },
154
154
  ],
155
- responseType: 'json'
155
+ responseType: "json",
156
156
  });
157
157
  result = response.data[0].translations[0].text;
158
158
  result = util_1.Util.replaceArgumentsWithNumbers(args, result);
package/config.d.ts CHANGED
@@ -2,36 +2,37 @@ export type Configuration = {
2
2
  translationKeyInfo: GoogleTranslationKey | AwsTranslationKey | AzureTranslationKey | DeepLProTranslationKey | DeepLFreeTranslationKey | OpenAITranslationKey;
3
3
  startDelimiter: string;
4
4
  endDelimiter: string;
5
- mode: 'file' | 'folder';
5
+ mode: "file" | "folder";
6
6
  sourceLocale: string;
7
- keepTranslations: 'keep' | 'retranslate';
8
- keepExtraTranslations: 'keep' | 'remove';
7
+ keepTranslations: "keep" | "retranslate";
8
+ keepExtraTranslations: "keep" | "remove";
9
+ ignorePrefix: string;
9
10
  };
10
11
  type GoogleTranslationKey = {
11
- kind: 'google';
12
+ kind: "google";
12
13
  apiKey: string;
13
14
  };
14
15
  type AwsTranslationKey = {
15
- kind: 'aws';
16
+ kind: "aws";
16
17
  accessKeyId: string;
17
18
  secretAccessKey: string;
18
19
  region: string;
19
20
  };
20
21
  type AzureTranslationKey = {
21
- kind: 'azure';
22
+ kind: "azure";
22
23
  secretKey: string;
23
24
  region: string;
24
25
  };
25
26
  type DeepLProTranslationKey = {
26
- kind: 'deepLPro';
27
+ kind: "deepLPro";
27
28
  secretKey: string;
28
29
  };
29
30
  type DeepLFreeTranslationKey = {
30
- kind: 'deepLFree';
31
+ kind: "deepLFree";
31
32
  secretKey: string;
32
33
  };
33
34
  type OpenAITranslationKey = {
34
- kind: 'openai';
35
+ kind: "openai";
35
36
  apiKey: string;
36
37
  };
37
38
  export {};
package/deepl.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { ITranslate } from './translate.interface';
1
+ import { ITranslate } from "./translate.interface";
2
2
  export declare class DeepLTranslate implements ITranslate {
3
3
  private subscriptionKey;
4
4
  private type;
5
5
  private endpoint;
6
- constructor(subscriptionKey: string, type: 'free' | 'pro');
6
+ constructor(subscriptionKey: string, type: "free" | "pro");
7
7
  isValidLocale(targetLocale: string): boolean;
8
8
  translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
9
9
  }