feat: locales #27
2 changed files with 64 additions and 10 deletions
|
@ -43,7 +43,8 @@ export default async () => {
|
|||
|
||||
client.commands = new Collection();
|
||||
|
||||
client.locales = await loadLocales();
|
||||
console.log('Localizations progression :');
|
||||
client.locales = await loadLocales(client.config.default_lang);
|
||||
|
||||
return client;
|
||||
};
|
||||
|
|
|
@ -3,9 +3,11 @@ import { readdir } from 'fs/promises';
|
|||
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('/');
|
||||
old_path.pop();
|
||||
|
||||
|
@ -34,7 +36,7 @@ export const loadLocales = async () => {
|
|||
);
|
||||
|
||||
// Check locales sanity
|
||||
checkLocales(locales);
|
||||
checkLocales(locales, default_lang);
|
||||
|
||||
return locales;
|
||||
};
|
||||
|
@ -81,14 +83,65 @@ export const getLocale = (client: Client, lang: string) => {
|
|||
};
|
||||
|
||||
/**
|
||||
* Check if locales are sane
|
||||
* WARN if translation aren't 100%
|
||||
* ERROR if default lang isn't 100%
|
||||
* Show percentage of translation progression
|
||||
*
|
||||
* Raise an error if the default lang isn't
|
||||
* the lang with most text
|
||||
* @param locales Locales loaded
|
||||
* @param default_lang default lang
|
||||
* @returns void
|
||||
*/
|
||||
export const checkLocales = async (locales: Map<string, Map<string, string>>) => {
|
||||
console.log(locales);
|
||||
export const checkLocales =
|
||||
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}%`);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue