Skip to main content

SpellChecker拼写检查器

Electron has built-in support for Chromium's spellchecker since Electron 8. 从Electron 8开始,Electron就内置了对Chromium拼写检查器的支持。 On Windows and Linux this is powered by Hunspell dictionaries, and on macOS it makes use of the native spellchecker APIs.在Windows和Linux上,这是由Hunspell字典提供支持的,在macOS上,它使用本地拼写检查器API。

How to enable the spellchecker?如何启用拼写检查器?

For Electron 9 and higher the spellchecker is enabled by default. 对于Electron 9及更高版本,默认情况下启用拼写检查器。For Electron 8 you need to enable it in webPreferences.对于Electron 8,您需要在webPreferences中启用它。

const myWindow = new BrowserWindow({
webPreferences: {
spellcheck: true
}
})

How to set the languages the spellchecker uses?如何设置拼写检查器使用的语言?

On macOS as we use the native APIs there is no way to set the language that the spellchecker uses. 在macOS上,当我们使用本机API时,无法设置拼写检查器使用的语言。By default on macOS the native spellchecker will automatically detect the language being used for you.默认情况下,macOS上的本机拼写检查器将自动检测您使用的语言。

For Windows and Linux there are a few Electron APIs you should use to set the languages for the spellchecker.对于Windows和Linux,您应该使用一些ElectronAPI来设置拼写检查器的语言。

// Sets the spellchecker to check English US and French
myWindow.session.setSpellCheckerLanguages(['en-US', 'fr'])

// An array of all available language codes
const possibleLanguages = myWindow.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?如何将拼写检查器的结果放入上下文菜单?

All the required information to generate a context menu is provided in the context-menu event on each webContents instance. 生成上下文菜单所需的所有信息都在每个webContents实例的context-menu事件中提供。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: () => mainWindow.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: () => mainWindow.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
})
)
}

menu.popup()
})

Does the spellchecker use any Google services?拼写检查器是否使用任何谷歌服务?

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. 虽然拼写检查器本身不向Google服务发送任何打字、单词或用户输入,但默认情况下,hunspell字典文件是从Google CDN下载的。If you want to avoid this you can provide an alternative URL to download the dictionaries from.如果您想避免这种情况,可以提供另一个URL来下载字典。

myWindow.session.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/')

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.请查看session.setSpellCheckerDictionaryDownloadURLL的文档,了解有关从何处获取字典文件以及如何托管这些文件的更多信息。