Skip to main content

ISpellCheckService Interface

Allows you to implement a custom spell check service.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.v23.2.dll

NuGet Package: DevExpress.Blazor

Declaration

public interface ISpellCheckService

Remarks

If the Rich Text Editor’s built-in spell check service does not meet your requirements, you can follow the steps below to implement a custom service.

1. Create a Spell Check Service

Create a class that implements the ISpellCheckService interface:

using DevExpress.Blazor.RichEdit;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

public class MySpellCheckService : ISpellCheckService {
    public CultureInfo Culture { get; set; }
    public bool AddToDictionaryEnabled { get; }
    public int MaxSuggestionCount { get; }
    string PathToDictionary { get;}
    List<string> CorrectWords { get;}

    public MySpellCheckService(string path) {
        PathToDictionary = path;
        Culture = CultureInfo.InvariantCulture;
        AddToDictionaryEnabled = true;
        MaxSuggestionCount = 7;
        CorrectWords = File.ReadLines(PathToDictionary).ToList();
        CorrectWords.Sort();
    }

    public void AddToDictionary(string word) {
        if (Culture.TwoLetterISOLanguageName == "en") {
            word = word.ToLower();
            int placeToInsert = CorrectWords.BinarySearch(word);
            CorrectWords.Insert(placeToInsert, word);
            File.AppendAllText(PathToDictionary, word + '\n');
        }
    }

    public bool CheckWordSpelling(string word) {
        if (Culture.TwoLetterISOLanguageName == "en") {
            int index = CorrectWords.BinarySearch(word.ToLower());
            return (index >= 0);
        }
        return false;
    } 

    public string[] GetSpellingSuggestions(string word) {
        List<string> suggestions = new List<string>();
        // Generate suggestions and add them to the list
        return suggestions.ToArray();
    }
}

2. Register the Custom Spell Check Service

Call the AddScoped, AddSingleton, or AddTransient method to add your spell check service to the application’s service collection:

public void ConfigureServices(IServiceCollection services) {
    services.AddSingleton<ISpellCheckService>(new MySpellCheckService("Data/MyDictionary.dic"));
    services.AddDevExpressBlazor();
}

Note

The Rich Text Editor disables the built-in spell check service after you add a custom service that implements the ISpellCheckService interface.

3. Configure the Rich Text Editor

Set the Rich Text Editor’s CheckSpelling property to true to enable the spell-check feature, and use the component’s DocumentCulture property to set the culture of an open document. Depending on the current document culture, you can change the behavior of the custom spell check service.

The example below demonstrates how to enable spell checking and set the control’s DocumentCulture property value:

<DxRichEdit @ref="@richEditor" CheckSpelling="true" DocumentCulture="en-US" />
See Also