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 { 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

@ -34,7 +34,6 @@ export const loadLocales = async () => {
return locales;
};
/**
* Builds a dictionary, if a translation is not available,
* we fallback to en-US.
@ -42,7 +41,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 +55,24 @@ 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) => {
if (key !== 'default') {
locales.set(key, desired_locales?.get(key) ?? default_locales.get(key));
}
});
return locales;
};