ELECTRON-656 (Localize context menu labels) (#472)

- Localize context menu labels
- Update condition to setLocale for pop-out window
This commit is contained in:
Kiran Niranjan 2018-08-22 17:34:16 +05:30 committed by Vishwas Shashidhar
parent c4726a9dc2
commit 1695920d24
7 changed files with 120 additions and 14 deletions

View File

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

View File

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

View File

@ -987,10 +987,13 @@ 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)) {
if (browserWindow && !browserWindow.isDestroyed()) {
if (isMainWindow(browserWindow)) {
menu = electron.Menu.buildFromTemplate(getTemplate(app));
electron.Menu.setApplicationMenu(menu);
@ -999,12 +1002,15 @@ function setLocale(browserWindow, opts) {
// update locale for custom title bar
if (isCustomTitleBarEnabled) {
const titleBarContent = i18n.getMessageFor('TitleBar');
browserWindow.webContents.send('locale-changed', { titleBar: titleBarContent });
localeContent.titleBar = i18n.getMessageFor('TitleBar');
}
}
}
localeContent.contextMenu = i18n.getMessageFor('ContextMenu');
browserWindow.webContents.send('locale-changed', localeContent);
}
updateConfigField('locale', language);
}

View File

@ -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",

View File

@ -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",

View File

@ -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": "切り取り",

View File

@ -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": "切り取り",