I want to share a simple solution to spell-checking with TSynEdit (Lazarus) which I just implemented. It is super-simple, thus maybe trivial. However, in my internet search I found that many people are seeking spell-checking for Lazarus and Delphi, so this might help some people or even trigger the development of a TSynSpellcheck component for being included to Lazarus.
It works already nice with English text (no umlauts) and almost nice in German text (only words with umlauts are not highlighted).
Background and problem formulation:
As a university teacher I am building a small editor tool which should have parallel spell checking for german and
Solution:
======
I found I nice trick to simply create a language spell-check for English and German (should work for all languages basically):
1.) I use a TSynEdit (lazarus 1.8.4) and place a TSynAnySyn with it on my main Form.
2.) I downloaded the English and German dictionary from hunspell (LibreOffice), opened and merged them in Excel (with right codepage selected to preserve German umlauts). In Excel I split the hunspell dictionary in two columns, removed the unneeded second column and sorted the words alphabetically (this speeds up the approach enormously). Then I saved it to an UTF-8 encoded codepage to a file 'dictionary.txt' (double-checked this in atom editor).
3.) At the opening of my main form I load this dictionary.txt file into a TStringList which I then set as 'constants' or 'objects' in the corresponding TSynAnySyn property that is set as a highlighter in my TSynEdit. This process takes only 4-5 seconds on my 4 year old MacBookPro, so that can be covered nicely by a splash screen at the start up of the application.
Results:
======
Actually everything works out of the box, the text entering works fast and all known words are highlighted.
Possible improvements:
=================
I think it is easy to add an 'add word to dictionary' feature as a context menu. Maybe the reading of the dictionary can be even faster if the dictionary is compiled into the app's executable as a resource and then be read from there.
Still existing problems:
================
The only two problems I encountered and maybe some of the experts here may help to improve :
- Words with german umlauts are not recognized. I think this is due to the fact that these characters can not be added to the IdentifierChars property, they are just replaced by question marks if one edits them to the string of '!$&+,-./0123456789:;?ABCDEFGHIJKLMNOPQRSTUVXYZ_abcdefghijklmnopqrstuvwxyz' that is set as a default there. I tried to add those characters also in the code of TSynAnySyn, but it seems to be restricted to deal with only the first 256 characters of the ASCII (or ANSI) table. I have to admit I do not really know all these codepage-related things.
- It is a bit strange that the highlighter highlights correctly spelled words (found in the dictionary) and not the other way around (marking not-detected words with a little wave below, as usual). I found no way to achieve this.
Any help from the community is appreciated, I would love to contribute this approach as a coding example for lazarus, as it works at least nicely for English words, but I don't know how/where I could upload the example code.
