From 63657db3be5037688a6a3c43b30c8499d2c2459c Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 3 Apr 2017 23:32:46 -0700 Subject: [PATCH] Quick fix for i18n Just use the english locale for now. Load locale data from the filesystem in the main process and pass it to the renderer preload script via ipc. Note that we need the locale data to be available by the time view scripts are loaded. // FREEBIE --- background.html | 1 + js/chromium.js | 24 +++++++++++++----------- js/i18n.js | 22 ++++++++++++++++++++++ main.js | 13 ++++++++++++- preload.js | 3 +++ 5 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 js/i18n.js diff --git a/background.html b/background.html index f4ac3c0e4..06eabaf8e 100644 --- a/background.html +++ b/background.html @@ -731,6 +731,7 @@ + diff --git a/js/chromium.js b/js/chromium.js index bfb57b32e..ff61b8519 100644 --- a/js/chromium.js +++ b/js/chromium.js @@ -153,17 +153,19 @@ }; // Translate - window.i18n = function(message, substitutions) { - if (window.chrome && chrome.i18n) { - return chrome.i18n.getMessage(message, substitutions); - } - }; - i18n.getLocale = function() { - if (window.chrome && chrome.i18n) { - return chrome.i18n.getUILanguage(); - } - return 'en'; - }; + + if (window.chrome && window.chrome.i18n) { + window.i18n = function(message, substitutions) { + return chrome.i18n.getMessage(message, substitutions); + }; + + i18n.getLocale = function() { + if (window.chrome && chrome.i18n) { + return chrome.i18n.getUILanguage(); + } + return 'en'; + }; + } extension.install = function(mode) { if (mode === 'standalone') { diff --git a/js/i18n.js b/js/i18n.js new file mode 100644 index 000000000..7e5c50cf6 --- /dev/null +++ b/js/i18n.js @@ -0,0 +1,22 @@ +/* + * vim: ts=4:sw=4:expandtab + */ +;(function() { + 'use strict'; + var json = window.env.locale_json; + window.i18n = function (message, substitutions) { + var s = json[message] ? json[message].message : message; + if (substitutions instanceof Array) { + substitutions.forEach(function(sub) { + s = s.replace(/\$.+?\$/, sub); + }); + } else if (substitutions) { + s = s.replace(/\$.+?\$/, substitutions); + } + return s; + }; + + i18n.getLocale = function() { + return window.env.locale; + }; +})(); diff --git a/main.js b/main.js index 119cbddcb..a697cd8e0 100644 --- a/main.js +++ b/main.js @@ -6,6 +6,7 @@ const url = require('url') const fs = require('fs') const autoUpdater = require('electron-updater').autoUpdater const autoUpdaterInterval = 60 * 60 * 1000; +const ipc = electron.ipcMain; app.setAppUserModelId('org.whispersystems.signal-desktop') @@ -47,12 +48,22 @@ function createWindow () { } }) + // Load locale + const locale = 'en'; // FIXME + const localeData = JSON.parse(fs.readFileSync(path.join(__dirname, '_locales', locale, 'messages.json'), 'utf-8')) + ipc.on('locale-data', function(event, arg) { + event.returnValue = localeData; + }); + // and load the index.html of the app. mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'background.html'), protocol: 'file:', slashes: true, - query: { node_env: NODE_ENV } + query: { + node_env: NODE_ENV, + locale: locale + } })) // Open the DevTools. diff --git a/preload.js b/preload.js index dc24352c6..c2ca1d3a7 100644 --- a/preload.js +++ b/preload.js @@ -8,3 +8,6 @@ window.location.search.substring(1).split('&').forEach(function(variable) { var pair = variable.split('='); env[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); }); + +const ipc = require('electron').ipcRenderer +window.env.locale_json = ipc.sendSync('locale-data');