make localization function specific and add specific function for locales

This commit is contained in:
Mylloon 2022-07-21 23:38:20 +02:00
parent def329bf67
commit 0cfea23150
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 29 additions and 7 deletions

View file

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

View file

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

View file

@ -34,7 +34,6 @@ export const loadLocales = async () => {
return locales; return locales;
}; };
/** /**
* Builds a dictionary, if a translation is not available, * Builds a dictionary, if a translation is not available,
* we fallback to en-US. * we fallback to en-US.
@ -42,7 +41,7 @@ export const loadLocales = async () => {
* @param text Name of string to fetch * @param text Name of string to fetch
* @returns the dictionary * @returns the dictionary
*/ */
export const getLocale = (client: Client, text: string) => { export const getLocalizations = (client: Client, text: string) => {
const data: Record<string, string> = {}; const data: Record<string, string> = {};
client.locales.forEach((locale, lang) => { client.locales.forEach((locale, lang) => {
@ -56,3 +55,24 @@ export const getLocale = (client: Client, text: string) => {
return data; 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) => {
if (key !== 'default') {
locales.set(key, desired_locales?.get(key) ?? default_locales.get(key));
}
});
return locales;
};