Skip to content

Commit

Permalink
Migrate electron version to i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionel Laské committed Sep 30, 2023
1 parent 380ef1e commit 089c88b
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 83 deletions.
44 changes: 34 additions & 10 deletions lib/l10n.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['i18next.min', 'axios.min'], function (i18next, axios) {
let l10ndefine = function (i18next, axios) {
const l10n = {language: {direction: "ltr"}};

l10n.init = async (lang) => {
Expand All @@ -18,12 +18,26 @@ define(['i18next.min', 'axios.min'], function (i18next, axios) {

l10n.loadLanguageResource = (lang) => {
return new Promise((resolve, reject) => {
axios.get("./locales/" + lang + ".json").then((response) => {
resolve(response.data);
}).catch((error) => {
console.log("Failed to load " + lang + " language: " + error);
resolve(null); // Resolve with null to indicate failure
});
// In web, load language file from server
if (typeof module === 'undefined') {
axios.get("./locales/" + lang + ".json").then((response) => {
resolve(response.data);
}).catch((error) => {
console.log("Failed to load " + lang + " language: " + error);
resolve(null); // Resolve with null to indicate failure
});
} else {
// In node, load language file from disk
const fs = require('fs');
fs.readFile("./locales/" + lang + ".json", (err, data) => {
if (err) {
console.log("Failed to load " + lang + " language: " + err);
resolve(null); // Resolve with null to indicate failure
} else {
resolve(JSON.parse(data));
}
});
}
});
};

Expand All @@ -47,9 +61,19 @@ define(['i18next.min', 'axios.min'], function (i18next, axios) {


function triggerLocalizedEvent() {
const event = new Event("localized");
window.dispatchEvent(event);
if (typeof module === 'undefined') {
const event = new Event("localized");
window.dispatchEvent(event);
}
};

return l10n;
});
}

if (typeof module !== 'undefined') {
let i18next = require('i18next');
let axios = require('axios');
module.exports = l10ndefine(i18next, axios);
} else {
define(['i18next.min', 'axios.min'], l10ndefine);
}
40 changes: 3 additions & 37 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
var electron = require('electron'),
fs = require('fs'),
temp = require('tmp'),
ini = require('ini'),
path = require('path'),
requirejs = require('requirejs');
requirejs = require('requirejs'),
l10n = require('./lib/l10n');

var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
Expand All @@ -22,40 +22,6 @@ var reinit = false;
var logoff = false;


// Localization features
l10n = {
ini: null,
language: '*',

init: function() {
this.language = app.getLocale() || "*";
this.ini = ini.parse(fs.readFileSync(app.getAppPath()+'/locale.ini', 'utf-8'));
},

setLanguage: function(lang) {
this.language = lang;
},

getLanguage: function() {
return this.language;
},

get: function(text, params) {
var locales = this.ini[this.language];
if (!locales) {
locales = this.ini['*'];
}
if (!locales[text]) {
return text;
}
var translate = locales[text];
for (var param in params) {
translate = translate.replace('{{'+param+'}}', params[param]);
}
return translate;
}
}

// Save a file
function saveFile(file, arg, sender) {
var buf;
Expand Down Expand Up @@ -131,7 +97,7 @@ function createWindow () {
// Wait for 'ready-to-show' to display our window
mainWindow.webContents.once('did-finish-load', function() {
// Initialize locales
l10n.init();
l10n.init(app.getLocale() || "en");

// Handle save file dialog
ipc.on('save-file-dialog', function(event, arg) {
Expand Down
Loading

0 comments on commit 089c88b

Please sign in to comment.