Refactor locale to only expose the data we care about

This commit is contained in:
David Balatero 2017-06-21 18:13:36 -07:00 committed by Scott Nonnenberg
parent 5e5ca80a6e
commit ceaff68d55
No known key found for this signature in database
GPG key ID: A4931C09644C654B
3 changed files with 50 additions and 51 deletions

View file

@ -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();

View file

@ -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'))
}
module.exports = {
normalizeLocaleName,
getLocaleMessages
// 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 = {
name: localeName,
messages
}

27
main.js
View file

@ -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();