check if default language is correct and add progression bar for all the other languages

This commit is contained in:
Mylloon 2022-07-22 01:13:28 +02:00
parent 456ebae87b
commit b6247551dc
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 64 additions and 10 deletions

View file

@ -43,7 +43,8 @@ export default async () => {
client.commands = new Collection(); client.commands = new Collection();
client.locales = await loadLocales(); console.log('Localizations progression :');
client.locales = await loadLocales(client.config.default_lang);
return client; return client;
}; };

View file

@ -3,9 +3,11 @@ import { readdir } from 'fs/promises';
import { removeExtension } from './misc'; import { removeExtension } from './misc';
/** /**
* Load the localizations files * Load the localizations files into memory
* @param default_lang default lang
* @returns Map of map with all the localizations
*/ */
export const loadLocales = async () => { export const loadLocales = async (default_lang: string) => {
const old_path = __dirname.split('/'); const old_path = __dirname.split('/');
old_path.pop(); old_path.pop();
@ -34,7 +36,7 @@ export const loadLocales = async () => {
); );
// Check locales sanity // Check locales sanity
checkLocales(locales); checkLocales(locales, default_lang);
return locales; return locales;
}; };
@ -81,14 +83,65 @@ export const getLocale = (client: Client, lang: string) => {
}; };
/** /**
* Check if locales are sane * Show percentage of translation progression
* WARN if translation aren't 100% *
* ERROR if default lang isn't 100% * Raise an error if the default lang isn't
* the lang with most text
* @param locales Locales loaded * @param locales Locales loaded
* @param default_lang default lang
* @returns void * @returns void
*/ */
export const checkLocales = async (locales: Map<string, Map<string, string>>) => { export const checkLocales =
console.log(locales); async (locales: Map<string, Map<string, string>>, default_lang: string) => {
// Associate each lang with the number of locale it has
let locales_size = new Map<string, number>();
locales.forEach((locales_data, lang) => {
locales_size.set(lang, locales_data.size);
});
return; // Sort the map
locales_size = new Map([...locales_size.entries()]
.sort((a, b) => b[1] - a[1]));
// Check if default lang is 100%
const [max_size_name] = locales_size.keys();
const [max_size] = locales_size.values();
const default_lang_size = locales_size.get(default_lang) ?? 0;
if (max_size > default_lang_size) {
throw new Error(
`The default locale (${default_lang} = ${default_lang_size}) isn't complete `
+ `(${max_size_name} = ${max_size}).`
);
}
// Remove the default language as it is used as a reference
locales_size.delete(default_lang);
// Displays the percentages according to the default language
// lower is bigger
const bar_size = 4;
locales_size.forEach((size, lang) => {
const percentage = (size / max_size) * 100;
const blocks = ' '.repeat(percentage / bar_size);
const blank = ' '.repeat((100 - percentage) / bar_size);
const color = () => {
switch (true) {
case percentage <= 25:
// red
return '\x1b[41m';
case percentage <= 50:
// mangeta
return '\x1b[45m';
case percentage <= 75:
// cyan
return '\x1b[46m';
case percentage <= 100:
// green
return '\x1b[42m';
default:
return '';
}
};
console.log(`${lang} | ${color()}${blocks}\x1b[0m${blank} | ${percentage}%`);
});
}; };