Compare commits

..

No commits in common. "fc3a2640caae117c7437902e8c554dd914af6bf5" and "def329bf679dc4ca59a4e5a77d8eac997422d3bf" have entirely different histories.

4 changed files with 13 additions and 63 deletions

View file

@ -1,6 +1,6 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { Client, CommandInteraction, Message } from 'discord.js';
import { getLocale, getLocalizations } from '../../utils/locales';
import { getLocale } 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(getLocalizations(client, `c_${filename}_name`))
.setDescriptionLocalizations(getLocalizations(client, `c_${filename}_desc`));
.setNameLocalizations(getLocale(client, `c_${filename}_name`))
.setDescriptionLocalizations(getLocale(client, `c_${filename}_desc`));
},
interaction: async (interaction: CommandInteraction, client: Client) => {
const loc = getLocale(client, interaction.locale);
const loc = client.locales.get(interaction.locale) ?? client.locales.get(client.config.default_lang);
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message;

View file

@ -1,14 +1,12 @@
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: loc.get('e_interacreate_no_command'),
content: client.locales.get(interaction.locale)?.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<string, Map<string, string>>();
const locales = new Map();
await Promise.all(
files.map(async lang => {
const content: {
@ -21,24 +21,20 @@ export const loadLocales = async () => {
);
locales.set(
removeExtension(lang),
lang.split('.')[0],
new Map(
Object.keys(content)
.filter(str => str !== 'default')
.map(str => {
return [str, content[str]];
}),
Object.keys(content).map(str => {
return [str, content[str]];
}),
)
);
})
);
// Check locales sanity
checkLocales(locales);
return locales;
};
/**
* Builds a dictionary, if a translation is not available,
* we fallback to en-US.
@ -46,7 +42,7 @@ export const loadLocales = async () => {
* @param text Name of string to fetch
* @returns the dictionary
*/
export const getLocalizations = (client: Client, text: string) => {
export const getLocale = (client: Client, text: string) => {
const data: Record<string, string> = {};
client.locales.forEach((locale, lang) => {
@ -60,35 +56,3 @@ export const getLocalizations = (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,15 +18,3 @@ 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('.');
};