mirror of https://github.com/GooseMod/GooseMod
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.6 KiB
59 lines
1.6 KiB
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
|
|
import langs from '../i18n/langs.json';
|
|
|
|
|
|
import { join, resolve, dirname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
const distDir = resolve(join(__dirname, '..', '..', 'dist'));
|
|
const translationsDir = resolve(join(__dirname, '..', 'i18n', 'translations'));
|
|
|
|
const getTranslation = (lang) => {
|
|
const path = join(translationsDir, lang + '.json');
|
|
if (!existsSync(path)) return false;
|
|
|
|
return JSON.parse(readFileSync(path, 'utf8'));
|
|
};
|
|
|
|
const flattenObj = (obj, key = '') => Object.keys(obj).reduce((acc, x) => {
|
|
const k = (key ? key + '.' : '') + x;
|
|
|
|
if (typeof obj[x] === 'object') {
|
|
const f = flattenObj(obj[x], k);
|
|
|
|
for (const y of Object.keys(f)) {
|
|
acc[y] = f[y];
|
|
}
|
|
} else {
|
|
acc[k] = obj[x];
|
|
}
|
|
|
|
return acc;
|
|
}, {});
|
|
|
|
export default (code) => {
|
|
const defaultTranslation = flattenObj(getTranslation('en-US'));
|
|
|
|
if (!existsSync(distDir)) mkdirSync(distDir);
|
|
|
|
for (const lang of Object.keys(langs)) {
|
|
const outPath = join(distDir, 'goosemod.' + lang + '.js');
|
|
|
|
const translation = {
|
|
...defaultTranslation,
|
|
...(flattenObj(getTranslation(lang)) || {})
|
|
};
|
|
|
|
let langCode = code;
|
|
|
|
for (const key in translation) {
|
|
const val = translation[key].replaceAll('\n', '\\n').replaceAll('\'', '\\\'').replaceAll('"', '\\"').replaceAll('`', '\\`').replaceAll('$', '\\$');
|
|
langCode = langCode.replaceAll(`#${key}#`, val);
|
|
}
|
|
|
|
writeFileSync(outPath, langCode);
|
|
}
|
|
}; |