Skip to content

Commit

Permalink
v1.7.0 - 增加右键菜单项的翻译
Browse files Browse the repository at this point in the history
  • Loading branch information
Xmader committed Oct 30, 2018
1 parent 990828c commit 3d14975
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 44 deletions.
54 changes: 11 additions & 43 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,14 @@
*/

const os = require('os');
const electron = require('electron');
const path = require('path');
const process = require('process')
const fs = require("fs")
const { app, BrowserWindow, Menu, ipcMain } = require('electron');

const edit_conf = require('./edit_conf.js')
const { buildMenu } = require('./menu.js')

const { app, BrowserWindow, Menu, ipcMain } = electron

let mainWindow = null;

const isDev = process.argv.pop() == "dev";

// 增加右键菜单
const contextMenuTemplate = [
{ label: "撤销", role: 'undo', accelerator: "CmdOrCtrl+Z" },
{ label: "恢复", role: 'redo', accelerator: "CmdOrCtrl+Y" },
{ type: 'separator' }, //分隔线
{ label: "剪切", role: 'cut', accelerator: "CmdOrCtrl+X" }, //Cut菜单项
{ label: "复制", role: 'copy', accelerator: "CmdOrCtrl+C" }, //Copy菜单项
{ label: "粘贴", role: 'paste', accelerator: "CmdOrCtrl+V" }, //Paste菜单项
{ type: 'separator' }, //分隔线
{ label: "全选", role: 'selectall', accelerator: "CmdOrCtrl+A" }, //Select All菜单项
];
if (isDev) {
[
{ type: 'separator' }, //分隔线
{ label: "重新加载页面", role: 'reload' },
{ label: "切换开发者工具", role: 'toggledevtools' },
].forEach(x => contextMenuTemplate.push(x))
}
const contextMenu = Menu.buildFromTemplate(contextMenuTemplate);

const appMenuTemplate = [
{
submenu: [{ label: "退出", role: "quit" }]
},
{
label: "编辑",
submenu: contextMenuTemplate
}
]

let mainWindow = null

app.commandLine.appendSwitch('ignore-certificate-errors') // 忽略证书相关错误, 适用于使用自签名证书将Aria2的RPC配置成HTTPS协议的情况

Expand Down Expand Up @@ -89,8 +54,11 @@ app.on('ready', function () {
// 打开窗口的调试工具
//mainWindow.webContents.openDevTools();

const locale = app.getLocale().includes("zh") ? "zh-CN" : "en-US"
const { contextMenu, appMenu } = buildMenu(locale)

if (platform == 'darwin') {
Menu.setApplicationMenu(Menu.buildFromTemplate(appMenuTemplate))
Menu.setApplicationMenu(appMenu)
} else {
mainWindow.setMenu(null)
}
Expand All @@ -105,11 +73,11 @@ app.on('ready', function () {
subpy.kill('SIGINT');
mainWindow = null;
});
});

ipcMain.on("right_btn", () => {
contextMenu.popup(mainWindow);
})
ipcMain.on("right_btn", () => {
contextMenu.popup(mainWindow)
})
});

ipcMain.on("show_progress_bar", (event, n) => {
if (mainWindow && mainWindow.setProgressBar) {
Expand Down
68 changes: 68 additions & 0 deletions app/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*!
* AriaNg GUI
*
* Copyright (c) 2018 Xmader
* Released under the MIT license
*
* Source Code: https://github.com/Xmader/aria-ng-gui
*
*/

const process = require("process")
const { Menu } = require("electron")
const Translate = require("./translate.js")

const isDev = process.argv.pop() == "dev"


// 增加右键菜单
const contextMenuTemplate = [
{ label: "撤销", role: "undo", accelerator: "CmdOrCtrl+Z" },
{ label: "恢复", role: "redo", accelerator: "CmdOrCtrl+Y" },
{ type: "separator" }, //分隔线
{ label: "剪切", role: "cut", accelerator: "CmdOrCtrl+X" }, //Cut菜单项
{ label: "复制", role: "copy", accelerator: "CmdOrCtrl+C" }, //Copy菜单项
{ label: "粘贴", role: "paste", accelerator: "CmdOrCtrl+V" }, //Paste菜单项
{ type: "separator" }, //分隔线
{ label: "全选", role: "selectall", accelerator: "CmdOrCtrl+A" }, //Select All菜单项
];
if (isDev) {
[
{ type: "separator" }, //分隔线
{ label: "重新加载页面", role: "reload" },
{ label: "切换开发者工具", role: "toggledevtools" },
].forEach(x => contextMenuTemplate.push(x))
}

const appMenuTemplate = [
{
submenu: [{ label: "退出", role: "quit" }]
},
{
label: "编辑",
submenu: contextMenuTemplate
}
]


const buildMenu = (locale) => {

const _buildMenuFromTemplate = (menuTemplate) => {
if (locale != "zh-CN") {
menuTemplate = Translate(menuTemplate, locale)
}
return Menu.buildFromTemplate(menuTemplate)
}

const contextMenu = _buildMenuFromTemplate(contextMenuTemplate)
const appMenu = _buildMenuFromTemplate(appMenuTemplate)

return {
contextMenu,
appMenu
}
}

module.exports = {
buildMenu
}
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ariang-gui",
"version": "1.6.2",
"version": "1.7.0",
"private": true,
"description": "AriaNg GUI",
"main": "app.js",
Expand Down
40 changes: 40 additions & 0 deletions app/translate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*!
* AriaNg GUI
*
* Copyright (c) 2018 Xmader
* Released under the MIT license
*
* Source Code: https://github.com/Xmader/aria-ng-gui
*
* translate.js - 翻译菜单项
*
*/

const translations = {
"撤销": "Undo",
"恢复": "Redo",
"剪切": "Cut",
"复制": "Copy",
"粘贴": "Paste",
"全选": "Select All",
"重新加载页面": "Reload",
"切换开发者工具": "Toggle Developer Tools",
"退出": "Quit",
"编辑": "Edit",
}

const Translate = (menuTemplate, locale = "en-US") => {
const _translateLabels = (x) => {
if (x.label) {
x.label = translations[x.label]
}
if (x.submenu) {
x.submenu = x.submenu.map(_translateLabels)
}
return x
}

return menuTemplate.map(_translateLabels)
}

module.exports = Translate

0 comments on commit 3d14975

Please sign in to comment.