feat: locales #27

Merged
Anri merged 26 commits from lang into main 2022-07-22 11:46:48 +02:00
Showing only changes of commit fc3a2640ca - Show all commits

View file

@ -1,17 +1,17 @@
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
import { removeExtension } from './misc';
/** /**
* Load the localizations files * Load the localizations files
*/ */
export const loadLocales = async () => { export const loadLocales = async () => {
// Check if there is a defined valid value
const old_path = __dirname.split('/'); const old_path = __dirname.split('/');
old_path.pop(); old_path.pop();
const files = await readdir(`${old_path.join('/')}/locales`); const files = await readdir(`${old_path.join('/')}/locales`);
const locales = new Map(); const locales = new Map<string, Map<string, string>>();
await Promise.all( await Promise.all(
files.map(async lang => { files.map(async lang => {
const content: { const content: {
@ -21,16 +21,21 @@ export const loadLocales = async () => {
); );
locales.set( locales.set(
lang.split('.')[0], removeExtension(lang),
new Map( new Map(
Object.keys(content).map(str => { Object.keys(content)
return [str, content[str]]; .filter(str => str !== 'default')
}), .map(str => {
return [str, content[str]];
}),
) )
); );
}) })
); );
// Check locales sanity
checkLocales(locales);
return locales; return locales;
}; };
@ -69,10 +74,21 @@ export const getLocale = (client: Client, lang: string) => {
const locales = new Map(); const locales = new Map();
default_locales?.forEach((_, key) => { default_locales?.forEach((_, key) => {
if (key !== 'default') { locales.set(key, desired_locales?.get(key) ?? default_locales.get(key));
locales.set(key, desired_locales?.get(key) ?? default_locales.get(key));
}
}); });
return locales; return locales;
}; };
/**
* Check if locales are sane
* WARN if translation aren't 100%
* ERROR if default lang isn't 100%
* @param locales Locales loaded
* @returns void
*/
export const checkLocales = async (locales: Map<string, Map<string, string>>) => {
console.log(locales);
return;
};