forked from RocketChat/Rocket.Chat.Livechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-i18n.js
More file actions
executable file
·61 lines (52 loc) · 1.94 KB
/
import-i18n.js
File metadata and controls
executable file
·61 lines (52 loc) · 1.94 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
#!/usr/bin/env node
const fs = require('fs');
const prompts = require('prompts');
const { promisify } = require('util');
const importTranslationsFrom = async(rocketChatSourceDir) => {
const oldTranslations = (await promisify(fs.readdir)(`${ rocketChatSourceDir }/packages/rocketchat-i18n/i18n`))
.filter((name) => name.startsWith('livechat.') && name !== 'livechat.en.i18n.json')
.map((name) => ({
language: /livechat\.(.+?)\.i18n.json/.exec(name)[1].replace('-', '_'),
strings: require(`${ rocketChatSourceDir }/packages/rocketchat-i18n/i18n/${ name }`),
}));
const newStrings = require('./src/i18n/default.json').en;
const oldStrings = require(`${ rocketChatSourceDir }/packages/rocketchat-i18n/i18n/livechat.en.i18n.json`);
const mapKeys = {};
for (const [newKey, newString] of Object.entries(newStrings)) {
const oldEntry = Object.entries(oldStrings).find(([, oldString]) => newString === oldString);
oldEntry && (mapKeys[oldEntry[0]] = newKey);
}
const newTranslations = oldTranslations
.map(({ language, strings }) => ({
language,
strings: {
...newStrings,
...(
Object.entries(strings)
.filter(([oldKey]) => !!mapKeys[oldKey])
.reduce((strings, [oldKey, oldString]) => ({ ...strings, [mapKeys[oldKey]]: oldString }), {})
),
},
}));
for (const { language, strings } of newTranslations) {
console.log(`Writing i18n file for language "${ language }"...`);
await promisify(fs.writeFile)(`${ __dirname }/src/i18n/${ language }.json`, JSON.stringify(strings, null, 2));
}
};
const main = async() => {
const { rocketChatSourceDir } = await prompts({
type: 'text',
name: 'rocketChatSourceDir',
message: 'Where is Rocket.Chat source?',
initial: process.argv[2] || '../Rocket.Chat',
validate: (path) => {
try {
return fs.lstatSync(path).isDirectory();
} catch (e) {
return false;
}
},
});
await importTranslationsFrom(rocketChatSourceDir);
};
require.main === module && main();