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/deepl.js CHANGED
@@ -11,45 +11,45 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.DeepLTranslate = void 0;
13
13
  const FormData = require("form-data");
14
- const axios = require('axios').default;
14
+ const axios = require("axios").default;
15
15
  const util_1 = require("./util");
16
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'
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
45
  ];
46
46
  class DeepLTranslate {
47
47
  constructor(subscriptionKey, type) {
48
48
  this.subscriptionKey = subscriptionKey;
49
49
  this.type = type;
50
- this.endpoint = 'https://api.deepl.com';
51
- if (this.type === 'free') {
52
- this.endpoint = 'https://api.free.deepl.com';
50
+ this.endpoint = "https://api.deepl.com";
51
+ if (this.type === "free") {
52
+ this.endpoint = "https://api.free.deepl.com";
53
53
  }
54
54
  }
55
55
  isValidLocale(targetLocale) {
@@ -59,18 +59,18 @@ class DeepLTranslate {
59
59
  return __awaiter(this, void 0, void 0, function* () {
60
60
  let args;
61
61
  ({ args, text } = util_1.Util.replaceContextVariables(text));
62
- let result = '';
62
+ let result = "";
63
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());
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
68
  const response = yield axios({
69
69
  baseURL: this.endpoint,
70
- url: 'v2/translate',
71
- method: 'post',
70
+ url: "v2/translate",
71
+ method: "post",
72
72
  data: formData,
73
- responseType: 'json'
73
+ responseType: "json",
74
74
  });
75
75
  result = response.translations[1].text;
76
76
  result = util_1.Util.replaceArgumentsWithNumbers(args, result);
package/files.d.ts CHANGED
@@ -1,12 +1,13 @@
1
+ import { TranslationFile } from "./translate.interface";
1
2
  export interface IFiles {
2
3
  sourceLocale: string;
3
4
  targetLocales: Array<string>;
4
- loadJsonFromLocale(locale: string): Promise<any>;
5
- saveJsonToLocale(locale: string, file: any): void;
5
+ loadJsonFromLocale(locale: string): Promise<TranslationFile>;
6
+ saveJsonToLocale(locale: string, file: TranslationFile): void;
6
7
  }
7
8
  export declare const readFileAsync: (filename: string) => Promise<string>;
8
- export declare const loadJsonFromLocale: (fileName: string) => Promise<any>;
9
- export declare const saveJsonToLocale: (filename: string, file: any) => void;
9
+ export declare const loadJsonFromLocale: (fileName: string) => Promise<TranslationFile>;
10
+ export declare const saveJsonToLocale: (filename: string, file: TranslationFile) => void;
10
11
  export declare class Files implements IFiles {
11
12
  folderPath: string;
12
13
  sourceLocale: string;
@@ -14,6 +15,6 @@ export declare class Files implements IFiles {
14
15
  constructor(filePath: string);
15
16
  private getLocaleFromFilename;
16
17
  private getTargetLocales;
17
- loadJsonFromLocale(locale: string): Promise<any>;
18
- saveJsonToLocale(locale: string, file: any): void;
18
+ loadJsonFromLocale(locale: string): Promise<TranslationFile>;
19
+ saveJsonToLocale(locale: string, file: TranslationFile): void;
19
20
  }
package/files.js CHANGED
@@ -45,31 +45,31 @@ const loadJsonFromLocale = (fileName) => __awaiter(void 0, void 0, void 0, funct
45
45
  let data = yield (0, exports.readFileAsync)(fileName);
46
46
  // handle empty files
47
47
  if (!data) {
48
- data = '{}';
48
+ data = "{}";
49
49
  }
50
50
  return JSON.parse(data);
51
51
  });
52
52
  exports.loadJsonFromLocale = loadJsonFromLocale;
53
53
  const saveJsonToLocale = (filename, file) => {
54
- let data = JSON.stringify(file, null, ' ');
55
- fs.writeFileSync(filename, data, 'utf8');
54
+ const data = JSON.stringify(file, null, " ");
55
+ fs.writeFileSync(filename, data, "utf8");
56
56
  };
57
57
  exports.saveJsonToLocale = saveJsonToLocale;
58
58
  class Files {
59
59
  constructor(filePath) {
60
60
  this.folderPath = path.dirname(filePath);
61
- let fileName = path.basename(filePath);
61
+ const fileName = path.basename(filePath);
62
62
  this.sourceLocale = this.getLocaleFromFilename(fileName);
63
63
  this.targetLocales = this.getTargetLocales();
64
64
  }
65
65
  getLocaleFromFilename(fileName) {
66
- return fileName.replace('.json', '');
66
+ return fileName.replace(".json", "");
67
67
  }
68
68
  getTargetLocales() {
69
- let locales = new Array();
70
- let files = fs.readdirSync(this.folderPath);
69
+ const locales = new Array();
70
+ const files = fs.readdirSync(this.folderPath);
71
71
  files.forEach((file) => {
72
- let locale = this.getLocaleFromFilename(file);
72
+ const locale = this.getLocaleFromFilename(file);
73
73
  if (locale !== this.sourceLocale) {
74
74
  locales.push(locale);
75
75
  }
@@ -77,11 +77,11 @@ class Files {
77
77
  return locales;
78
78
  }
79
79
  loadJsonFromLocale(locale) {
80
- let filename = this.folderPath + '/' + locale + '.json';
80
+ const filename = `${this.folderPath}/${locale}.json`;
81
81
  return (0, exports.loadJsonFromLocale)(filename);
82
82
  }
83
83
  saveJsonToLocale(locale, file) {
84
- let filename = this.folderPath + '/' + locale + '.json';
84
+ const filename = `${this.folderPath}/${locale}.json`;
85
85
  (0, exports.saveJsonToLocale)(filename, file);
86
86
  }
87
87
  }
package/folderFiles.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { IFiles } from './files';
1
+ import { IFiles } from "./files";
2
+ import { TranslationFile } from "./translate.interface";
2
3
  export declare class FolderFiles implements IFiles {
3
4
  folderPath: string;
4
5
  sourceLocale: string;
@@ -7,6 +8,6 @@ export declare class FolderFiles implements IFiles {
7
8
  constructor(filePath: string);
8
9
  private getTargetLocales;
9
10
  private createFileName;
10
- loadJsonFromLocale(locale: string): Promise<any>;
11
- saveJsonToLocale(locale: string, file: any): void;
11
+ loadJsonFromLocale(locale: string): Promise<TranslationFile>;
12
+ saveJsonToLocale(locale: string, file: TranslationFile): void;
12
13
  }
package/folderFiles.js CHANGED
@@ -37,10 +37,10 @@ class FolderFiles {
37
37
  console.log(this.folderPath, this.fileName, this.sourceLocale, this.targetLocales);
38
38
  }
39
39
  getTargetLocales() {
40
- let files = fs
40
+ const files = fs
41
41
  .readdirSync(this.folderPath)
42
42
  .map((folder) => path.basename(folder))
43
- .map((locale) => (locale !== this.sourceLocale ? locale : ''))
43
+ .map((locale) => (locale !== this.sourceLocale ? locale : ""))
44
44
  .filter((x) => x); // do not want empty strings
45
45
  return files;
46
46
  }
package/google.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ITranslate } from './translate.interface';
1
+ import { ITranslate } from "./translate.interface";
2
2
  export declare class GoogleTranslate implements ITranslate {
3
3
  private apikey;
4
4
  private googleTranslate;
package/google.js CHANGED
@@ -14,116 +14,116 @@ exports.GoogleTranslate = void 0;
14
14
  const v2_1 = require("@google-cloud/translate/build/src/v2");
15
15
  const util_1 = require("./util");
16
16
  const supportedLanguages = [
17
- 'af',
18
- 'sq',
19
- 'am',
20
- 'ar',
21
- 'hy',
22
- 'az',
23
- 'eu',
24
- 'be',
25
- 'bn',
26
- 'bs',
27
- 'bg',
28
- 'ca',
29
- 'ceb',
30
- 'zh-CN',
31
- 'zh',
32
- 'zh-TW',
33
- 'co',
34
- 'hr',
35
- 'cs',
36
- 'da',
37
- 'nl',
38
- 'en',
39
- 'eo',
40
- 'et',
41
- 'fi',
42
- 'fr',
43
- 'fy',
44
- 'gl',
45
- 'ka',
46
- 'de',
47
- 'el',
48
- 'gu',
49
- 'ht',
50
- 'ha',
51
- 'haw',
52
- 'he',
53
- 'iw',
54
- 'hi',
55
- 'hmn',
56
- 'hu',
57
- 'is',
58
- 'ig',
59
- 'id',
60
- 'ga',
61
- 'it',
62
- 'ja',
63
- 'jv',
64
- 'kn',
65
- 'kk',
66
- 'km',
67
- 'rw',
68
- 'ko',
69
- 'ku',
70
- 'ky',
71
- 'lo',
72
- 'lv',
73
- 'lt',
74
- 'lb',
75
- 'mk',
76
- 'mg',
77
- 'ms',
78
- 'ml',
79
- 'mt',
80
- 'mi',
81
- 'mr',
82
- 'mn',
83
- 'my',
84
- 'ne',
85
- 'no',
86
- 'ny',
87
- 'or',
88
- 'ps',
89
- 'fa',
90
- 'pl',
91
- 'pt',
92
- 'pa',
93
- 'ro',
94
- 'ru',
95
- 'sm',
96
- 'gd',
97
- 'sr',
98
- 'st',
99
- 'sn',
100
- 'sd',
101
- 'si',
102
- 'sk',
103
- 'sl',
104
- 'so',
105
- 'es',
106
- 'su',
107
- 'sw',
108
- 'sv',
109
- 'tl',
110
- 'tg',
111
- 'ta',
112
- 'tt',
113
- 'te',
114
- 'th',
115
- 'tr',
116
- 'tk',
117
- 'uk',
118
- 'ur',
119
- 'ug',
120
- 'uz',
121
- 'vi',
122
- 'cy',
123
- 'xh',
124
- 'yi',
125
- 'yo',
126
- 'zu'
17
+ "af",
18
+ "sq",
19
+ "am",
20
+ "ar",
21
+ "hy",
22
+ "az",
23
+ "eu",
24
+ "be",
25
+ "bn",
26
+ "bs",
27
+ "bg",
28
+ "ca",
29
+ "ceb",
30
+ "zh-CN",
31
+ "zh",
32
+ "zh-TW",
33
+ "co",
34
+ "hr",
35
+ "cs",
36
+ "da",
37
+ "nl",
38
+ "en",
39
+ "eo",
40
+ "et",
41
+ "fi",
42
+ "fr",
43
+ "fy",
44
+ "gl",
45
+ "ka",
46
+ "de",
47
+ "el",
48
+ "gu",
49
+ "ht",
50
+ "ha",
51
+ "haw",
52
+ "he",
53
+ "iw",
54
+ "hi",
55
+ "hmn",
56
+ "hu",
57
+ "is",
58
+ "ig",
59
+ "id",
60
+ "ga",
61
+ "it",
62
+ "ja",
63
+ "jv",
64
+ "kn",
65
+ "kk",
66
+ "km",
67
+ "rw",
68
+ "ko",
69
+ "ku",
70
+ "ky",
71
+ "lo",
72
+ "lv",
73
+ "lt",
74
+ "lb",
75
+ "mk",
76
+ "mg",
77
+ "ms",
78
+ "ml",
79
+ "mt",
80
+ "mi",
81
+ "mr",
82
+ "mn",
83
+ "my",
84
+ "ne",
85
+ "no",
86
+ "ny",
87
+ "or",
88
+ "ps",
89
+ "fa",
90
+ "pl",
91
+ "pt",
92
+ "pa",
93
+ "ro",
94
+ "ru",
95
+ "sm",
96
+ "gd",
97
+ "sr",
98
+ "st",
99
+ "sn",
100
+ "sd",
101
+ "si",
102
+ "sk",
103
+ "sl",
104
+ "so",
105
+ "es",
106
+ "su",
107
+ "sw",
108
+ "sv",
109
+ "tl",
110
+ "tg",
111
+ "ta",
112
+ "tt",
113
+ "te",
114
+ "th",
115
+ "tr",
116
+ "tk",
117
+ "uk",
118
+ "ur",
119
+ "ug",
120
+ "uz",
121
+ "vi",
122
+ "cy",
123
+ "xh",
124
+ "yi",
125
+ "yo",
126
+ "zu",
127
127
  ];
128
128
  class GoogleTranslate {
129
129
  constructor(apikey) {
@@ -138,7 +138,7 @@ class GoogleTranslate {
138
138
  return __awaiter(this, void 0, void 0, function* () {
139
139
  let args;
140
140
  ({ args, text } = util_1.Util.replaceContextVariables(text));
141
- let result = '';
141
+ let result = "";
142
142
  try {
143
143
  const response = yield this.googleTranslate.translate(text, targetLocale);
144
144
  result = response[0];
@@ -146,13 +146,13 @@ class GoogleTranslate {
146
146
  catch (error) {
147
147
  if (error instanceof Error) {
148
148
  let message = error.message;
149
- if (error.message === 'Invalid Value') {
150
- message = 'Invalid Locale ' + targetLocale;
149
+ if (error.message === "Invalid Value") {
150
+ message = `Invalid Locale ${targetLocale}`;
151
151
  }
152
152
  console.log(message);
153
153
  return message;
154
154
  }
155
- return 'error';
155
+ return "error";
156
156
  }
157
157
  // replace arguments with numbers
158
158
  result = util_1.Util.replaceArgumentsWithNumbers(args, result);
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { Configuration } from './config';
2
- import { translate } from './lib';
1
+ import { Configuration } from "./config";
2
+ import { translate } from "./lib";
3
3
  export { Configuration, translate };
package/index.js CHANGED
@@ -4,18 +4,18 @@ exports.translate = void 0;
4
4
  const process_1 = require("process");
5
5
  const lib_1 = require("./lib");
6
6
  Object.defineProperty(exports, "translate", { enumerable: true, get: function () { return lib_1.translate; } });
7
- let config = {};
7
+ const config = {};
8
8
  setDefaultConfiguration();
9
9
  setConfigurationFromEnvironment();
10
10
  setConfigurationFromCLI();
11
11
  // console.log(config);
12
12
  function setConfigurationFromCLI() {
13
- const argv = require('yargs').argv;
14
- if (argv.mode === 'file') {
15
- config.mode = 'file';
13
+ const argv = require("yargs").argv;
14
+ if (argv.mode === "file") {
15
+ config.mode = "file";
16
16
  }
17
- else if (argv.mode === 'folder') {
18
- config.mode = 'folder';
17
+ else if (argv.mode === "folder") {
18
+ config.mode = "folder";
19
19
  }
20
20
  if (argv.startDelimiter !== undefined) {
21
21
  config.startDelimiter = argv.startDelimiter;
@@ -23,42 +23,47 @@ function setConfigurationFromCLI() {
23
23
  if (argv.endDelimiter !== undefined) {
24
24
  config.endDelimiter = argv.endDelimiter;
25
25
  }
26
- if (argv.keepExtraTranslations === 'keep') {
27
- config.keepExtraTranslations = 'keep';
26
+ if (argv.keepExtraTranslations === "keep") {
27
+ config.keepExtraTranslations = "keep";
28
28
  }
29
- else if (argv.keepExtraTranslations === 'remove') {
30
- config.keepExtraTranslations = 'remove';
29
+ else if (argv.keepExtraTranslations === "remove") {
30
+ config.keepExtraTranslations = "remove";
31
31
  }
32
- if (argv.keepTranslations === 'keep') {
33
- config.keepTranslations = 'keep';
32
+ if (argv.keepTranslations === "keep") {
33
+ config.keepTranslations = "keep";
34
34
  }
35
- else if (argv.keepTranslations === 'retranslate') {
36
- config.keepTranslations = 'retranslate';
35
+ else if (argv.keepTranslations === "retranslate") {
36
+ config.keepTranslations = "retranslate";
37
37
  }
38
38
  if (argv.sourceLocale !== undefined) {
39
39
  config.sourceLocale = argv.sourceLocale;
40
40
  }
41
+ if (argv.ignorePrefix !== undefined) {
42
+ config.ignorePrefix = argv.ignorePrefix;
43
+ }
41
44
  if (argv.pivotTranslation !== undefined) {
42
45
  (0, lib_1.translate)(argv.pivotTranslation, config);
43
46
  }
44
47
  else {
45
- console.log('Please provide a pivot translation');
48
+ console.log(argv);
49
+ console.log("Please provide a pivot translation");
46
50
  (0, process_1.exit)(1);
47
51
  }
48
52
  }
49
53
  function setDefaultConfiguration() {
50
- config.mode = 'file';
51
- config.startDelimiter = '{';
52
- config.endDelimiter = '}';
53
- config.keepExtraTranslations = 'keep';
54
- config.keepTranslations = 'keep';
55
- config.sourceLocale = 'en';
54
+ config.mode = "file";
55
+ config.startDelimiter = "{";
56
+ config.endDelimiter = "}";
57
+ config.keepExtraTranslations = "keep";
58
+ config.keepTranslations = "keep";
59
+ config.sourceLocale = "en";
60
+ config.ignorePrefix = "";
56
61
  }
57
62
  function setConfigurationFromEnvironment() {
58
- require('dotenv').config();
63
+ require("dotenv").config();
59
64
  if (process.env.ATJ_GOOGLE_API_KEY) {
60
65
  config.translationKeyInfo = {
61
- kind: 'google',
66
+ kind: "google",
62
67
  apiKey: process.env.ATJ_GOOGLE_API_KEY,
63
68
  };
64
69
  }
@@ -66,7 +71,7 @@ function setConfigurationFromEnvironment() {
66
71
  process.env.ATJ_AWS_SECRET_ACCESS_KEY &&
67
72
  process.env.ATJ_AWS_REGION) {
68
73
  config.translationKeyInfo = {
69
- kind: 'aws',
74
+ kind: "aws",
70
75
  accessKeyId: process.env.ATJ_AWS_ACCESS_KEY_ID,
71
76
  secretAccessKey: process.env.ATJ_AWS_SECRET_ACCESS_KEY,
72
77
  region: process.env.ATJ_AWS_REGION,
@@ -74,26 +79,26 @@ function setConfigurationFromEnvironment() {
74
79
  }
75
80
  if (process.env.ATJ_AZURE_SECRET_KEY && process.env.ATJ_AZURE_REGION) {
76
81
  config.translationKeyInfo = {
77
- kind: 'azure',
82
+ kind: "azure",
78
83
  secretKey: process.env.ATJ_AZURE_SECRET_KEY,
79
84
  region: process.env.ATJ_AZURE_REGION,
80
85
  };
81
86
  }
82
87
  if (process.env.ATJ_DEEPL_PRO_SECRET_KEY) {
83
88
  config.translationKeyInfo = {
84
- kind: 'deepLPro',
89
+ kind: "deepLPro",
85
90
  secretKey: process.env.ATJ_DEEPL_PRO_SECRET_KEY,
86
91
  };
87
92
  }
88
93
  if (process.env.ATJ_DEEPL_FREE_SECRET_KEY) {
89
94
  config.translationKeyInfo = {
90
- kind: 'deepLFree',
95
+ kind: "deepLFree",
91
96
  secretKey: process.env.ATJ_DEEPL_FREE_SECRET_KEY,
92
97
  };
93
98
  }
94
99
  if (process.env.ATJ_OPEN_AI_SECRET_KEY) {
95
100
  config.translationKeyInfo = {
96
- kind: 'openai',
101
+ kind: "openai",
97
102
  apiKey: process.env.ATJ_OPEN_AI_SECRET_KEY,
98
103
  };
99
104
  }
@@ -107,20 +112,23 @@ function setConfigurationFromEnvironment() {
107
112
  config.endDelimiter = process.env.ATJ_END_DELIMITER;
108
113
  }
109
114
  if (process.env.ATJ_MODE &&
110
- (process.env.ATJ_MODE === 'file' || process.env.ATJ_MODE === 'folder')) {
115
+ (process.env.ATJ_MODE === "file" || process.env.ATJ_MODE === "folder")) {
111
116
  config.mode = process.env.ATJ_MODE;
112
117
  }
113
118
  if (process.env.ATJ_SOURCE_LOCALE) {
114
119
  config.sourceLocale = process.env.ATJ_SOURCE_LOCALE;
115
120
  }
116
121
  if (process.env.ATJ_KEEP_TRANSLATIONS &&
117
- (process.env.ATJ_KEEP_TRANSLATIONS === 'keep' ||
118
- process.env.ATJ_KEEP_TRANSLATIONS === 'retranslate')) {
122
+ (process.env.ATJ_KEEP_TRANSLATIONS === "keep" ||
123
+ process.env.ATJ_KEEP_TRANSLATIONS === "retranslate")) {
119
124
  config.keepTranslations = process.env.ATJ_KEEP_TRANSLATIONS;
120
125
  }
121
126
  if (process.env.ATJ_KEEP_EXTRA_TRANSLATIONS &&
122
- (process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === 'keep' ||
123
- process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === 'remove')) {
127
+ (process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === "keep" ||
128
+ process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === "remove")) {
124
129
  config.keepExtraTranslations = process.env.ATJ_KEEP_EXTRA_TRANSLATIONS;
125
130
  }
131
+ if (process.env.ATJ_IGNORE_PREFIX && process.env.ATJ_IGNORE_PREFIX !== "") {
132
+ config.ignorePrefix = process.env.ATJ_IGNORE_PREFIX;
133
+ }
126
134
  }
package/lib.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { Configuration } from './config';
1
+ import { Configuration } from "./config";
2
2
  export declare function translate(sourceFile: string, config: Configuration): Promise<void>;