* language relative to user, not global anymore
* add comments * store all the localizations instead of unique one * move localization function to utils/ folder
This commit is contained in:
parent
1b4a594880
commit
8ec15b7803
2 changed files with 9 additions and 42 deletions
|
@ -35,7 +35,6 @@ services:
|
|||
| Nom | Commentaire | Valeur par défaut
|
||||
| :-----------: | :-----------: | :-:
|
||||
| TOKEN_DISCORD | Token Discord | Aucune
|
||||
| LANGUAGE | Langue du bot | en
|
||||
|
||||
---
|
||||
### Références
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
import { Client, Collection, Intents } from 'discord.js';
|
||||
import { readFileSync, existsSync } from 'fs';
|
||||
import { readFileSync } from 'fs';
|
||||
import { SlashCommandBuilder } from '@discordjs/builders';
|
||||
import { loadLocales } from './locales';
|
||||
|
||||
const { version } = JSON.parse(readFileSync('./package.json').toString());
|
||||
|
||||
declare module 'discord.js' {
|
||||
// eslint-disable-next-line no-shadow
|
||||
export interface Client {
|
||||
/** Store the configuration */
|
||||
config: {
|
||||
version: string,
|
||||
token_discord: string | undefined,
|
||||
lang: string
|
||||
default_lang: string
|
||||
},
|
||||
/** Store all the slash commands */
|
||||
commands: Collection<
|
||||
string,
|
||||
{
|
||||
|
@ -19,7 +22,8 @@ declare module 'discord.js' {
|
|||
interaction: (interaction: CommandInteraction, client: Client) => unknown
|
||||
}
|
||||
>,
|
||||
locale: Map<string, string>
|
||||
/** Store all the localizations */
|
||||
locales: Map<string, Map<string, string>>
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,51 +35,15 @@ export default async () => {
|
|||
],
|
||||
});
|
||||
|
||||
// Store the client configuration
|
||||
client.config = {
|
||||
version: version,
|
||||
token_discord: process.env.TOKEN_DISCORD,
|
||||
lang: process.env.LANGUAGE ?? '',
|
||||
default_lang: 'en-US',
|
||||
};
|
||||
|
||||
// Store the commands available
|
||||
client.commands = new Collection();
|
||||
|
||||
// Store all the strings
|
||||
client.locale = await getLocale(client);
|
||||
client.locales = await loadLocales(client.config.default_lang);
|
||||
|
||||
return client;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get all the strings corresponding to the locales specified in env variable
|
||||
* @returns Collection of strings
|
||||
*/
|
||||
const getLocale = async (client: Client) => {
|
||||
const default_lang = 'en-US';
|
||||
|
||||
// Check if there is a defined valid value
|
||||
const old_path = __dirname.split('/');
|
||||
old_path.pop();
|
||||
const lang = client.config.lang === undefined ? default_lang : existsSync(
|
||||
`${old_path.join('/')}/locales/${client.config.lang}.json`,
|
||||
) ? client.config.lang : default_lang;
|
||||
|
||||
// Fallback to default language in case lang isn't valid
|
||||
if (client.config.lang !== lang) {
|
||||
client.config.lang = lang;
|
||||
}
|
||||
|
||||
const file: {
|
||||
[key: string]: string
|
||||
} = await import(
|
||||
`../locales/${lang}.json`
|
||||
);
|
||||
|
||||
return new Map(
|
||||
Object.keys(file).map(key => {
|
||||
return [key, file[key]];
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue