-
Notifications
You must be signed in to change notification settings - Fork 12
/
po-converter.js
31 lines (29 loc) · 1.1 KB
/
po-converter.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
const { readdirSync, readFileSync, writeFileSync } = require('fs');
const converter = require('i18next-conv');
const i18nDir = './i18n';
const options = {
skipUntranslated: true,
keyseparator: '°#°#°#°#°'
};
const translations = {};
const files = readdirSync(i18nDir);
const numberOfFiles = files.length;
const storeTranslationsForIndex = (index) => {
if ( index >= 0 && index < numberOfFiles) {
const file = files[index];
const fileSplittend = file.split('.');
if (fileSplittend.length === 2 && fileSplittend[1] === 'po') {
const fileContent = readFileSync(i18nDir + '/' + file, 'utf-8');
const locale = fileSplittend[0];
converter.gettextToI18next(locale, fileContent, options).then(function (data) {
translations[locale] = { translation: JSON.parse(data) };
storeTranslationsForIndex(index + 1);
});
} else {
storeTranslationsForIndex(index + 1);
}
} else {
writeFileSync('./i18n/resources.js', "const i18nextResources = " + JSON.stringify(translations, null, 2) + "; export default i18nextResources;");
}
};
storeTranslationsForIndex(0);