-
Notifications
You must be signed in to change notification settings - Fork 64
/
i18n_helper.js.dist
119 lines (105 loc) · 3.01 KB
/
i18n_helper.js.dist
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
/**
* Copy this file to i18n_helper.js to quickly
* import, rename, or delete i18n messages.
*
* To use, add the necessary code below
* and run `node i18n_helper.js`.
*
* WARNING: these functions may run asynchronously,
* so use only one at a time (or implement a callback)
* to ensure you don't end up with corrupt files.
*/
const dir = './i18n/';
const fs = require('fs');
// relative path to XTool's i18n directory
const xtoolsPath = './i18n/';
/**
* Import messages from another project to XTools
* To use, add this code to this file:
* const messagesToCopy = [
* 'pageviews',
* 'external-link',
* ...
* ];
* importMessages('../pageviews/messages/', messagesToCopy);
* @param {Array} messagesToCopy - message keys
*/
function importMessages(oldPath, messagesToCopy)
{
fs.readdir(oldPath, (err, files) => {
files.forEach(file => {
const oldJson = require(`${oldPath}${file}`);
let newJson;
try {
newJson = require(`${xtoolsPath}${file}`);
} catch (e) {
// new file will be created
newJson = {};
}
messagesToCopy.forEach(message => {
// skip if message already present
if (newJson[message]) {
return;
}
const oldMessage = oldJson[message];
newJson[message] = oldMessage;
});
sortAndWriteToFile(newJson, file);
});
});
}
/**
* Rename a message key.
* To use, add this code to this file:
* renameMessage('old_key', 'new_key');
* @param {String} oldName - current key name
* @param {String} newName - new key name
*/
function renameMessage(oldName, newName)
{
fs.readdir(dir, (err, files) => {
files.forEach(file => {
let json = require(`${xtoolsPath}${file}`);
const message = json[oldName];
delete json[oldName];
json[newName] = message;
sortAndWriteToFile(json, file);
});
});
}
/**
* Delete a message from all i18n files.
* To use, add this code to this file:
* deleteMessage('unused-message');
* @param {String} key - key name
*/
function deleteMessage(key)
{
fs.readdir(dir, (err, files) => {
files.forEach(file => {
let json = require(`${xtoolsPath}${file}`);
delete json[key];
sortAndWriteToFile(json, file);
});
});
}
/**
* Sort an i18n file by key and save it.
* To use, add this code to this file:
* sortAndWriteToFile(jsonBlob, 'en');
* @param {Object} json - i18n messages file, without the .json
* @param {String} filename
*/
function sortAndWriteToFile(json, filename)
{
let orderedJson = {};
Object.keys(json).sort().forEach(key => {
orderedJson[key] = json[key];
});
fs.writeFile(
`${xtoolsPath}${filename}`,
JSON.stringify(orderedJson, null, '\t') + "\n",
{},
err => console.log(err)
);
}