@eui/tools 5.1.13 → 5.2.0

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.
@@ -1 +1 @@
1
- 5.1.13
1
+ 5.2.0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 5.2.0 (2022-04-29)
2
+
3
+ ##### New Features
4
+
5
+ * **other:**
6
+ * added script for translating i18n files EUI-5847 [EUI-5847](https://webgate.ec.europa.eu/CITnet/jira/browse/EUI-5847) ([3e0740f4](https://webgate.ec.europa.eu/CITnet/stash/scm/csdr/eui-tools.git/commits/3e0740f40386d87f2baa4045ae14fe93adf40655))
7
+
8
+ * * *
9
+ * * *
1
10
  ## 5.1.13 (2022-04-28)
2
11
 
3
12
  ##### Bug Fixes
@@ -35,6 +35,7 @@ const scriptIndex = args.findIndex(
35
35
  x === 'csdr-stats' ||
36
36
  x === 'generate-app-metadata' ||
37
37
  x === 'help' ||
38
+ x === 'i18n-translate' ||
38
39
  x === 'inject-config-app' ||
39
40
  x === 'version'
40
41
  );
@@ -70,6 +71,7 @@ switch (script) {
70
71
  case 'csdr-stats':
71
72
  case 'generate-app-metadata':
72
73
  case 'help':
74
+ case 'i18n-translate':
73
75
  case 'inject-config-app':
74
76
  case 'version': {
75
77
  const result = spawn.sync(
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const i18nTranslate = require('../../scripts/utils/translate/translate-utils');
4
+
5
+ Promise.resolve()
6
+ .then(() => {
7
+ return i18nTranslate.translateI18nFile();
8
+ })
9
+ .catch((e) => {
10
+ console.log(e);
11
+ process.exit(1);
12
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eui/tools",
3
- "version": "5.1.13",
3
+ "version": "5.2.0",
4
4
  "tag": "latest",
5
5
  "license": "EUPL-1.1",
6
6
  "description": "eUI common tools and scripts",
@@ -0,0 +1,124 @@
1
+ 'use strict';
2
+
3
+ const tools = require('../../utils/tools');
4
+ const https = require('https');
5
+ var fs = require('fs');
6
+ const { filePath, sourceLang, targetLang } = require('../tools').getArgs();
7
+ const TEXT = '<text>';
8
+ const TEXT_END = '</text>';
9
+ const CONTENT = '<content>';
10
+ const CONTENT_END = '</content>';
11
+
12
+ // example: node translate-utils.js --filePath c:\i18n\en.json --sourceLang en --targetLang bg
13
+
14
+ const readFile = (filePath) => {
15
+ return JSON.parse(fs.readFileSync(filePath));
16
+ }
17
+
18
+ const translate = (text, sourceLang, targetLang) => {
19
+ tools.logTitle(`Translating file : ${filePath} from ${sourceLang} to ${targetLang}.`);
20
+ return new Promise((resolve, reject) => {
21
+ const data = JSON.stringify({
22
+ textToTranslate: text,
23
+ sourceLanguage: sourceLang,
24
+ targetLanguage: targetLang
25
+ });
26
+
27
+ const options = {
28
+ hostname: 'europa.eu',
29
+ path: '/webtools/rest/etrans/translate',
30
+ method: 'POST',
31
+ headers: {
32
+ 'Content-Type': 'application/json',
33
+ 'Content-Length': data.length
34
+ }
35
+ };
36
+
37
+ const req = https.request(options, res => {
38
+ let chunks_of_data = [];
39
+
40
+ res.on('data', d => {
41
+ chunks_of_data.push(d);
42
+ });
43
+ res.on('end', () => {
44
+ tools.logInfo('Received response from eTrans service.');
45
+ let response_body = Buffer.concat(chunks_of_data);
46
+ var response = JSON.parse(response_body);
47
+ resolve(Buffer.from(response.translation, 'base64').toString());
48
+ });
49
+ });
50
+
51
+ req.on('error', error => {
52
+ reject(error);
53
+ return;
54
+ });
55
+
56
+ tools.logInfo('Sending request to eTrans service.');
57
+ req.write(data);
58
+ req.end();
59
+ });
60
+ }
61
+
62
+ const prepareStringToTranslate = (arrLines, result) => {
63
+ var keys = Object.keys(arrLines);
64
+ keys.forEach((key) => {
65
+ if (arrLines[key] instanceof Object && !(arrLines[key] instanceof Array)) {
66
+ result += prepareStringToTranslate(arrLines[key], '');
67
+ } else {
68
+ result += `${TEXT}${arrLines[key]}${TEXT_END}`;
69
+ }
70
+ });
71
+ return result;
72
+ }
73
+
74
+ const createResultJsonFile = (arrLines, res) => {
75
+ const resString = res.split(`${CONTENT}${TEXT}`).pop().split(`${TEXT_END}${CONTENT_END}`)[0];
76
+ const resArr = resString.split(`${TEXT_END}${TEXT}`);
77
+ var resultJson = Object.assign({}, arrLines);
78
+ var finalRes = iterateResultToJson(resultJson, resArr);
79
+ return finalRes;
80
+ }
81
+
82
+ const iterateResultToJson = (resultJson, resArr) => {
83
+ var keys = Object.keys(resultJson);
84
+ keys.forEach((key, index) => {
85
+ if (resultJson[key] instanceof Object && !(resultJson[key] instanceof Array)) { // If there is nested JSON
86
+ resultJson[key] = iterateResultToJson(resultJson[key], resArr.slice(index));
87
+ var subKeys = Object.keys(resultJson[key]);
88
+ resArr.splice(index, subKeys.length - 1); // remove elements for nested JSON from results array
89
+ } else {
90
+ if (resultJson[key] instanceof Array) {
91
+ resultJson[key] = resArr[index].split(',').map(el => el.trim());
92
+ } else {
93
+ resultJson[key] = resArr[index];
94
+ }
95
+ }
96
+ });
97
+ return resultJson;
98
+ }
99
+
100
+ const translateI18nFile = () => {
101
+ return Promise.resolve()
102
+ .then(() => {
103
+ const arrLines = readFile(filePath);
104
+ const destFolder = filePath.substring(0, filePath.lastIndexOf("\\") + 1);
105
+ var stringToTranslate = prepareStringToTranslate(arrLines, '');
106
+ translate(`${CONTENT}${stringToTranslate}${CONTENT_END}`, sourceLang, targetLang)
107
+ .then((res) => {
108
+ const resultJson = createResultJsonFile(arrLines, res);
109
+ fs.writeFile(`${destFolder}${targetLang}.json`, JSON.stringify(resultJson, null, 2), function (err) {
110
+ if (err) { tools.logError(err); }
111
+ tools.logInfo(`File ${targetLang}.json created.`);
112
+ });
113
+
114
+ }, (err) => {
115
+ tools.logInfo(`Error: ${err}`);
116
+ });
117
+ }).catch((error) => {
118
+ tools.logError(error);
119
+ });
120
+ }
121
+
122
+ translateI18nFile();
123
+
124
+ module.exports.translateI18nFile = translateI18nFile;