forked from olavpo/dhis2-metadata-translation-swapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
340 lines (310 loc) · 6.55 KB
/
Copy pathapp.js
File metadata and controls
340 lines (310 loc) · 6.55 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
const fs = require("fs");
const inquirer = require("inquirer");
run();
async function run() {
let metadataFilePath = filePath();
if (!metadataFilePath) return;
let metadata = readFile(metadataFilePath);
if (!metadata) return;
let stats = localeStats(metadata);
let chosenLocale = await promptLocales(metadata, stats);
let swappedMetadata = swapTranslations(metadata, chosenLocale.currentLocale, chosenLocale.newLocale);
saveFile(swappedMetadata, saveFilePath(metadataFilePath, chosenLocale.newLocale));
}
/** CLI */
async function promptLocales(metadata, stats) {
var prompt = [
{
type: "list",
name: "currentLocale",
message: "Current main locale",
choices: allLocaleOptions(),
default: "en: English"
},
{
type: "list",
name: "newLocale",
message: "Locale to set as main locale",
choices: availableLocaleOptions(stats)
}
];
let { currentLocale, newLocale } = await inquirer.prompt(prompt);
return {
"currentLocale": currentLocale.split(":")[0],
"newLocale": newLocale.split(":")[0]
}
}
/** METADATA OPERATIONS */
function swapTranslations(metadata, currentLocale, newLocale) {
let property = {};
for (let type in metadata) {
if (metadata[type] && metadata[type].length > 0) {
for (let obj of metadata[type]) {
if (obj.hasOwnProperty("translations")) {
let newTranslations = [];
for (let translation of obj.translations) {
if (translation.locale == newLocale && obj.hasOwnProperty(propLookup(translation.property))) {
let currentValue = obj[propLookup(translation.property)];
let newValue = translation.value;
//Tmp fix for short name translations > 50 characters
if (translation.property == "SHORT_NAME") newValue = newValue.substring(0, 50);
obj[propLookup(translation.property)] = newValue;
translation.value = currentValue;
translation.locale = currentLocale;
newTranslations.push(translation);
}
else {
newTranslations.push(translation);
}
}
obj.translations = newTranslations;
}
}
}
else {
continue;
}
}
return metadata;
}
function propLookup(translationProperty) {
switch (translationProperty) {
case "NAME":
return "name";
case "SHORT_NAME":
return "shortName";
case "DESCRIPTION":
return "description";
case "FORM_NAME":
return "formName";
case "NUMERATOR_DESCRIPTION":
return "numeratorDescription";
case "DENOMINATOR_DESCRIPTION":
return "denominatorDescription";
case "CONTENT":
return "content";
case "subtitle":
return "subtitle";
case "rangeAxisLabel":
return "rangeAxisLabel";
case "title":
return "title";
default:
console.log("ERROR: unknown translatable property: " + translationProperty);
return false;
}
}
function localeStats(metadata) {
let stats = {
"otherObjects": 0,
"translatableObjects": 0
};
for (let type in metadata) {
if (metadata[type] && metadata[type].length > 0) {
for (let obj of metadata[type]) {
if (obj.hasOwnProperty("translations")) {
stats.translatableObjects++;
let objLocales = {};
for (let translation of obj.translations) {
objLocales[translation.locale] = true; //translation (full or partial) exist for this object
}
for (let locale in objLocales) {
if (!stats[locale]) {
stats[locale] = 1;
}
else {
stats[locale]++;
}
}
}
else {
stats.otherObjects++;
}
}
}
else {
continue;
}
}
return stats;
}
/** FILE OPERATIONS */
function filePath() {
if (process.argv.length > 2) {
var filePath = process.argv[2];
}
else {
console.log("No metadata file specified. Use: node app.js metadata.json");
return false;
}
if (fs.existsSync(filePath)) {
return filePath;
}
else {
console.log("The specified file does not exist.");
return false;
}
}
function saveFilePath(filePath, newLocale) {
return filePath.replace(".json", "_" + newLocale + ".json");
}
function readFile(filePath) {
let fileContent = fs.readFileSync(filePath);
let metadata;
try {
metadata = JSON.parse(fileContent);
} catch (error) {
console.log("Problem parsing JSON:");
console.log(error);
return false;
}
return metadata;
}
function saveFile(metadata, filePath) {
try {
fs.writeFileSync(filePath, JSON.stringify(metadata, null, 4));
console.log("Metadata with swapped translations save to " + filePath);
} catch (error) {
console.log("Error saving swapped metadata file.");
console.log(error);
}
}
/** UTILITIES */
function allLocaleOptions() {
let localeOptions = [];
let all = allLocales();
for (let locale of all) {
localeOptions.push(locale.locale + ": " + locale.name);
}
return localeOptions;
}
function availableLocaleOptions(localeStats) {
var localeOptions = [];
for (var localeKey in localeStats) {
if (localeKey != "translatableObjects" && localeKey != "otherObjects") {
let completeness = (100 * localeStats[localeKey] / localeStats["translatableObjects"]).toFixed(1);
localeOptions.push(localeKey + ": " + localeStats[localeKey] + " objects translated (" + completeness + "%)");
}
}
return localeOptions;
}
function allLocales() {
return [
{
"locale": "ar",
"name": "Arabic"
},
{
"locale": "ar_EG",
"name": "Arabic (Egypt)"
},
{
"locale": "ar_IQ",
"name": "Arabic (Iraq)"
},
{
"locale": "ar_SD",
"name": "Arabic (Sudan)"
},
{
"locale": "bn",
"name": "Bengali"
},
{
"locale": "bi",
"name": "Bislama"
},
{
"locale": "my",
"name": "Burmese"
},
{
"locale": "zh",
"name": "Chinese"
},
{
"locale": "da",
"name": "Danish"
},
{
"locale": "en",
"name": "English"
},
{
"locale": "fr",
"name": "French"
},
{
"locale": "in_ID",
"name": "Indonesian (Indonesia)"
},
{
"locale": "km",
"name": "Khmer"
},
{
"locale": "rw",
"name": "Kinyarwanda"
},
{
"locale": "lo",
"name": "Lao"
},
{
"locale": "mn",
"name": "Mongolian"
},
{
"locale": "ne",
"name": "Nepali"
},
{
"locale": "pt",
"name": "Portuguese"
},
{
"locale": "pt_BR",
"name": "Portuguese (Brazil)"
},
{
"locale": "ps",
"name": "Pushto"
},
{
"locale": "ru",
"name": "Russian"
},
{
"locale": "es",
"name": "Spanish"
},
{
"locale": "sv",
"name": "Swedish"
},
{
"locale": "tg",
"name": "Tajik"
},
{
"locale": "tet",
"name": "Tetum"
},
{
"locale": "ur",
"name": "Urdu"
},
{
"locale": "vi",
"name": "Vietnamese"
},
{
"locale": "ckb",
"name": "ckb"
},
{
"locale": "prs",
"name": "prs"
}
];
}