Permalink
Browse files

[DDW-515] Implement translations for the application menu

  • Loading branch information...
daniloprates committed Jan 10, 2019
1 parent 2305264 commit c792f829bc9e9b9278601dba49e90d8ab3532885
@@ -1,66 +1,27 @@
// @flow
import os from 'os';
import { app, BrowserWindow, globalShortcut, Menu, shell } from 'electron';
import { app, BrowserWindow, shell } from 'electron';
import { client } from 'electron-connect';
import { includes } from 'lodash';
import { Logger } from './utils/logging';
import { setupLogging } from './utils/setupLogging';
import { createMainWindow } from './windows/main';
import { winLinuxMenu } from './menus/win-linux';
import { osxMenu } from './menus/osx';
import { installChromeExtensions } from './utils/installChromeExtensions';
import { environment } from './environment';
import {
OPEN_ABOUT_DIALOG_CHANNEL,
GO_TO_ADA_REDEMPTION_SCREEN_CHANNEL,
GO_TO_NETWORK_STATUS_SCREEN_CHANNEL
} from '../common/ipc/api';
import mainErrorHandler from './utils/mainErrorHandler';
import { launcherConfig } from './config';
import { setupCardano } from './cardano/setup';
import { CardanoNode } from './cardano/CardanoNode';
import { safeExitWithCode } from './utils/safeExitWithCode';
import { buildAppMenus } from './utils/buildAppMenus';
import { ensureXDGDataIsSet } from './cardano/config';
import { CardanoNodeStates } from '../common/types/cardano-node.types';

// Global references to windows to prevent them from being garbage collected
let mainWindow: BrowserWindow;
let cardanoNode: ?CardanoNode;

const openAbout = () => {
if (mainWindow) mainWindow.webContents.send(OPEN_ABOUT_DIALOG_CHANNEL);
};

const goToAdaRedemption = () => {
if (mainWindow) mainWindow.webContents.send(GO_TO_ADA_REDEMPTION_SCREEN_CHANNEL);
};

const goToNetworkStatus = () => {
if (mainWindow) mainWindow.webContents.send(GO_TO_NETWORK_STATUS_SCREEN_CHANNEL);
};

const restartInSafeMode = async () => {
Logger.info('Restarting in SafeMode...');
if (cardanoNode) await cardanoNode.stop();
Logger.info('Exiting Daedalus with code 21.');
safeExitWithCode(21);
};

const restartWithoutSafeMode = async () => {
Logger.info('Restarting without SafeMode...');
if (cardanoNode) await cardanoNode.stop();
Logger.info('Exiting Daedalus with code 22.');
safeExitWithCode(22);
};

const { isDev, isMacOS, isWatchMode, buildLabel } = environment;
const menuActions = {
openAbout,
goToAdaRedemption,
goToNetworkStatus,
restartInSafeMode,
restartWithoutSafeMode,
};
const { isDev, isWatchMode, buildLabel } = environment;

const safeExit = async () => {
if (!cardanoNode) {
@@ -103,37 +64,17 @@ const onAppReady = async () => {
client.create(mainWindow);
}

// Build app menus
let menu;
if (isMacOS) {
menu = Menu.buildFromTemplate(osxMenu(app, mainWindow, menuActions, isInSafeMode));
Menu.setApplicationMenu(menu);
} else {
menu = Menu.buildFromTemplate(winLinuxMenu(app, mainWindow, menuActions, isInSafeMode));
mainWindow.setMenu(menu);
}

// Hide application window on Cmd+H hotkey (OSX only!)
if (isMacOS) {
app.on('activate', () => {
if (!mainWindow.isVisible()) app.show();
});

mainWindow.on('focus', () => {
globalShortcut.register('CommandOrControl+H', app.hide);
});

mainWindow.on('blur', () => {
globalShortcut.unregister('CommandOrControl+H');
});
}

mainWindow.on('close', async (event) => {
Logger.info('mainWindow received <close> event. Safe exiting Daedalus now.');
event.preventDefault();
await safeExit();
});

// TODO: Figure out how to get the locale
// const locale = 'en-US.json';
const locale = 'ja-JP.json';
buildAppMenus(mainWindow, cardanoNode, isInSafeMode, locale);

// Security feature: Prevent creation of new browser windows
// https://github.com/electron/electron/blob/master/docs/tutorial/security.md#14-disable-or-limit-creation-of-new-windows
app.on('web-contents-created', (_, contents) => {
@@ -0,0 +1,19 @@
{
"menu.osx.daedalus": "Daedalus",
"menu.osx.daedalus.about": "About",
"menu.osx.daedalus.adaRedemption": "Ada redemption",
"menu.osx.daedalus.gpuSafeMode": "GPU safe mode",
"menu.osx.daedalus.networkStatus": "Network status",
"menu.osx.daedalus.quit": "Quit",
"menu.osx.edit": "Edit",
"menu.osx.edit.undo": "Undo",
"menu.osx.edit.redo": "Redo",
"menu.osx.edit.cut": "Cut",
"menu.osx.edit.copy": "Copy",
"menu.osx.edit.paste": "Paste",
"menu.osx.edit.selectAll": "Select All",
"menu.osx.view": "View",
"menu.osx.view.reload": "Reload",
"menu.osx.view.toggleFullScreen": "Toggle Full Screen",
"menu.osx.view.toggleDeveloperTools": "Toggle Developer Tools"
}
@@ -0,0 +1,19 @@
{
"menu.osx.daedalus": "ダイダロス",
"menu.osx.daedalus.about": "",
"menu.osx.daedalus.adaRedemption": "アダの償還",
"menu.osx.daedalus.gpuSafeMode": "GPUセーフモード",
"menu.osx.daedalus.networkStatus": "ネットワークステータス",
"menu.osx.daedalus.quit": "終了する",
"menu.osx.edit": "編集",
"menu.osx.edit.undo": "元に戻す",
"menu.osx.edit.redo": "やり直し",
"menu.osx.edit.cut": "カット",
"menu.osx.edit.copy": "コピー",
"menu.osx.edit.paste": "ペースト",
"menu.osx.edit.selectAll": "すべて選択",
"menu.osx.view": "見る",
"menu.osx.view.reload": "リロード",
"menu.osx.view.toggleFullScreen": "フルスクリーン切り替え",
"menu.osx.view.toggleDeveloperTools": "「開発者ツールの切り替え」"
}
@@ -2,27 +2,32 @@
import type { App, BrowserWindow } from 'electron';
import { compact } from 'lodash';
import type { MenuActions } from './MenuActions.types';
import { getTranslation } from '../utils/getTranslation';

const id = 'menu.osx';

export const osxMenu = (
app: App,
window: BrowserWindow,
actions: MenuActions,
isInSafeMode: boolean
isInSafeMode: boolean,
translations: {},
translation: function = getTranslation(translations, id)
) => (
[{
label: 'Daedalus',
label: translation('daedalus'),
submenu: compact([{
label: 'About',
label: translation('daedalus.about'),
click() {
actions.openAbout();
},
}, {
label: 'Ada redemption',
label: translation('daedalus.adaRedemption'),
click() {
actions.goToAdaRedemption();
}
}, {
label: 'GPU safe mode',
label: translation('daedalus.gpuSafeMode'),
type: 'checkbox',
checked: isInSafeMode,
click() {
@@ -31,62 +36,62 @@ export const osxMenu = (
actions.restartInSafeMode();
},
}, {
label: 'Network status',
label: translation('daedalus.networkStatus'),
accelerator: 'Command+S',
click() {
actions.goToNetworkStatus();
},
}, {
label: 'Quit',
label: translation('daedalus.quit'),
accelerator: 'Command+Q',
click() {
app.quit();
}
}])
}, {
label: 'Edit',
label: translation('edit'),
submenu: [{
label: 'Undo',
label: translation('edit.undo'),
accelerator: 'Command+Z',
role: 'undo'
}, {
label: 'Redo',
label: translation('edit.redo'),
accelerator: 'Shift+Command+Z',
role: 'redo'
}, {
type: 'separator'
}, {
label: 'Cut',
label: translation('edit.cut'),
accelerator: 'Command+X',
role: 'cut'
}, {
label: 'Copy',
label: translation('edit.copy'),
accelerator: 'Command+C',
role: 'copy'
}, {
label: 'Paste',
label: translation('edit.paste'),
accelerator: 'Command+V',
role: 'paste'
}, {
label: 'Select All',
label: translation('edit.selectAll'),
accelerator: 'Command+A',
role: 'selectall'
}]
}, {
label: 'View',
label: translation('view'),
submenu: [
{
label: 'Reload',
label: translation('view.reload'),
accelerator: 'Command+R',
click: () => window.webContents.reload()
},
{
label: 'Toggle Full Screen',
label: translation('view.toggleFullScreen'),
accelerator: 'Ctrl+Command+F',
click: () => window.setFullScreen(!window.isFullScreen())
},
{
label: 'Toggle Developer Tools',
label: translation('view.toggleDeveloperTools'),
accelerator: 'Alt+Command+I',
click: () => window.toggleDevTools()
}
@@ -0,0 +1,87 @@
// @flow
import { app, globalShortcut, Menu, BrowserWindow } from 'electron';
import { environment } from '../environment';
import { winLinuxMenu } from '../menus/win-linux';
import { osxMenu } from '../menus/osx';
import { Logger } from './logging';
import { safeExitWithCode } from './safeExitWithCode';
import { CardanoNode } from '../cardano/CardanoNode';
import {
OPEN_ABOUT_DIALOG_CHANNEL,
GO_TO_ADA_REDEMPTION_SCREEN_CHANNEL,
GO_TO_NETWORK_STATUS_SCREEN_CHANNEL
} from '../../common/ipc/api';

export const buildAppMenus = async (
mainWindow: BrowserWindow,
cardanoNode: ?CardanoNode,
isInSafeMode: boolean,
locale: string,
) => {

const openAbout = () => {
if (mainWindow) mainWindow.webContents.send(OPEN_ABOUT_DIALOG_CHANNEL);
};

const goToAdaRedemption = () => {
if (mainWindow) mainWindow.webContents.send(GO_TO_ADA_REDEMPTION_SCREEN_CHANNEL);
};

const goToNetworkStatus = () => {
if (mainWindow) mainWindow.webContents.send(GO_TO_NETWORK_STATUS_SCREEN_CHANNEL);
};

const restartInSafeMode = async () => {
Logger.info('Restarting in SafeMode...');
if (cardanoNode) await cardanoNode.stop();
Logger.info('Exiting Daedalus with code 21.');
safeExitWithCode(21);
};

const restartWithoutSafeMode = async () => {
Logger.info('Restarting without SafeMode...');
if (cardanoNode) await cardanoNode.stop();
Logger.info('Exiting Daedalus with code 22.');
safeExitWithCode(22);
};

const { isMacOS } = environment;
const translations = require(`../locales/${locale}`);
const menuActions = {
openAbout,
goToAdaRedemption,
goToNetworkStatus,
restartInSafeMode,
restartWithoutSafeMode,
};

// Build app menus
let menu;
if (isMacOS) {
menu = Menu.buildFromTemplate(
osxMenu(app, mainWindow, menuActions, isInSafeMode, translations)
);
Menu.setApplicationMenu(menu);
} else {
menu = Menu.buildFromTemplate(
winLinuxMenu(app, mainWindow, menuActions, isInSafeMode)
);
mainWindow.setMenu(menu);
}

// Hide application window on Cmd+H hotkey (OSX only!)
if (isMacOS) {
app.on('activate', () => {
if (!mainWindow.isVisible()) app.show();
});

mainWindow.on('focus', () => {
globalShortcut.register('CommandOrControl+H', app.hide);
});

mainWindow.on('blur', () => {
globalShortcut.unregister('CommandOrControl+H');
});
}

};
@@ -0,0 +1,8 @@
// @flow
export const getTranslation =
(
translations: {},
id: string,
) => (
key: string
) => translations[`${id}.${key}`];

0 comments on commit c792f82

Please sign in to comment.