From 1695920d245da4c6a771097850159283fcd9fdac Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Wed, 22 Aug 2018 17:34:16 +0530 Subject: [PATCH] ELECTRON-656 (Localize context menu labels) (#472) - Localize context menu labels - Update condition to setLocale for pop-out window --- js/preload/preloadMain.js | 7 ++++++- js/spellChecker/index.js | 43 +++++++++++++++++++++++++++++++++++---- js/windowMgr.js | 24 ++++++++++++++-------- locale/en-US.json | 15 ++++++++++++++ locale/en.json | 15 ++++++++++++++ locale/ja-JP.json | 15 ++++++++++++++ locale/ja.json | 15 ++++++++++++++ 7 files changed, 120 insertions(+), 14 deletions(-) diff --git a/js/preload/preloadMain.js b/js/preload/preloadMain.js index a6b2ad2e..729a8c9e 100644 --- a/js/preload/preloadMain.js +++ b/js/preload/preloadMain.js @@ -32,6 +32,7 @@ const KeyCodes = { let Search; let SearchUtils; let CryptoLib; +let spellChecker; let isAltKey = false; let isMenuOpen = false; @@ -69,7 +70,7 @@ function loadSpellChecker() { const SpellCheckerHelper = require('../spellChecker').SpellCheckHelper; /* eslint-enable global-require */ // Method to initialize spell checker - const spellChecker = new SpellCheckerHelper(); + spellChecker = new SpellCheckerHelper(); spellChecker.initializeSpellChecker(); } catch (err) { /* eslint-disable no-console */ @@ -492,6 +493,10 @@ function createAPI() { if (dataObj.titleBar) { titleBar.updateLocale(dataObj.titleBar); } + + if (dataObj.contextMenu && spellChecker) { + spellChecker.updateContextMenuLocale(dataObj.contextMenu); + } } }); diff --git a/js/spellChecker/index.js b/js/spellChecker/index.js index 96ad1ee5..9bcfe110 100644 --- a/js/spellChecker/index.js +++ b/js/spellChecker/index.js @@ -29,12 +29,47 @@ class SpellCheckHelper { this.spellCheckHandler.switchLanguage(sysLocale); } - const contextMenuBuilder = new ContextMenuBuilder(this.spellCheckHandler, null, false, SpellCheckHelper.processMenu); + this.contextMenuBuilder = new ContextMenuBuilder(this.spellCheckHandler, null, false, this.processMenu.bind(this)); this.contextMenuListener = new ContextMenuListener((info) => { - contextMenuBuilder.showPopupMenu(info); + this.contextMenuBuilder.showPopupMenu(info); }); } + /** + * Updates the locale for context menu labels + * @param content {Object} - locale content for context menu + */ + updateContextMenuLocale(content) { + this.localeContent = content; + this.contextMenuBuilder.setAlternateStringFormatter(SpellCheckHelper.getStringTable(content)); + } + + /** + * Builds the string table for context menu + * + * @param content {Object} - locale content for context menu + * @return {Object} - String table for context menu + */ + static getStringTable(content) { + return { + copyMail: () => content['Copy Email Address'] || `Copy Email Address`, + copyLinkUrl: () => content['Copy Link'] || 'Copy Link', + openLinkUrl: () => content['Open Link'] || 'Open Link', + copyImageUrl: () => content['Copy Image URL'] || 'Copy Image URL', + copyImage: () => content['Copy Image'] || 'Copy Image', + addToDictionary: () => content['Add to Dictionary'] || 'Add to Dictionary', + lookUpDefinition: (lookup) => { + const lookUp = content['Look Up '] || 'Look Up '; + return `${lookUp}"${lookup.word}"`; + }, + searchGoogle: () => content['Search with Google'] || 'Search with Google', + cut: () => content.Cut || 'Cut', + copy: () => content.Copy || 'Copy', + paste: () => content.Paste || 'Paste', + inspectElement: () => content['Inspect Element'] || 'Inspect Element', + }; + } + /** * Method to add default menu items to the * menu that was generated by ContextMenuBuilder @@ -45,7 +80,7 @@ class SpellCheckHelper { * @param menu * @returns menu */ - static processMenu(menu) { + processMenu(menu) { let isLink = false; menu.items.map((item) => { @@ -60,7 +95,7 @@ class SpellCheckHelper { menu.append(new MenuItem({ role: 'reload', accelerator: 'CmdOrCtrl+R', - label: 'Reload' + label: this.localeContent && this.localeContent.Reload || 'Reload', })); } return menu; diff --git a/js/windowMgr.js b/js/windowMgr.js index e25e83b0..6aa36bd0 100644 --- a/js/windowMgr.js +++ b/js/windowMgr.js @@ -987,22 +987,28 @@ eventEmitter.on('notificationSettings', (notificationSettings) => { */ function setLocale(browserWindow, opts) { const language = opts && opts.language || app.getLocale(); + const localeContent = {}; log.send(logLevels.INFO, `language changed to ${language}. Updating menu and user config`); setLanguage(language); - if (browserWindow && isMainWindow(browserWindow)) { - menu = electron.Menu.buildFromTemplate(getTemplate(app)); - electron.Menu.setApplicationMenu(menu); + if (browserWindow && !browserWindow.isDestroyed()) { + if (isMainWindow(browserWindow)) { - if (isWindows10()) { - browserWindow.setMenuBarVisibility(false); + menu = electron.Menu.buildFromTemplate(getTemplate(app)); + electron.Menu.setApplicationMenu(menu); - // update locale for custom title bar - if (isCustomTitleBarEnabled) { - const titleBarContent = i18n.getMessageFor('TitleBar'); - browserWindow.webContents.send('locale-changed', { titleBar: titleBarContent }); + if (isWindows10()) { + browserWindow.setMenuBarVisibility(false); + + // update locale for custom title bar + if (isCustomTitleBarEnabled) { + localeContent.titleBar = i18n.getMessageFor('TitleBar'); + } } } + + localeContent.contextMenu = i18n.getMessageFor('ContextMenu'); + browserWindow.webContents.send('locale-changed', localeContent); } updateConfigField('locale', language); diff --git a/locale/en-US.json b/locale/en-US.json index f933b24e..fa617005 100644 --- a/locale/en-US.json +++ b/locale/en-US.json @@ -17,6 +17,21 @@ "Bring to Front on Notifications": "Bring to Front on Notifications", "Certificate Error": "Certificate Error", "Close": "Close", + "ContextMenu": { + "Add to Dictionary": "Add to Dictionary", + "Copy": "Copy", + "Copy Email Address": "Copy Email Address", + "Copy Image": "Copy Image", + "Copy Image URL": "Copy Image URL", + "Copy Link": "Copy Link", + "Cut": "Cut", + "Inspect Element": "Inspect Element", + "Look Up ": "Look Up ", + "Open Link": "Open Link", + "Paste": "Paste", + "Reload": "Reload", + "Search with Google": "Search with Google" + }, "Copy": "Copy", "Custom": "Custom", "Cut": "Cut", diff --git a/locale/en.json b/locale/en.json index 5af82c06..ec1bbc7c 100644 --- a/locale/en.json +++ b/locale/en.json @@ -17,6 +17,21 @@ "Bring to Front on Notifications": "Bring to Front on Notifications", "Certificate Error": "Certificate Error", "Close": "Close", + "ContextMenu": { + "Add to Dictionary": "Add to Dictionary", + "Copy": "Copy", + "Copy Email Address": "Copy Email Address", + "Copy Image": "Copy Image", + "Copy Image URL": "Copy Image URL", + "Copy Link": "Copy Link", + "Cut": "Cut", + "Inspect Element": "Inspect Element", + "Look Up ": "Look Up ", + "Open Link": "Open Link", + "Paste": "Paste", + "Reload": "Reload", + "Search with Google": "Search with Google" + }, "Copy": "Copy", "Custom": "Custom", "Cut": "Cut", diff --git a/locale/ja-JP.json b/locale/ja-JP.json index cf6a2186..2361fce6 100644 --- a/locale/ja-JP.json +++ b/locale/ja-JP.json @@ -17,6 +17,21 @@ "Bring to Front on Notifications": "通知時に前面に表示", "Certificate Error": "証明書のエラー", "Close": "閉じる", + "ContextMenu": { + "Add to Dictionary": "辞書に追加", + "Copy": "コピー", + "Copy Email Address": "メールアドレスをコピーする", + "Copy Image": "イメージをコピーする", + "Copy Image URL": "コピー画像のURL", + "Copy Link": "リンクをコピーする", + "Cut": "カット", + "Inspect Element": "要素の検査", + "Look Up ": "見上げる", + "Open Link": "リンクを開く", + "Paste": "ペースト", + "Reload": "リロード", + "Search with Google": "Googleで検索" + }, "Copy": "コピー", "Custom": "カスタム", "Cut": "切り取り", diff --git a/locale/ja.json b/locale/ja.json index c7880460..06a956da 100644 --- a/locale/ja.json +++ b/locale/ja.json @@ -17,6 +17,21 @@ "Bring to Front on Notifications": "通知時に前面に表示", "Certificate Error": "証明書のエラー", "Close": "閉じる", + "ContextMenu": { + "Add to Dictionary": "辞書に追加", + "Copy": "コピー", + "Copy Email Address": "メールアドレスをコピーする", + "Copy Image": "イメージをコピーする", + "Copy Image URL": "コピー画像のURL", + "Copy Link": "リンクをコピーする", + "Cut": "カット", + "Inspect Element": "要素の検査", + "Look Up ": "見上げる", + "Open Link": "リンクを開く", + "Paste": "ペースト", + "Reload": "リロード", + "Search with Google": "Googleで検索" + }, "Copy": "コピー", "Custom": "カスタム", "Cut": "切り取り",