-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondJSONGenerator.js
More file actions
198 lines (167 loc) · 7.59 KB
/
Copy pathSecondJSONGenerator.js
File metadata and controls
198 lines (167 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs');
const utility = require('./UtilityModule');
module.exports = {
createTranslationJSON
};
var languages = ['de', 'en', 'es', 'fr', 'it', 'ja', 'nl'];
/** This function will return a String which will represent a JSON. It will be resulted by processing the array of the "English keys"
*
* @param {String[]} englishKeys
* @param {JSON} languageModel
* @param {JSON} englishModel
* @param {Number} nrBigSpaces
* @returns {String}
* @private
*/
function processEnglishKeys(englishKeys, languageModel, englishModel, nrBigSpaces) {
let innerJSON = '{\n';
for (var i = 0; i < englishKeys.length - 1; i++) {
if (languageModel[englishKeys[i]]) {
innerJSON += utility.generateSpaces(nrBigSpaces) + '"' + englishModel[ englishKeys[ i ] ] + '" : "' + languageModel[ englishKeys[ i ] ] + '",\n';
}
else
innerJSON += resolveUnfoundKeyException(englishKeys[ i ], languageModel, englishModel, nrBigSpaces);
}
if ( languageModel[ englishKeys[ englishKeys.length - 1 ] ] )
innerJSON += utility.generateSpaces( nrBigSpaces ) + '"' +
englishModel[ englishKeys[ englishKeys.length - 1 ] ] + '" : "' + languageModel[ englishKeys[ englishKeys.length - 1 ] ] + '"';
else {
innerJSON += resolveUnfoundKeyException(englishKeys[ englishKeys.length - 1 ], languageModel, englishModel, nrBigSpaces);
}
innerJSON += '\n' + utility.generateSpaces( nrBigSpaces ) + '}';
return innerJSON;
}
/** In some of the cases the inner keys in the JSON will be only partial keys in the model JSON. In this case I will have to iterate all the language JSON and to find out
* if the partial key is a component of the complete key and it also starts with that component
*
* @param {String} partialKey
* @param {JSON} languageModel
* @param {JSON} englishModel
* @param {JSON} englishModel
* @param {Number} nrBigSpaces
* @returns {String}
* @private
*/
function processInnerArray(innerArray, languageModel, englishModel, nrTabs)
{
let text = '{\n';
for(var i=0; i < innerArray.length; i++) {
text += utility.generateSpaces( nrTabs ) + '"' + languageModel[ innerArray[ i ] ] + '" : "' + englishModel[ innerArray[ i ] ] + '",\n';
}
text += utility.generateSpaces( nrTabs ) + '}';
return text;
}
/** Sometimes the value of a key in the model JSON is not an array of keys, but an inner JSON. This function will handle the particular case
*
* @param {JSON} innerObject
* @param {JSON} languageModel
* @param {JSON} englishModel
* @param {Number} nrBigSpaces
* @returns {String}
* @private
*/
function processInnerObject(innerObject, languageModel, englishModel, nrBigSpaces) {
let text = '{\n';
for (var itr in innerObject) {
text += utility.generateSpaces( nrBigSpaces ) + '"' + englishModel[itr] + '" : ' + processInnerArray(innerObject[ itr ], languageModel, englishModel, nrBigSpaces + 1) + '\n';
}
return text + utility.generateSpaces( nrBigSpaces ) + '}';
}
/** In some of the cases the inner keys in the JSON will be only partial keys in the model JSON. In this case I will have to iterate all the language JSON and to find out
* if the partial key is a component of the complete key and it also starts with that component
*
* @param {String} partialKey
* @param {JSON} languageModel
* @param {JSON} englishModel
* @param {JSON} englishModel
* @param {Number} nrBigSpaces
* @returns {String}
* @private
*/
function resolveUnfoundKeyException(partialKey, languageModel, englishModel, nrBigSpaces) {
let text = "";
for (var itr in languageModel) {
if ( itr.startsWith( partialKey ) ) {
text += utility.generateSpaces( nrBigSpaces ) + '"' + englishModel[ itr ] + '" : ' + '"' + languageModel[ itr ] + '"\n';
}
}
return text;
}
/** Every key in the final JSON (like en, fr, ja...) will contain a value, also JSON which will contain words and expressions in English and its translations in the required languages
*
* @param {String} principalKey
* @param {String[] } englishKeys
* @param {JSON} languageModel
* @param {JSON} englishModel
* @param {Number} nrBigSpaces
* @param {Boolean} specialIndentationRequired
* @returns {String}
* @private
*/
function createPrincipalValueJSON(principalKey, englishKeys, languageModel, englishModel, nrBigSpaces, specialIndentationRequired) {
let output = '';
if ( specialIndentationRequired ) {
output = ' "' + principalKey + '":';
}
else {
output = utility.generateSpaces( nrBigSpaces ) + '"' + principalKey + '": ';
}
output += processEnglishKeys(englishKeys, languageModel, englishModel, nrBigSpaces + 1 );
return output;
}
/** Sorts the classes to be instantiated.
*
* @param {String} keyName
* @param {JSON} innerObject
* @param {String} languageModel
* @param {String} englishModel
* @param {Number} nrBigSpaces
* @returns {String}
* @private
*/
function createNestedValueJSON( keyName, innerObject, languageModel, englishModel, nrBigSpaces )
{
return utility.generateSpaces( nrBigSpaces ) + '"' + keyName + '": ' +
processInnerObject(innerObject, languageModel, englishModel, nrBigSpaces + 1 ) + ',\n';
}
/** This function will iterate the JSON received as a parameter and will return a more complex JSON which will be a component of the final one that will be returned through the driver function of this file
*
* @param {JSON} model
* @param {JSON} languageModel
* @param {JSON} englishModel
* @param {Number} nrBigSpaces
* @return {String}
* @private
*/
function iterateModelJSON(model, languageModel, englishModel, nrBigSpaces) {
let partialString = ' {\n';
for (var itr in model) {
if (itr != "dashboards") {
if (Array.isArray(model[itr])) {
partialString += createPrincipalValueJSON(itr, model[itr], languageModel, englishModel, nrBigSpaces + 1, false) + ',\n';
} else if (typeof model[itr] === 'object') {
partialString += createNestedValueJSON(itr, model[ itr ], languageModel, englishModel, nrBigSpaces + 1);
}
}
}
return partialString.substring(0, partialString.length - 2) + '\n' + utility.generateSpaces( nrBigSpaces ) + '}';
}
/** This function will return a String that will serve to translate the content of the webpage from English to other languages
* @param {JSON} model It will be a JSON that will contain the key for the equivalent translation in different languages
* @returns {String} it will be a String that will represent a JSON that for every category will contain as keys the words or expressions in English and as values the translations in the required languages
* @public
*/
function createTranslationJSON( model ) {
let finalJSON = '';
const fs = require( 'fs' );
//Here I read the english translation
let englishModel = utility.parseJSONFile('translatedFiles/en/supervisor.json');
//Here I iterate every language and I create the subJSONs JSONs
for (var i = 0; i < languages.length - 1; i++) {
let languageModel = utility.parseJSONFile('translatedFiles/' + languages[i] + '/supervisor.json');
finalJSON += ' "' + languages[i] + '": ' + iterateModelJSON(model, languageModel, englishModel, 1) + ",\n";
}
let languageModel = utility.parseJSONFile('translatedFiles/' + languages[languages.length - 1] + '/supervisor.json');
finalJSON += ' "' + languages[languages.length - 1] + '": ' + iterateModelJSON(model, languageModel, englishModel, 1);
return '{\n' + finalJSON + '\n}';
}