SpellChecker
自 Electron 8 起,Electron 就内置了对 Chromium 拼写检查器的支持。在 Windows 和 Linux 上,它由 Hunspell 词典提供支持,在 macOS 上,它使用原生拼写检查器 API。
¥Electron has built-in support for Chromium's spellchecker since Electron 8. On Windows and Linux this is powered by Hunspell dictionaries, and on macOS it makes use of the native spellchecker APIs.
如何启用拼写检查器?
¥How to enable the spellchecker?
对于 Electron 9 及更高版本,拼写检查器默认启用。对于 Electron 8,你需要在 webPreferences
中启用它。
¥For Electron 9 and higher the spellchecker is enabled by default. For Electron 8 you need to enable it in webPreferences
.
const myWindow = new BrowserWindow({
webPreferences: {
spellcheck: true
}
})
如何设置拼写检查器使用的语言?
¥How to set the languages the spellchecker uses?
在 macOS 上,当我们使用原生 API 时,无法设置拼写检查器使用的语言。默认情况下,macOS 上的原生拼写检查器将自动检测你使用的语言。
¥On macOS as we use the native APIs there is no way to set the language that the spellchecker uses. By default on macOS the native spellchecker will automatically detect the language being used for you.
对于 Windows 和 Linux,你应该使用一些 Electron API 来设置拼写检查器的语言。
¥For Windows and Linux there are a few Electron APIs you should use to set the languages for the spellchecker.
// Sets the spellchecker to check English US and French
myWindow.webContents.session.setSpellCheckerLanguages(['en-US', 'fr'])
// An array of all available language codes
const possibleLanguages = myWindow.webContents.session.availableSpellCheckerLanguages
默认情况下,拼写检查器将启用与当前操作系统区域设置匹配的语言。
¥By default the spellchecker will enable the language matching the current OS locale.
如何将拼写检查器的结果放入上下文菜单中?
¥How do I put the results of the spellchecker in my context menu?
每个 webContents
实例上的 context-menu
事件中提供了生成上下文菜单所需的所有信息。下面提供了一个如何使用此信息创建上下文菜单的小示例。
¥All the required information to generate a context menu is provided in the context-menu
event on each webContents
instance. A small example
of how to make a context menu with this information is provided below.
const { Menu, MenuItem } = require('electron')
myWindow.webContents.on('context-menu', (event, params) => {
const menu = new Menu()
// Add each spelling suggestion
for (const suggestion of params.dictionarySuggestions) {
menu.append(new MenuItem({
label: suggestion,
click: () => myWindow.webContents.replaceMisspelling(suggestion)
}))
}
// Allow users to add the misspelled word to the dictionary
if (params.misspelledWord) {
menu.append(
new MenuItem({
label: 'Add to dictionary',
click: () => myWindow.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
})
)
}
menu.popup()
})
拼写检查器是否使用任何 Google 服务?
¥Does the spellchecker use any Google services?
尽管拼写检查器本身不会将任何打字、单词或用户输入发送到 Google 服务,但 hunspell 字典文件默认从 Google CDN 下载。如果你想避免这种情况,你可以提供另一个 URL 来下载词典。
¥Although the spellchecker itself does not send any typings, words or user input to Google services the hunspell dictionary files are downloaded from a Google CDN by default. If you want to avoid this you can provide an alternative URL to download the dictionaries from.
myWindow.webContents.session.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/')
查看 session.setSpellCheckerDictionaryDownloadURL
的文档,了解有关从何处获取字典文件以及如何托管它们的更多信息。
¥Check out the docs for session.setSpellCheckerDictionaryDownloadURL
for more information on where to get the dictionary files from and how you need to host them.