From ceaff68d55588c6fb60f96f139b799a10fe37dfc Mon Sep 17 00:00:00 2001 From: David Balatero Date: Wed, 21 Jun 2017 18:13:36 -0700 Subject: [PATCH] Refactor locale to only expose the data we care about --- app/autoupdate.js | 49 ++++++++++++++++++++++------------------------- app/locale.js | 23 ++++++++++++++++++++-- main.js | 27 +++++--------------------- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/app/autoupdate.js b/app/autoupdate.js index 4b5145fd3..df910db57 100644 --- a/app/autoupdate.js +++ b/app/autoupdate.js @@ -1,7 +1,8 @@ const autoUpdater = require('electron-updater').autoUpdater const { dialog } = require('electron'); -const config = require('./config'); +const config = require('./config'); +const locale = require('./locale'); const windowState = require('./window_state'); const hour = 60 * 60; @@ -18,42 +19,38 @@ function checkForUpdates() { autoUpdater.checkForUpdates(); } -function showUpdateDialog(localeMessages) { - return function() { - const options = { - type: 'info', - buttons: [ - localeMessages.autoUpdateRestartButtonLabel.message, - localeMessages.autoUpdateLaterButtonLabel.message - ], - title: localeMessages.autoUpdateNewVersionTitle.message, - message: localeMessages.autoUpdateNewVersionMessage.message, - detail: localeMessages.autoUpdateNewVersionInstructions.message, - defaultId: RESTART_BUTTON, - cancelId: LATER_BUTTON - } - - dialog.showMessageBox(options, function(response) { - if (response == RESTART_BUTTON) { - windowState.markShouldQuit(); - autoUpdater.quitAndInstall(); - } - }); +function showUpdateDialog() { + const options = { + type: 'info', + buttons: [ + locale.messages.autoUpdateRestartButtonLabel.message, + locale.messages.autoUpdateLaterButtonLabel.message + ], + title: locale.messages.autoUpdateNewVersionTitle.message, + message: locale.messages.autoUpdateNewVersionMessage.message, + detail: locale.messages.autoUpdateNewVersionInstructions.message, + defaultId: RESTART_BUTTON, + cancelId: LATER_BUTTON } + + dialog.showMessageBox(options, function(response) { + if (response == RESTART_BUTTON) { + windowState.markShouldQuit(); + autoUpdater.quitAndInstall(); + } + }); } function onError(error) { console.log("Got an error while updating: ", error.stack); } -function initializeAutoUpdater(localeMessages) { +function initializeAutoUpdater() { if (autoUpdateDisabled()) { return; } - const onUpdateDownloaded = showUpdateDialog(localeMessages); - - autoUpdater.addListener('update-downloaded', onUpdateDownloaded); + autoUpdater.addListener('update-downloaded', showUpdateDialog); autoUpdater.addListener('error', onError); checkForUpdates(); diff --git a/app/locale.js b/app/locale.js index 3d4dc340a..33c802cef 100644 --- a/app/locale.js +++ b/app/locale.js @@ -1,5 +1,6 @@ const path = require('path'); const fs = require('fs'); +const app = require('electron').app; function normalizeLocaleName(locale) { if (/^en-/.test(locale)) { @@ -23,7 +24,25 @@ function getLocaleMessages(locale) { return JSON.parse(fs.readFileSync(targetFile, 'utf-8')) } +// Load locale - if we can't load messages for the current locale, we +// default to 'en' +// +// possible locales: +// https://github.com/electron/electron/blob/master/docs/api/locales.md +let localeName = normalizeLocaleName(app.getLocale()); +let messages; + +try { + messages = getLocaleMessages(localeName); +} catch (e) { + console.log('Problem loading messages for locale ', localeName, e.stack); + console.log('Falling back to en locale'); + + localeName = 'en'; + messages = getLocaleMessages(localeName); +} + module.exports = { - normalizeLocaleName, - getLocaleMessages + name: localeName, + messages } diff --git a/main.js b/main.js index a9cd5acfe..80b1e0699 100644 --- a/main.js +++ b/main.js @@ -8,7 +8,6 @@ const Menu = electron.Menu; const shell = electron.shell; const autoupdate = require('./app/autoupdate'); -const locale = require('./app/locale'); const windowState = require('./app/window_state'); console.log('setting AUMID'); @@ -39,26 +38,9 @@ if (config.environment === 'production' && !process.mas) { } const userConfig = require('./app/user_config'); - let windowConfig = userConfig.get('window'); -// Load locale - if we can't load messages for the current locale, we -// default to 'en' -// -// possible locales: -// https://github.com/electron/electron/blob/master/docs/api/locales.md -let localeName = locale.normalizeLocaleName(app.getLocale()); -let messages; - -try { - messages = locale.getLocaleMessages(localeName); -} catch (e) { - console.log('Problem loading messages for locale ', localeName, e.stack); - console.log('Falling back to en locale'); - - localeName = 'en'; - messages = locale.getLocaleMessages(localeName); -} +const locale = require('./app/locale'); function createWindow () { const windowOptions = Object.assign({ @@ -102,7 +84,7 @@ function createWindow () { // Ingested in preload.js via a sendSync call ipc.on('locale-data', function(event, arg) { - event.returnValue = messages; + event.returnValue = locale.messages; }); function prepareURL(pathSegments) { @@ -111,7 +93,7 @@ function createWindow () { protocol: 'file:', slashes: true, query: { - locale: localeName, + locale: locale.name, version: app.getVersion(), buildExpiration: config.get('buildExpiration'), serverUrl: config.get('serverUrl'), @@ -172,11 +154,12 @@ function createWindow () { app.on('ready', function() { console.log('app ready'); - autoupdate.initializeAutoUpdater(messages); + autoupdate.initializeAutoUpdater(); createWindow(); let template = require('./app/menu.js'); + if (process.platform === 'darwin') { template[3].submenu[3].click = function() { mainWindow.show();