feat: locales #27

Merged
Anri merged 26 commits from lang into main 2022-07-22 11:46:48 +02:00
2 changed files with 9 additions and 42 deletions
Showing only changes of commit 8ec15b7803 - Show all commits

View file

@ -35,7 +35,6 @@ services:
| Nom | Commentaire | Valeur par défaut | Nom | Commentaire | Valeur par défaut
| :-----------: | :-----------: | :-: | :-----------: | :-----------: | :-:
| TOKEN_DISCORD | Token Discord | Aucune | TOKEN_DISCORD | Token Discord | Aucune
| LANGUAGE | Langue du bot | en
--- ---
### Références ### Références

View file

@ -1,17 +1,20 @@
import { Client, Collection, Intents } from 'discord.js'; import { Client, Collection, Intents } from 'discord.js';
import { readFileSync, existsSync } from 'fs'; import { readFileSync } from 'fs';
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { loadLocales } from './locales';
const { version } = JSON.parse(readFileSync('./package.json').toString()); const { version } = JSON.parse(readFileSync('./package.json').toString());
declare module 'discord.js' { declare module 'discord.js' {
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
export interface Client { export interface Client {
/** Store the configuration */
config: { config: {
version: string, version: string,
token_discord: string | undefined, token_discord: string | undefined,
lang: string default_lang: string
}, },
/** Store all the slash commands */
commands: Collection< commands: Collection<
string, string,
{ {
@ -19,7 +22,8 @@ declare module 'discord.js' {
interaction: (interaction: CommandInteraction, client: Client) => unknown 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 = { client.config = {
version: version, version: version,
token_discord: process.env.TOKEN_DISCORD, token_discord: process.env.TOKEN_DISCORD,
lang: process.env.LANGUAGE ?? '', default_lang: 'en-US',
}; };
// Store the commands available
client.commands = new Collection(); client.commands = new Collection();
// Store all the strings client.locales = await loadLocales(client.config.default_lang);
client.locale = await getLocale(client);
return client; 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]];
}),
);
};