const path = require('path'); const fs = require('fs'); const chalk = require('chalk'); const { Util } = require('../../util/'); class LocaleLoader { constructor(client) { this.client = client; this.languages = {}; } template(locale, index) { // console.log(String.raw`${this.languages[locale][index]}`); return (this.languages[locale] ? this.languages[locale][index] : `Locale \`${locale}\` does not exist.`) || `Language entry \`${locale}:${index}\` does not exist.`; } async loadLanguages() { const _directory = path.join(process.cwd(), 'structure/language/languages'); const directories = fs.readdirSync(_directory); //locale directories, ex. en_us, fi for (const directory of directories) this.loadLanguage(directory); } loadLanguage(language) { const directory = path.join(process.cwd(), `structure/language/languages/${language}`); const files = Util.readdirRecursive(directory); const combined = {}; for(let file of files) { file = fs.readFileSync(file, { encoding: 'utf8' }); const result = this.loadFile(file); Object.assign(combined, result); } this.languages[language] = combined; this.client.logger.info(`${chalk.bold('[LOCAL]')} Language ${chalk.bold(language)} was ${chalk.bold("loaded")}.`); } loadFile(file) { if(process.platform === 'win32') { file = file.split('\n').join(''); file = file.replace(/\r/gu, '\n'); } const lines = file.split('\n'); const parsed = {}; let matched = null; let text = []; for(const line of lines) { if(line.startsWith('//') || line.startsWith('#')) continue; const matches = line.match(/\[([_A-Z0-9]{1,})\]/u); if(matches) { if (matched) { parsed[matched] = text.join('\n').trim(); [, matched] = matches; text = []; } else { [, matched] = matches; } } else if (matched) { text.push(line); } else { continue; } } parsed[matched] = text.join('\n').trim(); return parsed; } } module.exports = LocaleLoader;