Compare commits
No commits in common. "8f0096a73cb764c40cf609ac88d7305ab40a6391" and "8ec15b78038cc67f68a28d8de9524dc87184ab2b" have entirely different histories.
8f0096a73c
...
8ec15b7803
4 changed files with 3 additions and 64 deletions
|
@ -4,8 +4,8 @@ import { getLocale } from '../../utils/locales';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data: (client: Client) => new SlashCommandBuilder()
|
data: (client: Client) => new SlashCommandBuilder()
|
||||||
.setNameLocalizations(getLocale(client, 'c_ping_name'))
|
.setNameLocalizations(getLocale(client, 'ping_name'))
|
||||||
.setDescriptionLocalizations(getLocale(client, 'c_ping_desc')),
|
.setDescriptionLocalizations(getLocale(client, 'ping_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 = client.locales.get(interaction.locale) ?? client.locales.get(client.config.default_lang);
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
{
|
{
|
||||||
"e_interacreate": "Sorry, the command probably no longer exists...",
|
"e_interacreate": "Sorry, the command probably no longer exists...",
|
||||||
|
|
||||||
"c_ping_name": "Ping",
|
|
||||||
"c_ping_desc": "Pong",
|
|
||||||
"c_ping1": "Roundtrip latency",
|
"c_ping1": "Roundtrip latency",
|
||||||
"c_ping2": "Websocket heartbeat"
|
"c_ping2": "Websocket heartbeat"
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ export default async () => {
|
||||||
|
|
||||||
client.commands = new Collection();
|
client.commands = new Collection();
|
||||||
|
|
||||||
client.locales = await loadLocales();
|
client.locales = await loadLocales(client.config.default_lang);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,59 +0,0 @@
|
||||||
import { Client } from 'discord.js';
|
|
||||||
import { readdir } from 'fs/promises';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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();
|
|
||||||
await Promise.all(
|
|
||||||
files.map(async lang => {
|
|
||||||
const content: {
|
|
||||||
[key: string]: string
|
|
||||||
} = await import(
|
|
||||||
`../locales/${lang}`
|
|
||||||
);
|
|
||||||
|
|
||||||
locales.set(
|
|
||||||
lang.split('.')[0],
|
|
||||||
new Map(
|
|
||||||
Object.keys(content).map(str => {
|
|
||||||
return [str, content[str]];
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return locales;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Builds a dictionary, if a translation is not available,
|
|
||||||
* we fallback to en-US.
|
|
||||||
* @param client Client
|
|
||||||
* @param text Name of string to fetch
|
|
||||||
* @returns the dictionary
|
|
||||||
*/
|
|
||||||
export const getLocale = (client: Client, text: string) => {
|
|
||||||
const data: Record<string, string> = {};
|
|
||||||
|
|
||||||
client.locales.forEach((locale, lang) => {
|
|
||||||
const str = locale.get(text)
|
|
||||||
?? client.locales.get(client.config.default_lang)?.get(text);
|
|
||||||
|
|
||||||
if (str === undefined) {
|
|
||||||
throw 'Missing locales';
|
|
||||||
}
|
|
||||||
data[lang] = str;
|
|
||||||
});
|
|
||||||
|
|
||||||
return data;
|
|
||||||
};
|
|
Loading…
Reference in a new issue