statki/utils/localisation.js
MaciejkaG 38ae0c7796 Major changes
- Multiple translation improvements
- Added language switch in settings
- Minor fixes and improvements

- Still need to fix error messages (they are hardcoded in polish)
2024-04-06 01:48:49 +02:00

49 lines
1.4 KiB
JavaScript

import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export class Lang {
constructor(langs) {
const languagesPath = path.join(__dirname, '../lang');
this.allText = null;
for (let i = 0; i < langs.length; i++) {
const lang = langs[i];
if (fs.readdirSync(languagesPath).includes(`${lang}.json`)) {
try {
this.allText = JSON.parse(fs.readFileSync(path.join(languagesPath, `${lang}.json`), 'utf8'));
this.lang = lang;
return;
} catch (e) {
console.log(e);
}
}
}
this.allText = JSON.parse(fs.readFileSync(path.join(languagesPath, 'en.json'), 'utf8'));
this.lang = 'en';
}
t(key) {
if (this.allText == null) {
throw new Error(`Language class has been improperly configured. (Unknown localisation module error)`);
} else {
let keySplit = key.split(".");
try {
return keySplit.reduce((x, y) => x[y], this.allText);
} catch (e) {
if (e instanceof TypeError) {
return "LocKeyErr"
}
}
}
}
}