Multilanguage Support
RadSpellChecker spell checks words by comparing them to a predefined list, hosted in a dictionary (a .TDF file) associated with a specific language. In Telerik UI for WPF you can find the assembly Telerik.Windows.Documents.Proofing.Dictionaries.En-US which contains a class deriving from RadDicitonary that can be used for en-US culture.
If you want to use RadSpellChecker with another culture, you need to write your own class implementing RadDictionary, in which you have to load the corresponding .TDF file. You can find some available dictionaries here or you can write one of your own following the steps described in this blog post.
Loading a dictionary through MEF
As RadSpellChecker instantiates and loads the classes implementing RadDictionary through MEF, when you write your class do not forget to mark it with the [WordDictionaryMetadata("
")] attribute, providing as argument the appropriate culture.
To use RadSpellChecker in Spanish for example, load the corresponding .TDF file and mark the class with [WordDictionaryMetadata("es-ES")] attribute.
RadSpellChecker matches every type it supports to an instance of a class deriving from IControlSpellChecker. This class contains a property of type DocumentSpellChecker, which in turn contains SpellCheckingCulture property of type CultureInfo. The DocumentSpellChecker class holds a dictionary matching a CultureInfo to a RadDictionary. By setting SpellCheckingCulture property you simply tell the DocumentSpellChecker to use the dictionary corresponding to that culture.
To retrieve the corresponding IControlSpellChecker for your control (in our case TextBox) you can use the static class ControlSpellCheckersManager. Here is the code to set the spellchecking culture of RadSpellChecker for the TextBox Control to Spanish:
//Here we take the IControlSpellChecker instance for the TextBox Control
IControlSpellChecker controlSpellchecker = ControlSpellCheckersManager.GetControlSpellChecker(typeof(TextBox));
//We get the SpellChecker property and cast it to DocumentSpellChecker (which inherits from ISpellChecker)
DocumentSpellChecker documentSpellChecker = (DocumentSpellChecker) controlSpellchecker.SpellChecker;
//Then we set the SpellCheckingCulture of DocumentSpellChecker to Spanish
documentSpellChecker.SpellCheckingCulture = new System.Globalization.CultureInfo("es-ES");
As IcontrolSpellChecker.SpellChecker property is of type ISpellChecker you need to cast it to DocumentSpellChecker.
So, to summarize, thanks to MEF you can load an instance of a class, containing a dictionary for any language, and by setting the SpellCheckingCulture property you switch to that language.
Loading a dictionary explicitly
There is another way to load a dictionary explicitly (without MEF). To achieve that you need to use the SpellChecker property of IControlSpellChecker and cast it to DocumentSpellChecker. The cast is needed because DocumentSpellChecker contains a method called AddDictionary(IWordsDictionary dictionary, CultureInfo culture) which we can use to add a CultureInfo – RadDictionary pair to the DocumentSpellChecker and thus when we set the SpellCheckingCulture property, it will find the appropriate RadDictionary and use it to generate suggestions.
In fact the code here is pretty much the same as the code above:
//Here we take the IControlSpellChecker instance for the TextBoxControl
IControlSpellChecker controlSpellchecker = ControlSpellCheckersManager.GetControlSpellChecker(typeof(TextBox));
//We get the SpellChecker property and cast it to DocumentSpellChecker(which inherits from ISpellChecker)
DocumentSpellChecker documentSpellChecker = (DocumentSpellChecker) controlSpellchecker.SpellChecker;
//Then we add the class deriving from RadDictionary and the Culture info to the DocumentSpellChecker(we are using Spanish again for the example)
documentSpellChecker.AddDictionary(new RadEs_ESDictionary(), new System.Globalization.CultureInfo("es-ES"));
In this case the RadEs_ESDictionary class is located in another assembly and inherits RadDictionary:
public class RadEs_ESDictionary : RadDictionary
{
public RadEs_ESDictionary()
{
}
protected override void EnsureDictionaryLoadedOverride()
{
Stream stream = Application.GetResourceStream(new Uri("SpellCheckingInSpanishRRTB;component/es-ES.tdf", UriKind.Relative)).Stream;
this.Load(stream);
}
}