Spell Checker
This page describes how to configure and use the built-in spell checker in your application.
Molybden provides a built-in spell checker that can be used to check spelling in the text fields of your application.
The spell checker is powered by the Chromium’s spell checking engine. On Windows and Linux it is based on the Hunspell dictionaries. On macOS, it is based on the dictionaries and spell checking settings provided the operating system.
By default, the spell checker is enabled and checks text in the text fields. It underlines misspelled words with a red dotted line. When you right-click a misspelled word, the context menu shows the list of suggested words and the Add to Dictionary item.
You can change the default behavior by customizing context menu.
To configure the spell checker, you need to use the molybden::SpellChecker
class. Molybden supports
application profiles. Each profile has its own spell checker.
To access the spell checker for the default application profile, use the following approach:
auto spell_checker = app->profile()->spellChecker();
The spell checker instance allows you to enable/disable spell checking, specify the language of the spell checker and the list of words that should be ignored by the spell checker.
Disabling spell checker
By default, the spell checker is enabled. To disable it, use the following approach:
spell_checker->disable();
Configuring languages
The spell checker can check text in multiple languages simultaneously. By default, it checks text in the language of the operating system.
Windows and Linux
On Windows and Linux you can specify the list of languages that should be used by the spell checker. To do that, use the following approach:
spell_checker->addLanguage(kEnglishUs);
spell_checker->addLanguage(kFrench);
spell_checker->addLanguage(kGerman);
spell_checker->addLanguage(kUkrainian);
The addLanguage()
function loads the dictionary for the specified language and adds it to the list of languages used
by the spell checker. It blocks the current thread execution until the dictionary is loaded from the
application user data directory. If the dictionary does not
exist in the user data directory, Molybden will download it from Google CDN. The function returns true
if the
language was added successfully.
To remove a language from the list of languages used by the spell checker, use the following approach:
spell_checker->removeLanguage(kEnglishUs);
macOS
On macOS, the spell checker uses the list of languages specified in the operating system settings. To change the list of languages, you need to change the list of languages in the operating system settings. To do that:
- Open System Settings.
- Go to Keyboard.
- In the Text Input section, click the Edit… button.
- In the opened dialog, click the Spelling drop down and select the Set Up… item.
- In the opened dialog, enable the languages that should be used by the spell checker.
Custom dictionary
Custom dictionary is a list of words that should be ignored by the spell checker. To access the custom dictionary, use the following approach:
auto custom_dictionary = spell_checker->customDictionary();
To add a word to the custom dictionary, use the following code:
bool success = custom_dictionary->addWord("John");
Important: the word must be UTF-8, between 1 and 99 bytes long, and without leading or trailing ASCII whitespace.
To remove a word from the custom dictionary, use the following code:
bool success = custom_dictionary->removeWord("John");
To get the list of words in the custom dictionary, use the following code:
for (const auto& word : custom_dictionary->words()) {
std::cout << word << std::endl;
}