Skip to main content

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.