forked from Maps4HTML/MapML.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-locales.js
45 lines (38 loc) · 1.51 KB
/
load-locales.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
// load-locales.js
const fs = require('fs');
const path = require('path');
function loadLocalePlugin() {
return {
name: 'load-locale',
resolveId(source) {
if (source === 'generated-locale') return source;
return null;
},
load(id) {
if (id === 'generated-locale') {
// Load and process each language's messages.json file
const enMessagesPath = path.resolve(__dirname, 'node_modules/mapml-extension/src/_locales/en/messages.json');
const frMessagesPath = path.resolve(__dirname, 'node_modules/mapml-extension/src/_locales/fr/messages.json');
const enMessages = JSON.parse(fs.readFileSync(enMessagesPath, 'utf-8'));
const frMessages = JSON.parse(fs.readFileSync(frMessagesPath, 'utf-8'));
// Function to transform messages.json content to the desired structure
const transformMessages = (messages) => {
return Object.keys(messages).reduce((acc, key) => {
if (key !== 'extName' && key !== 'extDescription') {
acc[key] = messages[key].message;
}
return acc;
}, {});
};
// Generate locale objects
const locale = transformMessages(enMessages);
const localeFr = transformMessages(frMessages);
// Export the transformed objects
return `export const locale = ${JSON.stringify(locale)};
export const localeFr = ${JSON.stringify(localeFr)};`;
}
return null;
}
};
}
module.exports = loadLocalePlugin;