-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi18nAddMissingEntries.js
99 lines (81 loc) · 2.61 KB
/
i18nAddMissingEntries.js
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
var fs = require('fs');
function browseLocalesDirectory()
{
return fs.readdirSync('src/locales');
}
function getLocalesFiles(directoryFiles)
{
// Get content of every file of the 'locales' directory
let translationsFiles = [];
for(let i=0; i<directoryFiles.length; ++i)
{
translationsFiles.push(fs.readFileSync('src/locales/'+directoryFiles[i], { encoding: 'utf8' }));
if(directoryFiles[i]=='en.ts')
{
translationsFiles.unshift(translationsFiles.pop());
directoryFiles.unshift(directoryFiles.splice(i,1)[0]);
}
}
return translationsFiles;
}
function removeComments(content)
{
// Remove comments
let currentCharacter = 'A'.charCodeAt();
for(let index=0;index<26; ++index)
{
content = content.replace("//"+String.fromCharCode(currentCharacter++),"");
}
return content;
}
function checkEntries(files)
{
for(let i=1;i<files.length;++i)
{
for(let currentEntry in files[0])
{
if(!files[i][currentEntry])
{
files[i][currentEntry] = '';
}
}
}
}
function beautifyAndWriteFile(JSONContent, beforeEqualText, filepath)
{
let content = JSON.stringify(JSONContent);
content = beforeEqualText + '\n' + content;
content = content.replace('{','{ ');
content = content.replace(/","/gi,'",\n "');
content = content.replace('}','\n}');
let currentCharacter = 'a'.charCodeAt(),
currentUpperCaseCharacter = 'A'.charCodeAt();
for(let index=0; index<26; ++index)
{
content = content.replace(' "'+String.fromCharCode(currentCharacter),'\n //'+String.fromCharCode(currentUpperCaseCharacter++)+'\n "'+String.fromCharCode(currentCharacter++))
}
fs.writeFileSync(filepath, content);
}
function i18nAddMissingEntries()
{
let directoryFiles = browseLocalesDirectory();
let translationsFiles = getLocalesFiles(directoryFiles);
let translationsFilesJSON = [],
beforeEqualTexts = [];
for(let index=0; index<translationsFiles.length; ++index)
{
let equalPosition = translationsFiles[index].indexOf('=');
beforeEqualTexts.push(translationsFiles[index].substring(0, equalPosition+1));
translationsFilesJSON[index] = JSON.parse(removeComments(translationsFiles[index].substring(equalPosition+1)));
}
checkEntries(translationsFilesJSON);
// No need to write the 'en.ts' file because it is our default file
translationsFilesJSON.shift();
beforeEqualTexts.shift();
directoryFiles.shift();
for(let index=0; index<translationsFilesJSON.length; ++index)
{
beautifyAndWriteFile(translationsFilesJSON[index], beforeEqualTexts[index], 'src/locales/'+directoryFiles[index]);
}
}
module.exports = i18nAddMissingEntries;