2020-04-19 12:12:10 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2020-04-19 21:52:42 +02:00
|
|
|
const chalk = require('chalk');
|
2020-04-19 12:12:10 +02:00
|
|
|
|
|
|
|
const { Util } = require('../util/');
|
|
|
|
|
2020-04-17 17:23:13 +02:00
|
|
|
class LocaleLoader {
|
|
|
|
|
2020-04-19 12:12:10 +02:00
|
|
|
constructor(client) {
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
this.languages = {};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:52:42 +02:00
|
|
|
template(locale, index) {
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
// console.log(String.raw`${this.languages[locale][index]}`);
|
2020-05-08 08:50:54 +02:00
|
|
|
return (this.languages[locale] ? this.languages[locale][index] : `Locale \`${locale}\` does not exist.`) || `Entry \`${locale}:${index}\` does not exist.`;
|
2020-04-19 21:52:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadLanguages() {
|
|
|
|
|
|
|
|
const _directory = path.join(process.cwd(), 'language/languages');
|
|
|
|
const directories = fs.readdirSync(_directory); //locale directories, ex. en_us, fi
|
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
for (const directory of directories) this.loadLanguage(directory);
|
2020-04-19 21:52:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
loadLanguage(language) {
|
2020-04-19 12:12:10 +02:00
|
|
|
|
2020-04-19 21:52:42 +02:00
|
|
|
this.client.logger.info(`Loading locale ${chalk.bold(language)}`);
|
2020-04-19 12:12:10 +02:00
|
|
|
const directory = path.join(process.cwd(), `language/languages/${language}`);
|
|
|
|
const files = Util.readdirRecursive(directory);
|
|
|
|
|
2020-05-24 11:00:54 +02:00
|
|
|
const combined = {};
|
2020-04-19 12:12:10 +02:00
|
|
|
for(let file of files) {
|
|
|
|
file = fs.readFileSync(file, {
|
|
|
|
encoding: 'utf8'
|
|
|
|
});
|
2020-04-19 21:52:42 +02:00
|
|
|
const result = this.loadFile(file);
|
2020-04-19 12:12:10 +02:00
|
|
|
Object.assign(combined, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.languages[language] = combined;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:52:42 +02:00
|
|
|
loadFile(file) {
|
2020-04-19 12:12:10 +02:00
|
|
|
|
|
|
|
const lines = file.split('\n');
|
|
|
|
const parsed = {};
|
|
|
|
|
|
|
|
let matched = null;
|
|
|
|
let text = '';
|
|
|
|
for(const line of lines) {
|
2020-05-06 01:40:46 +02:00
|
|
|
if(line.startsWith('//')) continue;
|
2020-05-24 10:54:33 +02:00
|
|
|
const matches = line.match(/\[([_A-Z0-9]{1,})\]/u);
|
2020-04-19 12:12:10 +02:00
|
|
|
if(matches) {
|
2020-05-24 11:00:54 +02:00
|
|
|
if (matched) {
|
2020-04-19 12:12:10 +02:00
|
|
|
parsed[matched] = text;
|
2020-05-24 11:00:54 +02:00
|
|
|
[, matched] = matches;
|
2020-04-19 12:12:10 +02:00
|
|
|
text = '';
|
|
|
|
} else {
|
2020-05-24 11:00:54 +02:00
|
|
|
[, matched] = matches;
|
2020-04-19 12:12:10 +02:00
|
|
|
}
|
2020-05-24 11:00:54 +02:00
|
|
|
} else if(matched) {
|
|
|
|
text += line;
|
2020-04-19 12:12:10 +02:00
|
|
|
} else {
|
2020-05-24 11:00:54 +02:00
|
|
|
continue;
|
2020-04-19 12:12:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-08 08:50:54 +02:00
|
|
|
parsed[matched] = text;
|
2020-04-19 12:12:10 +02:00
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
for(const [ key, value ] of Object.entries(parsed)) {
|
2020-05-24 11:00:54 +02:00
|
|
|
parsed[key] = value.replace(/^(\r)|(\r){1,}$/gu, '').replace(/\r/gu, '\n');
|
2020-04-19 12:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
|
|
|
|
}
|
2020-04-17 17:23:13 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LocaleLoader;
|