load all languages at once, method for fetching templates

This commit is contained in:
Erik 2020-04-19 22:52:42 +03:00
parent a5a91b5001
commit ad812cf8f8

View File

@ -1,5 +1,6 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const chalk = require('chalk');
const { Util } = require('../util/'); const { Util } = require('../util/');
@ -12,9 +13,24 @@ class LocaleLoader {
} }
// directory = path.join(process.cwd(), 'languages', 'en_us') template(locale, index) {
return this.languages[locale] ? this.languages[locale][index] : `Locale ${locale} does not exist.` || `Entry ${locale}:${index} does not exist.`;
}
async loadLanguages() {
const _directory = path.join(process.cwd(), 'language/languages');
const directories = fs.readdirSync(_directory); //locale directories, ex. en_us, fi
for (const directory of directories) await this.loadLanguage(directory);
}
async loadLanguage(language) { async loadLanguage(language) {
this.client.logger.info(`Loading locale ${chalk.bold(language)}`);
const directory = path.join(process.cwd(), `language/languages/${language}`); const directory = path.join(process.cwd(), `language/languages/${language}`);
const files = Util.readdirRecursive(directory); const files = Util.readdirRecursive(directory);
@ -23,16 +39,15 @@ class LocaleLoader {
file = fs.readFileSync(file, { file = fs.readFileSync(file, {
encoding: 'utf8' encoding: 'utf8'
}); });
const result = this.loadFile(language, file); const result = this.loadFile(file);
Object.assign(combined, result); Object.assign(combined, result);
} }
this.languages[language] = combined; this.languages[language] = combined;
console.log(this.languages);
} }
loadFile(language, file) { loadFile(file) {
const lines = file.split('\n'); const lines = file.split('\n');
const parsed = {}; const parsed = {};