Compare commits

..

3 commits

Author SHA1 Message Date
fc3a2640ca
* cleaner locales
* WIP locales sanity
2022-07-22 00:05:49 +02:00
f446913cd5
add method to remove extension from str 2022-07-22 00:04:19 +02:00
0cfea23150
make localization function specific and add specific function for locales 2022-07-21 23:38:20 +02:00
4 changed files with 63 additions and 13 deletions

View file

@ -1,6 +1,6 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { Client, CommandInteraction, Message } from 'discord.js';
import { getLocale } from '../../utils/locales';
import { getLocale, getLocalizations } from '../../utils/locales';
import { getFilename } from '../../utils/misc';
export default {
@ -9,12 +9,12 @@ export default {
return new SlashCommandBuilder()
.setName(filename)
.setDescription(client.locales.get(client.config.default_lang)?.get(`c_${filename}_desc`) ?? '?')
.setNameLocalizations(getLocale(client, `c_${filename}_name`))
.setDescriptionLocalizations(getLocale(client, `c_${filename}_desc`));
.setNameLocalizations(getLocalizations(client, `c_${filename}_name`))
.setDescriptionLocalizations(getLocalizations(client, `c_${filename}_desc`));
},
interaction: async (interaction: CommandInteraction, client: Client) => {
const loc = client.locales.get(interaction.locale) ?? client.locales.get(client.config.default_lang);
const loc = getLocale(client, interaction.locale);
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message;

View file

@ -1,12 +1,14 @@
import { Client, Interaction } from 'discord.js';
import { getLocale } from '../../utils/locales';
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-interactionCreate */
export default (interaction: Interaction, client: Client) => {
if (interaction.isCommand()) {
const command = client.commands.get(interaction.commandName);
if (!command) {
const loc = getLocale(client, interaction.locale);
return interaction.reply({
content: client.locales.get(interaction.locale)?.get('e_interacreate_no_command'),
content: loc.get('e_interacreate_no_command'),
ephemeral: true,
});
}

View file

@ -1,17 +1,17 @@
import { Client } from 'discord.js';
import { readdir } from 'fs/promises';
import { removeExtension } from './misc';
/**
* Load the localizations files
*/
export const loadLocales = async () => {
// Check if there is a defined valid value
const old_path = __dirname.split('/');
old_path.pop();
const files = await readdir(`${old_path.join('/')}/locales`);
const locales = new Map();
const locales = new Map<string, Map<string, string>>();
await Promise.all(
files.map(async lang => {
const content: {
@ -21,9 +21,11 @@ export const loadLocales = async () => {
);
locales.set(
lang.split('.')[0],
removeExtension(lang),
new Map(
Object.keys(content).map(str => {
Object.keys(content)
.filter(str => str !== 'default')
.map(str => {
return [str, content[str]];
}),
)
@ -31,10 +33,12 @@ export const loadLocales = async () => {
})
);
// Check locales sanity
checkLocales(locales);
return locales;
};
/**
* Builds a dictionary, if a translation is not available,
* we fallback to en-US.
@ -42,7 +46,7 @@ export const loadLocales = async () => {
* @param text Name of string to fetch
* @returns the dictionary
*/
export const getLocale = (client: Client, text: string) => {
export const getLocalizations = (client: Client, text: string) => {
const data: Record<string, string> = {};
client.locales.forEach((locale, lang) => {
@ -56,3 +60,35 @@ export const getLocale = (client: Client, text: string) => {
return data;
};
/**
* Return the locale data for a lang,
* fallback to default language when a string isn't available
* @param client Client
* @param lang Lang to fetch
* @returns the map with the desired languaged clogged with the default one
*/
export const getLocale = (client: Client, lang: string) => {
const default_locales = client.locales.get(client.config.default_lang);
const desired_locales = client.locales.get(lang);
const locales = new Map();
default_locales?.forEach((_, key) => {
locales.set(key, desired_locales?.get(key) ?? default_locales.get(key));
});
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;
};

View file

@ -18,3 +18,15 @@ export const getFilename = (path: string) => {
return path_list[path_list.length - 1].split('.')[0];
};
/**
* Remove extension from a filename
* @param filename string of the filename with an extension
* @returns string of the filename without an extension
*/
export const removeExtension = (filename: string) => {
const array = filename.split('.');
array.pop();
return array.join('.');
};