Compare commits
No commits in common. "fc3a2640caae117c7437902e8c554dd914af6bf5" and "def329bf679dc4ca59a4e5a77d8eac997422d3bf" have entirely different histories.
fc3a2640ca
...
def329bf67
4 changed files with 13 additions and 63 deletions
|
@ -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, getLocalizations } from '../../utils/locales';
|
import { getLocale } 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(getLocalizations(client, `c_${filename}_name`))
|
.setNameLocalizations(getLocale(client, `c_${filename}_name`))
|
||||||
.setDescriptionLocalizations(getLocalizations(client, `c_${filename}_desc`));
|
.setDescriptionLocalizations(getLocale(client, `c_${filename}_desc`));
|
||||||
},
|
},
|
||||||
|
|
||||||
interaction: async (interaction: CommandInteraction, client: Client) => {
|
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;
|
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message;
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
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: loc.get('e_interacreate_no_command'),
|
content: client.locales.get(interaction.locale)?.get('e_interacreate_no_command'),
|
||||||
ephemeral: true,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
import { Client } from 'discord.js';
|
import { Client } from 'discord.js';
|
||||||
import { readdir } from 'fs/promises';
|
import { readdir } from 'fs/promises';
|
||||||
import { removeExtension } from './misc';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the localizations files
|
* Load the localizations files
|
||||||
*/
|
*/
|
||||||
export const loadLocales = async () => {
|
export const loadLocales = async () => {
|
||||||
|
// Check if there is a defined valid value
|
||||||
const old_path = __dirname.split('/');
|
const old_path = __dirname.split('/');
|
||||||
old_path.pop();
|
old_path.pop();
|
||||||
|
|
||||||
const files = await readdir(`${old_path.join('/')}/locales`);
|
const files = await readdir(`${old_path.join('/')}/locales`);
|
||||||
|
|
||||||
const locales = new Map<string, Map<string, string>>();
|
const locales = new Map();
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
files.map(async lang => {
|
files.map(async lang => {
|
||||||
const content: {
|
const content: {
|
||||||
|
@ -21,24 +21,20 @@ export const loadLocales = async () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
locales.set(
|
locales.set(
|
||||||
removeExtension(lang),
|
lang.split('.')[0],
|
||||||
new Map(
|
new Map(
|
||||||
Object.keys(content)
|
Object.keys(content).map(str => {
|
||||||
.filter(str => str !== 'default')
|
return [str, content[str]];
|
||||||
.map(str => {
|
}),
|
||||||
return [str, content[str]];
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check locales sanity
|
|
||||||
checkLocales(locales);
|
|
||||||
|
|
||||||
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.
|
||||||
|
@ -46,7 +42,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 getLocalizations = (client: Client, text: string) => {
|
export const getLocale = (client: Client, text: string) => {
|
||||||
const data: Record<string, string> = {};
|
const data: Record<string, string> = {};
|
||||||
|
|
||||||
client.locales.forEach((locale, lang) => {
|
client.locales.forEach((locale, lang) => {
|
||||||
|
@ -60,35 +56,3 @@ export const getLocalizations = (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) => {
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
|
@ -18,15 +18,3 @@ export const getFilename = (path: string) => {
|
||||||
|
|
||||||
return path_list[path_list.length - 1].split('.')[0];
|
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('.');
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in a new issue