Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(locales): Add translations support #796

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions locales/en/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"errors": {
"unsupported_nodejs_version": "Unsupported NodeJS version! Please install Node.js 12, 13, or 14.",
"unknown_error": "Unknown error occurred"
},
"messages": {
"starting_modmail": "Starting Modmail",
"on_nodejs": "on Node.js",
"please_run_npm_ci": "Please run \"npm ci\" before starting the bot",
"error_disallowed_intents": "Error: Disallowed intents specified",
"to_run_the_bot": "To run the bot, you must enable 'Server Members Intent' on your bot's page in the Discord Developer Portal:",
"go_to_discord_developers": "Go to https://discord.com/developers/applications",
"click_on_your_bot": "Click on your bot",
"click_bot_sidebar": "Click 'Bot' on the sidebar",
"turn_on_server_members_intent": "Turn on 'Server Members Intent'",
"please_run_npm_ci_again": "Please run \"npm ci\" again! Package \"{{testedPackage}}\" is missing.",
"is_missing": "is missing.",
"updating_database": "Updating database. This can take a while. Don't close the bot!",
"done": "Done!",
"preparing_plugins": "Preparing plugins...",
"connecting_to_discord": "Connecting to Discord...",
"connected_waiting_servers": "Connected! Waiting for servers to become available...",
"servers_not_available": "Servers did not become available after 15 seconds, continuing start-up anyway",
"single_server_warning": "WARNING: The bot will not work before it's invited to the server.",
"multiple_servers_warning": "WARNING: The bot will not function correctly until it's invited to *all* main servers and the inbox server.",
"both_servers_warning": "WARNING: The bot will not function correctly until it's invited to *both* the main server and the inbox server.",
"initializing": "Initializing...",
"loading_plugins": "Loading plugins...",
"loaded_plugins": "Loaded {{loadedCount}} plugins ({{baseCount}} built-in plugins, {{externalCount}} external plugins)",
"listening_to_dms": "Done! Now listening to DMs."
}
}
32 changes: 32 additions & 0 deletions locales/fr/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"errors": {
"unsupported_nodejs_version": "Version de NodeJS non supportée ! Veuillez installer Node.js 12, 13 ou 14.",
"unknown_error": "Une erreur inconnue s'est produite"
},
"messages": {
"starting_modmail": "Démarrage de Modmail",
"on_nodejs": "sur Node.js",
"please_run_npm_ci": "Veuillez exécuter \"npm ci\" avant de démarrer le bot",
"error_disallowed_intents": "Erreur : Intentions interdites spécifiées",
"to_run_the_bot": "Pour exécuter le bot, vous devez activer 'Server Members Intent' sur la page de votre bot dans le portail développeur Discord :",
"go_to_discord_developers": "Allez sur https://discord.com/developers/applications",
"click_on_your_bot": "Cliquez sur votre bot",
"click_bot_sidebar": "Cliquez sur 'Bot' dans la barre latérale",
"turn_on_server_members_intent": "Activez 'Server Members Intent'",
"please_run_npm_ci_again": "Veuillez exécuter \"npm ci\" à nouveau ! Le package \"{{testedPackage}}\" est manquant.",
"is_missing": "est manquant.",
"updating_database": "Mise à jour de la base de données. Cela peut prendre un certain temps. Ne fermez pas le bot !",
"done": "Terminé !",
"preparing_plugins": "Préparation des plugins...",
"connecting_to_discord": "Connexion à Discord...",
"connected_waiting_servers": "Connecté ! Attente de la disponibilité des serveurs...",
"servers_not_available": "Les serveurs n'ont pas été disponibles après 15 secondes, poursuite du démarrage quand même",
"single_server_warning": "AVERTISSEMENT : Le bot ne fonctionnera pas avant d'être invité sur le serveur.",
"multiple_servers_warning": "AVERTISSEMENT : Le bot ne fonctionnera pas correctement tant qu'il ne sera pas invité à *tous* les serveurs principaux et le serveur de la boîte de réception.",
"both_servers_warning": "AVERTISSEMENT : Le bot ne fonctionnera pas correctement tant qu'il ne sera pas invité à *à la fois* le serveur principal et le serveur de la boîte de réception.",
"initializing": "Initialisation...",
"loading_plugins": "Chargement des plugins...",
"loaded_plugins": "Chargé {{loadedCount}} plugins ({{baseCount}} plugins intégrés, {{externalCount}} plugins externes)",
"listening_to_dms": "Terminé ! Maintenant à l'écoute des MP."
}
}
77 changes: 74 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"express": "^4.18.2",
"helmet": "^7.0.0",
"humanize-duration": "^3.29.0",
"i18next": "^23.8.2",
"i18next-fs-backend": "^2.3.1",
"ini": "^4.1.1",
"json5": "^2.2.3",
"knex": "^2.5.1",
Expand Down
21 changes: 21 additions & 0 deletions src/i18next.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Initialize i18next for translations
const fs = require("fs");
const path = require("path");
const i18next = require("i18next")
const Backend = require("i18next-fs-backend")

i18next
.use(Backend)
.init({
initImmediate: false,
lng: "fr",
fallbackLng: "en",
preload: ["en", "fr"],
ns: ["translation"],
defaultNS: "translation",
backend: {
loadPath: "locales/{{lng}}/{{ns}}.json"
}
})

module.exports = i18next;
25 changes: 14 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//Init translations
const i18next = require("./i18next");

// Verify NodeJS version
const nodeMajorVersion = parseInt(process.versions.node.split(".")[0], 10);
if (nodeMajorVersion < 12) {
console.error("Unsupported NodeJS version! Please install Node.js 12, 13, or 14.");
console.error(i18next.t("errors.unsupported_nodejs_version"));
process.exit(1);
}

Expand All @@ -16,7 +19,7 @@ const path = require("path");
try {
fs.accessSync(path.join(__dirname, "..", "node_modules"));
} catch (e) {
console.error("Please run \"npm ci\" before starting the bot");
console.error(i18next.t("messages.please_run_npm_ci"));
process.exit(1);
}

Expand All @@ -41,14 +44,14 @@ function errorHandler(err) {
// Leave out stack traces for BotErrors (the message has enough info)
console.error(`Error: ${err.message}`);
} else if (err.message === "Disallowed intents specified") {
let fullMessage = "Error: Disallowed intents specified";
let fullMessage = i18next.t("messages.error_disallowed_intents");
fullMessage += "\n\n";
fullMessage += "To run the bot, you must enable 'Server Members Intent' on your bot's page in the Discord Developer Portal:";
fullMessage += i18next.t("messages.to_run_the_bot");
fullMessage += "\n\n";
fullMessage += "1. Go to https://discord.com/developers/applications"
fullMessage += "2. Click on your bot"
fullMessage += "3. Click 'Bot' on the sidebar"
fullMessage += "4. Turn on 'Server Members Intent'"
fullMessage += i18next.t("messages.go_to_discord_developers");
fullMessage += i18next.t("messages.click_on_your_bot");
fullMessage += i18next.t("messages.click_bot_sidebar");
fullMessage += i18next.t("messages.turn_on_server_members_intent");

console.error(fullMessage);
} else if (err instanceof PluginInstallationError) {
Expand Down Expand Up @@ -89,7 +92,7 @@ try {
fs.accessSync(path.join(__dirname, "..", "node_modules", mod))
});
} catch (e) {
console.error(`Please run "npm ci" again! Package "${testedPackage}" is missing.`);
console.error(i18next.t("messages.please_run_npm_ci_again", { testedPackage }));
process.exit(1);
}

Expand All @@ -101,9 +104,9 @@ try {
// Make sure the database is up to date
const [completed, newMigrations] = await knex.migrate.list();
if (newMigrations.length > 0) {
console.log("Updating database. This can take a while. Don't close the bot!");
console.log(i18next.t("messages.updating_database"));
await knex.migrate.latest();
console.log("Done!");
console.log(i18next.t("messages.done"));
}

// Start the bot
Expand Down
28 changes: 17 additions & 11 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require("path");
const config = require("./cfg");
const bot = require("./bot");
const knex = require("./knex");
const i18next = require("./i18next");
const { messageQueue } = require("./queue");
const utils = require("./utils");
const { formatters } = require("./formatters")
Expand All @@ -20,28 +21,28 @@ const {getOrFetchChannel} = require("./utils");

module.exports = {
async start() {
console.log("Preparing plugins...");
console.log(i18next.t("messages.preparing_plugins"));
await installAllPlugins();

console.log("Connecting to Discord...");
console.log(i18next.t("messages.connecting_to_discord"));

bot.once("ready", async () => {
console.log("Connected! Waiting for servers to become available...");
console.log(i18next.t("messages.connected_waiting_servers"));

await (new Promise(resolve => {
const waitNoteTimeout = setTimeout(() => {
console.log("Servers did not become available after 15 seconds, continuing start-up anyway");
console.log(i18next.t("messages.servers_not_available"));
console.log("");

const isSingleServer = config.mainServerId.includes(config.inboxServerId);
if (isSingleServer) {
console.log("WARNING: The bot will not work before it's invited to the server.");
console.log(i18next.t("messages.single_server_warning"));
} else {
const hasMultipleMainServers = config.mainServerId.length > 1;
if (hasMultipleMainServers) {
console.log("WARNING: The bot will not function correctly until it's invited to *all* main servers and the inbox server.");
console.log(i18next.t("messages.multiple_servers_warning"));
} else {
console.log("WARNING: The bot will not function correctly until it's invited to *both* the main server and the inbox server.");
console.log(i18next.t("messages.both_servers_warning"));
}
}

Expand All @@ -59,17 +60,22 @@ module.exports = {
});
}));

console.log("Initializing...");
console.log(i18next.t("messages.initializing"));
initStatus();
initBaseMessageHandlers();
initUpdateNotifications();

console.log("Loading plugins...");
console.log(i18next.t("messages.loading_plugins"));
const pluginResult = await loadAllPlugins();
console.log(`Loaded ${pluginResult.loadedCount} plugins (${pluginResult.baseCount} built-in plugins, ${pluginResult.externalCount} external plugins)`);
console.log(i18next.t("messages.loaded_plugins", {
loadedCount: pluginResult.loadedCount,
baseCount: pluginResult.baseCount,
externalCount: pluginResult.externalCount
}
));

console.log("");
console.log("Done! Now listening to DMs.");
console.log(i18next.t("messages.listening_to_dms"));
console.log("");

const openThreads = await threads.getAllOpenThreads();
Expand Down