Translations for main.js

This commit is contained in:
aandrew-me 2022-10-27 12:49:15 +06:00
parent bb16ecfb41
commit 50a6d570ae

46
main.js
View File

@ -1,5 +1,8 @@
const { app, BrowserWindow, dialog, ipcMain, shell } = require("electron"); const { app, BrowserWindow, dialog, ipcMain, shell } = require("electron");
const { autoUpdater } = require("electron-updater"); const { autoUpdater } = require("electron-updater");
const fs = require("fs");
const path = require("path");
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
let win, secondaryWindow; let win, secondaryWindow;
@ -29,12 +32,30 @@ function createWindow() {
// win.webContents.openDevTools(); // win.webContents.openDevTools();
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
} }
let loadedLanguage;
app.whenReady().then(() => { app.whenReady().then(() => {
// Logging // Logging
console.log("Locale:" + app.getLocale()); console.log("Locale:" + app.getLocale());
console.log("Version: " + app.getVersion()); console.log("Version: " + app.getVersion());
let locale = app.getLocale();
if (fs.existsSync(path.join(__dirname, "translations", locale + ".json"))) {
loadedLanguage = JSON.parse(
fs.readFileSync(
path.join(__dirname, "translations", locale + ".json"),
"utf8"
)
);
} else {
loadedLanguage = JSON.parse(
fs.readFileSync(
path.join(__dirname, "translations", "en.json"),
"utf8"
)
);
}
createWindow(); createWindow();
app.on("activate", () => { app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) { if (BrowserWindow.getAllWindows().length === 0) {
@ -102,10 +123,12 @@ autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
if (process.platform === "darwin") { if (process.platform === "darwin") {
const dialogOpts = { const dialogOpts = {
type: "info", type: "info",
buttons: ["Download", "No"], buttons: [i18n("Download"), i18n("No")],
title: "Update Available", title: "Update Available",
detail: releaseName, detail: releaseName,
message: "A new version is available, do you want to download it?", message: i18n(
"A new version is available, do you want to download it?"
),
}; };
dialog.showMessageBox(dialogOpts).then((returnValue) => { dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) { if (returnValue.response === 0) {
@ -119,10 +142,10 @@ autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
else { else {
const dialogOpts = { const dialogOpts = {
type: "info", type: "info",
buttons: ["Update", "No"], buttons: [i18n("Update"), i18n("No")],
title: "Update Available", title: "Update Available",
detail: process.platform === "win32" ? releaseNotes : releaseName, detail: process.platform === "win32" ? releaseNotes : releaseName,
message: "A new version is available, do you want to update?", message: i18n("A new version is available, do you want to update?"),
}; };
dialog.showMessageBox(dialogOpts).then((returnValue) => { dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) { if (returnValue.response === 0) {
@ -135,9 +158,9 @@ autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => { autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
const dialogOpts = { const dialogOpts = {
type: "info", type: "info",
buttons: ["Restart", "Later"], buttons: [i18n("Restart"), i18n("Later")],
title: "Update Ready", title: "Update Ready",
message: "Install and restart now?", message: i18n("Install and restart now?"),
}; };
dialog.showMessageBox(dialogOpts).then((returnValue) => { dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) { if (returnValue.response === 0) {
@ -147,3 +170,12 @@ autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
} }
}); });
}); });
// Translation
function i18n(phrase) {
let translation = loadedLanguage[phrase];
if (translation === undefined) {
translation = phrase;
}
return translation;
}