locked
C# WPF Multilingual spellcheck on richtextbox RRS feed

  • Question

  • Hi everyone,

    I need to turn spellcheck on for a richtextbox, and set the language to one the user has picked from a drop down. For now, I'm just testing it by building the richtextbox in xaml and providing a language to the xaml language attribute.

    I've read two different resources and one says I need to set the language attribute, and the other says I need to set the xml:lang attribute. Neither seems to work. I've tried setting either one to "es" for Spanish, and I've also tried setting both to "es". I've also tried french by setting them to "fr-FR", without success. The only thing that happens is that english words aren't marked, but the other language words are marked as misspelled.

    I also read that I need to change the keyboard language. This would be a problem for my application as the language within the application needs to be switched on the fly, so having the end user go to their keyboard settings just so spellcheck will work is a problem. However, I've changed my keyboard settings, and spell check still does not work properly. This time it doesn't mark anything as misspelled, even misspelled english words.

    What am I missing?

     

    http://msdn.microsoft.com/en-us/library/system.windows.controls.spellcheck(v=VS.100).aspx

    http://www.dev102.com/2008/03/25/customize-spellcheck-on-wpf-text-controls/

    http://books.google.com/books?id=clLc5BBHqRMC&pg=PA121&lpg=PA121&dq=C%23+wpf+enable+spellcheck&source=bl&ots=_r59pZRDjP&sig=yHMBc39EHKK5gaRMzxlBaEsY890&hl=en&ei=oXnIS8zWH4G88gaq48yGBw&sa=X&oi=book_result&ct=result&resnum=6&ved=0CBMQ6AEwBQ#v=onepage&q&f=false

     

    Friday, April 16, 2010 5:59 PM

Answers

  • Let me try to sort things out one by one.

    First, we exactly components do we need for WPF to enable spell check for English/Spanish/German/French languages?

    This table should make it clear:

     

    Windows XP

    Windows Vista

    Windows 7

    NetFx 3.5 SP1

    Language Packs

    Out of box

    Out of box

    NetFx 4

    Language Packs

    Language Packs

    Language Packs

    Here 'Language Packs' means you need to install corresponding .NET Framework Language Packs. For example, if you want to enable Spanish spell check on Windows XP and .NET Framework 3.5 SP1, then you install the .NET Framework 3.5 SP1 Spanish Language Pack. And if you want to enable German spell check on .NET Framework 4.0, then you install the .NET Framework 4.0 German Language Pack.

    The Language Packs for .NET Framework 4.0 will be available soon.

    Out of box means you don't have to do anything and the spell check for the four languages are automatically available.

    If you're deploying your application via ClickOnce, you can include the corresponding language packs as prerequisites. For help on including language packs in a ClickOnce deployment, you can use the ClickOnce and Setup & Deployment Projects Forum.

    Second, how does RichTextBox control determine the language to check?

    If a Run element is marked with specific language, spell check will use it:

    <RichTextBox SpellCheck.IsEnabled="True" AcceptsReturn="True" AcceptsTab="True">
     <FlowDocument>
      <Paragraph>
       <Run Language="es-es">uno dos tres cuatrro</Run>
       <LineBreak/>
       <Run Language="en-us">one two three fourr</Run>
      </Paragraph>
     </FlowDocument>
    </RichTextBox>

    At runtime, the spell check uses the input language that was used to type in the words, as described in previous post.

    Hope this makes it clear.

    Regards,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Marked as answer by sub_j Wednesday, April 21, 2010 1:17 PM
    Wednesday, April 21, 2010 5:30 AM

All replies

  • Hi,

    Can you let me know which version of .NET Framework are you targeting? 3.0/3.5 or 4.0?

    Thanks,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Monday, April 19, 2010 12:52 PM
  • We're currently in 3.5, with plans to upgrade to 4.0 in the next few weeks.   I had be trying a standalone project with just a RichTextBox in 3.5, but then tried the same thing in 4.0 when it was released.  I got the same results both times.
    Monday, April 19, 2010 7:24 PM
  • Hi,

    there are several things involved here.

    First of all, XAML maps xml:lang and Language to each other, the two are the same.

    Second, the spellchecking dictionary is selected through the Language attribute on the Run level. Language is inherited, so if you want the language of a certain section to be different from the next, you'll have to define this attribute on the Run or Paragraph elements of the FlowDocument which is the RichTextBoxes content. By default, Language is en-US, and all text that is defined in XAML will inherit this language setting.

    Third, the InputLanguageManager.InputLanguage attribute only works on the control level, not on the element level in the FlowDocument, and a changed input language will only be activated after the control loses and regains focus. Also, a changed InputLanguage will not affect text already entered into the control. Only new text will be marked as being in the new language. The split between new and old language might go through the middle of a word, since two adjacent Run elements don't need to be separated by whitespace. In this case, one part of the word will be checked according to the first language, and the other according to the second, which normally doesn't make sense. Also, in order to work, the value of InputLanguage needs to be an exact match to one of the system's defined input languages, i.e. language and culture must be the same, otherwise the keyboard won't be switched. I haven't tested it, but I believe with spell checking it is the same.

    Fourth, if the caret enters a section with a different Language attribute, this doesn't cause the InputLanguage to change automatically. You have to detect this situation in code, and change the InputLanguage in code as well.

    I created an example for a multi-language RichTextBox in this blog post: http://wpfglue.wordpress.com/2009/11/21/switching-the-keyboard-to-different-languages/ It's not perfect; especially, there is a problem with switching back to the original keyboard layout when leaving the RichTextBox. However, it demonstrates how the different attributes work...

    Also, what I wrote here applies to WPF 3.5 SP1, and I hear that some of these or relates issues have been addressed in WPF 4.0, so it might be worth while taking a look.

    I'm very interested in multi language text in RichTextBox controls, so I'd be grateful for anything you find out and post here...


    http://wpfglue.wordpress.com
    Monday, April 19, 2010 7:29 PM
  • Thanks hbarck, I'm looking at your code now.  I'll let you know if I find my answer.

     

    I should mention that I'm not trying to switch languages just yet.  I created a sample project and have been simply switching out the Language attribute everytime I debug.  Inside the grid in the default MainWindow.xaml, i placed:

     

    <RichTextBox Background="#EEEEEE" SpellCheck.IsEnabled="True" Language="es">
            <FlowDocument>
              <Paragraph>
                Spanish- uno dos tres, English- one two three
              </Paragraph>
            </FlowDocument>
          </RichTextBox>

    The first thing I did notice was that it doesn't perform spellcheck on what's already inside the RichTextBox... which is not an issue though.


    The issue I'm having is that when I run this code, and start typing in "one, two, three, uno, dos, tres", the only two words that are underlined as misspelled are "uno" and "tres".  Since this is new text, it gets spellchecked... but in English.  I'm looking for it to be Spanish.  What I want to see is "one, two, three" of the new text marked as misspelled, meanwhile "uno, dos, tres" of the new text is not marked as misspelled.

     

    Based on your post, I quickly tried InputLanguageManager.InputLanguage="es" in place of Language="es", as I expect the entire text box to be one language.  Unfortunately, I still have the same problem that it is spellchecking in english, not spanish.  There is one slight difference though, and that is it now spellchecks the text i have hardcoded in the xaml.  I did change my Regional and location settings, as well as my keyboard, and saw the same issues i was having with spellcheck being essentially turned off.  In other words, even when using InputLanguageManager.InputLanguage, when changing the keyboard language or regional/location settings, nothing was marked as misspelled.

    Another thing I noticed, when I used InputLanguageManager.InputLanguage="fr-FR", my keyboard layout changed to French.  This is definitely helpful to know!  However, with "es" or even "es-ES", my keyboard did not change.

     

    As for what I'm planning on for a future version of this application, a user can input any language they want into the RichTextBox, but it will, or it should, only spellcheck for the language they selected from a drop down.  The application will be setup with an MDI interface, and the user could have several "windows" open within the application, each with its own textbox.  One window could be in English, and should spellcheck in english, while another could be in Spanish, and should thus spellcheck in Spanish.

    Monday, April 19, 2010 8:38 PM
  • Hi,

    what I found helpful when working on this kind of thing was to save the FlowDocument to XAML with XamlSerializer, and to check what kinds of elements and attributes the RichTextBox puts into the document in response to entering text in different input languages.

    Don't know, is there any way to find out what spell checking dictionaries are installed? In the WPF 4.0 documentation, it says that there are dictionaries for English, French, German and Spanish, for WPF 3.5 I didn't find any information.


    http://wpfglue.wordpress.com
    Monday, April 19, 2010 10:41 PM
  • Okay, if you're running on Windows XP (please let me know), try install the language packs for .NET Framework 3.5 SP1:

    http://www.microsoft.com/downloads/details.aspx?FamilyID=8489ed13-b831-4855-96f7-dd35e4c02a20&displaylang=en

    On that page use "Change Language" to download the language pack you need.

    On Windows Vista and Windows 7, the spell check should work out of the box without .NET language packs.

    If you're working with .NET Framework 4.0, then you'll always need the corresponding .NET language packs to enable spell check.

    Please let me know how it goes.

    Thanks,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Tuesday, April 20, 2010 3:05 AM
  • Thanks Jie.  I'm running XP, and testing the Spanish language pack in 3.5 and in 4.0.

     

    In 3.5, the language pack seems to work as far as the designer goes, meaning in my RichTextBox, where I have a hardcoded flowdocument and text ("Spanish- uno dos tres, English- one two three"), "uno dos tres" is not marked as misspelled, but "Spanish, English- one two three" are.  When I run the app, the same is happening, "uno dos tres" is not underlined, but all the English words are.  This works whether or not the regional/location settings are set to Spanish.  So it also works when I have my Region/location as English/United States.  However, as soon as I start typing text inside the RichTextbox while the app is running, regardless of region, only English words aren't underlined, but the Spanish words are underlined.

    When I change my keyboard settings to Spanish, its only then that new words typed in Spanish are not marked, while new English words are marked as misspelled.  One thing to note is that as you change keyboard settings, as mentioned in some of the replies above, it does switch between spellcheck languages.  So if your keyboard is Spanish, you type and English word, then switch your keyboard to English, that English word will still be marked as misspelled.  I haven't examined it, but it looks like each <Run> is marked with the language.  So even though your keyboad is now English, if you right click on that "misspelled" English word in the Spanish run, it will list you a bunch of Spanish suggestions.

     

    In 4.0, none of this works.  Is there another language pack download for 4.0 for XP?  We are looking to upgrade this app to 4.0 as soon as possible, so knowing how to get this working for 4.0 is more important than 3.5.  The designer doesn't spellcheck, and neither does typing inside the richtextbox at runtime.  Switching regions/locations doesn't work, and neither does switching keyboards.  However, swicthing keyboards does have the strange effect of when in English, it spellchecks at runtime in English, but in Spanish, it doesn't spellcheck at all.

    For 3.5 and 4.0, how does this affect release versions to client computers?  Would the language pack(s) be rolled up into the assemblies?  Or would the clients need to download and install the language packs themselves?  If so, could that be solved with ClickOnce, as we are targeting ClickOnce for release, that wouldn't be so bad.

     

    hbarck- Before installing the language pack, I checked again and "es-ES" DOES change the keyboard layout, "es" does not.  I must have made a mistake while testing.  Perhaps I clicked in the wrong spot before looking at the language bar?  We will never know.  Thanks for your help.  It looks like the language packs work off of the keyboard setup, so I may wind up having to change the users' keyboard afterall.  Thanks again!

    Tuesday, April 20, 2010 3:13 PM
  • Let me try to sort things out one by one.

    First, we exactly components do we need for WPF to enable spell check for English/Spanish/German/French languages?

    This table should make it clear:

     

    Windows XP

    Windows Vista

    Windows 7

    NetFx 3.5 SP1

    Language Packs

    Out of box

    Out of box

    NetFx 4

    Language Packs

    Language Packs

    Language Packs

    Here 'Language Packs' means you need to install corresponding .NET Framework Language Packs. For example, if you want to enable Spanish spell check on Windows XP and .NET Framework 3.5 SP1, then you install the .NET Framework 3.5 SP1 Spanish Language Pack. And if you want to enable German spell check on .NET Framework 4.0, then you install the .NET Framework 4.0 German Language Pack.

    The Language Packs for .NET Framework 4.0 will be available soon.

    Out of box means you don't have to do anything and the spell check for the four languages are automatically available.

    If you're deploying your application via ClickOnce, you can include the corresponding language packs as prerequisites. For help on including language packs in a ClickOnce deployment, you can use the ClickOnce and Setup & Deployment Projects Forum.

    Second, how does RichTextBox control determine the language to check?

    If a Run element is marked with specific language, spell check will use it:

    <RichTextBox SpellCheck.IsEnabled="True" AcceptsReturn="True" AcceptsTab="True">
     <FlowDocument>
      <Paragraph>
       <Run Language="es-es">uno dos tres cuatrro</Run>
       <LineBreak/>
       <Run Language="en-us">one two three fourr</Run>
      </Paragraph>
     </FlowDocument>
    </RichTextBox>

    At runtime, the spell check uses the input language that was used to type in the words, as described in previous post.

    Hope this makes it clear.

    Regards,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Marked as answer by sub_j Wednesday, April 21, 2010 1:17 PM
    Wednesday, April 21, 2010 5:30 AM
  • Thank you very much Jie.  This is exactly the answer I'm looking for.  The MSDN site needs to say this, but only says this below:

    "To enable the spelling checker, set the SpellCheck. IsEnabled property to true on a text-editing control. When this spelling checker is enabled, misspelled words are underlined by using a red wavy line, as shown in the following illustration. Spelling checker is supported only when WPF provides a default dictionary. In .NET Framework 4, WPF provides dictionaries for English, French, German, and Spanish."- from the link I provided in my original post (http://msdn.microsoft.com/en-us/library/system.windows.controls.spellcheck(v=VS.100).aspx)

     

    Not only does it not mention language packs for 4.0, it says the WPF framework provides this.  I will post this forum link under the community section, but this should really be changed to reflect how multilingual spellcheck works.

     

    Thanks again!

    Wednesday, April 21, 2010 1:22 PM
  • NetFx 4 language packs appeared now, I installed the Hungarian one and tried the spelling: not works. So, I missed something or the spelling can work ONLY for 4 languages?
    Thursday, July 1, 2010 11:34 AM