Skip to content

Commit c7b2eb6

Browse files
docs: add documentation on how to use the built-in spellchecker (electron#23508)
1 parent 3daeec5 commit c7b2eb6

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

docs/tutorial/spellchecker.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# SpellChecker
2+
3+
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.
4+
5+
## How to enable the spellchecker?
6+
7+
For Electron 9 and higher the spellchecker is enabled by default. For Electron 8 you need to enable it in `webPreferences`.
8+
9+
```js
10+
const myWindow = new BrowserWindow({
11+
webPreferences: {
12+
spellcheck: true
13+
}
14+
})
15+
```
16+
17+
## How to set the languages the spellchecker uses?
18+
19+
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.
20+
21+
For Windows and Linux there are a few Electron APIs you should use to set the languages for the spellchecker.
22+
23+
```js
24+
// Sets the spellchecker to check English US and French
25+
myWindow.session.setSpellCheckerLanguages(['en-US', 'fr'])
26+
27+
// An array of all available language codes
28+
const possibleLanguages = myWindow.session.availableSpellCheckerLanguages
29+
```
30+
31+
By default the spellchecker will enable the language matching the current OS locale.
32+
33+
## How do I put the results of the spellchecker in my context menu?
34+
35+
All the required information to generate a context menu is provided in the [`context-menu`](../api/web-contents.md#event-context-menu) event on each `webContents` instance. A small example
36+
of how to make a context menu with this information is provided below.
37+
38+
```js
39+
const { Menu, MenuItem } = require('electron')
40+
41+
myWindow.webContents.on('context-menu', (event, params) => {
42+
const menu = new Menu()
43+
44+
// Add each spelling suggestion
45+
for (const suggestion of params.dictionarySuggestions) {
46+
menu.append(new MenuItem({
47+
label: suggestion,
48+
click: () => mainWindow.webContents.replaceMisspelling(suggestion)
49+
}))
50+
}
51+
52+
// Allow users to add the misspelled word to the dictionary
53+
if (params.misspelledWord) {
54+
menu.append(
55+
new MenuItem({
56+
label: 'Add to dictionary',
57+
click: () => mainWindow.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
58+
})
59+
)
60+
}
61+
62+
menu.popup()
63+
})
64+
```
65+
66+
## Does the spellchecker use any Google services?
67+
68+
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.
69+
70+
```js
71+
myWindow.session.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/')
72+
```
73+
74+
Check out the docs for [`session.setSpellCheckerDictionaryDownloadURL`](https://www.electronjs.org/docs/api/session#sessetspellcheckerdictionarydownloadurlurl) for more information on where to get the dictionary files from and how you need to host them.

0 commit comments

Comments
 (0)