maciejkapaste-backup-2023-2.../pastes/paste_cd114b.txt

34 lines
1.3 KiB
Plaintext
Raw Normal View History

2023-12-31 18:17:29 +01:00
#
# ID: cd114b
# Nazwa: Localisation JS utility
# Opis: Simple Node.js localisation utility. Works with .json language files.
# Publiczny: 1
# Data utworzenia/ostatniej edycji (UTC): 2023-10-16 16:05:29
#
const path = require('node:path');
const fs = require('node:fs');
module.exports = {
language: class {
constructor(lang) {
const languagesPath = path.join(__dirname, '../lang');
if (fs.readdirSync(languagesPath).includes(`${lang}.json`)) {
try {
this.allText = JSON.parse(fs.readFileSync(path.join(languagesPath, `${lang}.json`), 'utf8'));
} catch {
throw new Error(`Couldn't open language file '${lang}.json'. Please make sure it uses UTF-8 encryption.`);
}
} else {
throw new Error(`Language file '${lang}.json' doesn't exist. If you mischanged the LANGUAGE property in .env file, change it to en_GB for English (default).`);
}
}
getText(key) {
if (this.allText == null) {
throw new Error(`Language class has been improperly configured. (Unknown localisation module error)`);
} else {
return this.allText[key];
}
}
}
};