Refactor locale to only expose the data we care about
This commit is contained in:
parent
5e5ca80a6e
commit
ceaff68d55
3 changed files with 50 additions and 51 deletions
|
@ -1,7 +1,8 @@
|
||||||
const autoUpdater = require('electron-updater').autoUpdater
|
const autoUpdater = require('electron-updater').autoUpdater
|
||||||
const { dialog } = require('electron');
|
const { dialog } = require('electron');
|
||||||
const config = require('./config');
|
|
||||||
|
|
||||||
|
const config = require('./config');
|
||||||
|
const locale = require('./locale');
|
||||||
const windowState = require('./window_state');
|
const windowState = require('./window_state');
|
||||||
|
|
||||||
const hour = 60 * 60;
|
const hour = 60 * 60;
|
||||||
|
@ -18,17 +19,16 @@ function checkForUpdates() {
|
||||||
autoUpdater.checkForUpdates();
|
autoUpdater.checkForUpdates();
|
||||||
}
|
}
|
||||||
|
|
||||||
function showUpdateDialog(localeMessages) {
|
function showUpdateDialog() {
|
||||||
return function() {
|
|
||||||
const options = {
|
const options = {
|
||||||
type: 'info',
|
type: 'info',
|
||||||
buttons: [
|
buttons: [
|
||||||
localeMessages.autoUpdateRestartButtonLabel.message,
|
locale.messages.autoUpdateRestartButtonLabel.message,
|
||||||
localeMessages.autoUpdateLaterButtonLabel.message
|
locale.messages.autoUpdateLaterButtonLabel.message
|
||||||
],
|
],
|
||||||
title: localeMessages.autoUpdateNewVersionTitle.message,
|
title: locale.messages.autoUpdateNewVersionTitle.message,
|
||||||
message: localeMessages.autoUpdateNewVersionMessage.message,
|
message: locale.messages.autoUpdateNewVersionMessage.message,
|
||||||
detail: localeMessages.autoUpdateNewVersionInstructions.message,
|
detail: locale.messages.autoUpdateNewVersionInstructions.message,
|
||||||
defaultId: RESTART_BUTTON,
|
defaultId: RESTART_BUTTON,
|
||||||
cancelId: LATER_BUTTON
|
cancelId: LATER_BUTTON
|
||||||
}
|
}
|
||||||
|
@ -39,21 +39,18 @@ function showUpdateDialog(localeMessages) {
|
||||||
autoUpdater.quitAndInstall();
|
autoUpdater.quitAndInstall();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError(error) {
|
function onError(error) {
|
||||||
console.log("Got an error while updating: ", error.stack);
|
console.log("Got an error while updating: ", error.stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeAutoUpdater(localeMessages) {
|
function initializeAutoUpdater() {
|
||||||
if (autoUpdateDisabled()) {
|
if (autoUpdateDisabled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const onUpdateDownloaded = showUpdateDialog(localeMessages);
|
autoUpdater.addListener('update-downloaded', showUpdateDialog);
|
||||||
|
|
||||||
autoUpdater.addListener('update-downloaded', onUpdateDownloaded);
|
|
||||||
autoUpdater.addListener('error', onError);
|
autoUpdater.addListener('error', onError);
|
||||||
|
|
||||||
checkForUpdates();
|
checkForUpdates();
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const app = require('electron').app;
|
||||||
|
|
||||||
function normalizeLocaleName(locale) {
|
function normalizeLocaleName(locale) {
|
||||||
if (/^en-/.test(locale)) {
|
if (/^en-/.test(locale)) {
|
||||||
|
@ -23,7 +24,25 @@ function getLocaleMessages(locale) {
|
||||||
return JSON.parse(fs.readFileSync(targetFile, 'utf-8'))
|
return JSON.parse(fs.readFileSync(targetFile, 'utf-8'))
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
// Load locale - if we can't load messages for the current locale, we
|
||||||
normalizeLocaleName,
|
// default to 'en'
|
||||||
getLocaleMessages
|
//
|
||||||
|
// 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
27
main.js
|
@ -8,7 +8,6 @@ const Menu = electron.Menu;
|
||||||
const shell = electron.shell;
|
const shell = electron.shell;
|
||||||
|
|
||||||
const autoupdate = require('./app/autoupdate');
|
const autoupdate = require('./app/autoupdate');
|
||||||
const locale = require('./app/locale');
|
|
||||||
const windowState = require('./app/window_state');
|
const windowState = require('./app/window_state');
|
||||||
|
|
||||||
console.log('setting AUMID');
|
console.log('setting AUMID');
|
||||||
|
@ -39,26 +38,9 @@ if (config.environment === 'production' && !process.mas) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const userConfig = require('./app/user_config');
|
const userConfig = require('./app/user_config');
|
||||||
|
|
||||||
let windowConfig = userConfig.get('window');
|
let windowConfig = userConfig.get('window');
|
||||||
|
|
||||||
// Load locale - if we can't load messages for the current locale, we
|
const locale = require('./app/locale');
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createWindow () {
|
function createWindow () {
|
||||||
const windowOptions = Object.assign({
|
const windowOptions = Object.assign({
|
||||||
|
@ -102,7 +84,7 @@ function createWindow () {
|
||||||
|
|
||||||
// Ingested in preload.js via a sendSync call
|
// Ingested in preload.js via a sendSync call
|
||||||
ipc.on('locale-data', function(event, arg) {
|
ipc.on('locale-data', function(event, arg) {
|
||||||
event.returnValue = messages;
|
event.returnValue = locale.messages;
|
||||||
});
|
});
|
||||||
|
|
||||||
function prepareURL(pathSegments) {
|
function prepareURL(pathSegments) {
|
||||||
|
@ -111,7 +93,7 @@ function createWindow () {
|
||||||
protocol: 'file:',
|
protocol: 'file:',
|
||||||
slashes: true,
|
slashes: true,
|
||||||
query: {
|
query: {
|
||||||
locale: localeName,
|
locale: locale.name,
|
||||||
version: app.getVersion(),
|
version: app.getVersion(),
|
||||||
buildExpiration: config.get('buildExpiration'),
|
buildExpiration: config.get('buildExpiration'),
|
||||||
serverUrl: config.get('serverUrl'),
|
serverUrl: config.get('serverUrl'),
|
||||||
|
@ -172,11 +154,12 @@ function createWindow () {
|
||||||
app.on('ready', function() {
|
app.on('ready', function() {
|
||||||
console.log('app ready');
|
console.log('app ready');
|
||||||
|
|
||||||
autoupdate.initializeAutoUpdater(messages);
|
autoupdate.initializeAutoUpdater();
|
||||||
|
|
||||||
createWindow();
|
createWindow();
|
||||||
|
|
||||||
let template = require('./app/menu.js');
|
let template = require('./app/menu.js');
|
||||||
|
|
||||||
if (process.platform === 'darwin') {
|
if (process.platform === 'darwin') {
|
||||||
template[3].submenu[3].click = function() {
|
template[3].submenu[3].click = function() {
|
||||||
mainWindow.show();
|
mainWindow.show();
|
||||||
|
|
Loading…
Add table
Reference in a new issue