none
How To Override The DigitShapes Setting In C# .NET 4.0? (Use Native Digits Setting in Region and Language Dialog) RRS feed

  • Question

  • I have a C# form application that needs to override the DigitShapes (Use Native Digits) setting.  What I need to achieve is to always display digits using their original shapes.  So I need to always run with the DigitShapes set to "None", regardless of the Use Native Digits setting in the "Region and Language" setting in Windows.  (I am testing in Windows 7).

    For example, if the character value is between 0x30 and 0x39 I need to display the numeric using the western Arabic numerals "123" etc. (http://en.wikipedia.org/wiki/Arabic_numerals)   If the character value is between 0x660 and 0x669, I want it to display with the Indic glyph (http://en.wikipedia.org/wiki/Eastern_Arabic_numerals)

    In the windows "Region and Language" dialog, when "Use Native Digits" is set to "Never", then the application numerics always displays as desired.  When "Use Native Digits" is set to "Context", then values in the 0x30 to 0x39 will display with the Indic glyph when the direction is right to left (RTL) or if it is preceded by a BiDi character with a RTL directionality.  My understanding is that the DigitShapes setting maps to the "Use Native Digits" setting in the "Region and Language" dialog.

    By default, the arabic locales default "Use Native Digits" to "Context".  I want to force my application to always run as if the setting is "Never".

    I have been attempting to override the locale setting in my application to force DigitShapes to be "none".  The sample code below gets the current locale context and overrides the DigitShapes and then resets the culture for the application.  In the debugger I can see that if I query the thread culture, it does indeed have DigitShapes set to "None".  However, numerics are still being substituted during display in a RTL environment.

    I have tried calling ClearCachedData, but that doesn't seem to help.  Is this a bug or am I missing a step to force my application to never substitute digits? 

    Sample code

            static void Main(string []args)
            {            
                System.Globalization.CultureInfo cinfo = (System.Globalization.CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
                Thread.CurrentThread.CurrentCulture.ClearCachedData();
                Application.CurrentCulture.ClearCachedData();
                cinfo.NumberFormat.DigitSubstitution = System.Globalization.DigitShapes.None;
                Thread.CurrentThread.CurrentCulture = cinfo;
                Thread.CurrentThread.CurrentUICulture = cinfo;
                Application.CurrentCulture = cinfo;           

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MyForm(args));
            }

    Help is appreciated.

    Robert

    Wednesday, May 29, 2013 1:40 PM

All replies

  • Hi Robert,

    You need to related language package. By default, there only English language is installed. 

    Please visit the download center to get the corresponding language pack: http://www.microsoft.com/en-us/download/default.aspx  

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 30, 2013 7:04 AM
    Moderator
  • Hi Mike,

    Thanks for the response but I believe you misunderstand the issue. The issue is not that the Indic characters are not displaying... the issue is that characters with a value of 0x30 is being displayed as an Indic character "0" instead of the US character "0". This is how Windows works when running with an Arabic locale and the "Use Native Digits" is set to "Context" in the Region and Lanuage dialog. Windows controls are performing this glyph substitution on the fly.

    My problem is that I want the character 0x30 to always display as a US "0" in my application. This occurs when "Use Native Digits" is set to "None" in the windows Region and Language dialog. However, the default setting for the Arabic locales is "Context", so I need to override that at runtime in my application. Even though I have overridden the culture setting, that I believe is the correct setting in my code sample above, when I run on an Arabic locale, the value 0x30 is still being displayed with an Indic digit. This occurs in edit boxes and text fields. I need to override the default shape setting so no substitution occurs within my application. If I want to display an Indic digit, I will use the Unicode value 0x660.

    That being said, I have tested in environments with the Arabic language pack installed. The result is the same. I have tested with the OS language set to English and with the OS running in Arabic, the results are the same. The language pack is not the solution here.

    Thanks, Robert

    Thursday, May 30, 2013 11:04 AM
  • Hi Robert,

    Sorry for misunderstanding you.

    Based on you further description, I got you.

    You have mentioned, when you set this setting on OS, the displayed number is exactly what you expected. But you set the similar setting in the thread culture, it doesn't work well.

    Now, I am trying to involve some other one into this case, it will take some times. Thank you for your patience.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 31, 2013 12:48 AM
    Moderator
  • Hello Mike,

    I have a very simple sample application that shows the problem.  It is just a plain form with a single text box. I do not see a way to attach it here to the forum.  I could email it to you.  Please let me know as I need to resolve the issue.

    I tried to attach an image of the form, but the forum will not allow me to post an image until I verify my account, which I believe is already done before I could post, and I have searched and searched and cannot find anyway to verify my account.  My MSDN subscrption was purchased by my company and it is associated with my email.   I cannot find any kind of a link to verify my account. 

    Thank you,

    Robert

    Monday, June 3, 2013 1:34 PM
  • Hi Robert,

    You can try this workaround to send the test project to me:  upload it to skydrive and post the link here.

    >>I cannot find any kind of a link to verify my account. 

    try this: http://social.technet.microsoft.com/wiki/contents/articles/15960.how-to-verify-your-msdntechnet-forums-account-so-that-you-can-post-images-and-links.aspx

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 4, 2013 2:46 AM
    Moderator
  • Hi Mike,

    The file is here:

    skydrive.live.com/embed?cid=D8827D2DFA4B41F6&resid=D8827D2DFA4B41F6%21108&authkey=AFgVAsUeewvRzps

    (I still don't have a verified account so I removed the beginning of the address so I could post it)

    When you run the application it displays the current locale and it displays the shape setting.

    To reproduce the problem, set the locale in Region and Language to Arabic (Saudi Arabia).  Run the application.  Then with your keyboard set to english, try typing numbers 1 2 3, you will see they display as Indic characters even though the applicaiton is overriding the setting.

    If you go to Region and Language and change the "Use Native Digits" to "Never", then start the application again, only at that point does the applicaiton display US digits.  Either I am not making the proper setting in my applicaiton to prevent digit substution or there is a bug.  Please let me know.

    Thanks,

    Robert

    Tuesday, June 4, 2013 12:35 PM
  • Hi Mike, Have you been able to figure out why my sample application is not working as expected?

    Thank you,
    Robert.


    Robert

    Tuesday, June 11, 2013 1:59 PM
  • Hi Robert,

    It seems it is only configured in Region and Language of OS. Is there any config API in Code?


    James

    Wednesday, June 19, 2013 2:46 AM
  • Hi James,

    Yes, there is an API.  If you look at my sample application you can see that I am disabling the numeric substitution here:

                System.Globalization.CultureInfo cinfo = (System.Globalization.CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
                Thread.CurrentThread.CurrentCulture.ClearCachedData();
                Application.CurrentCulture.ClearCachedData();
                cinfo.NumberFormat.DigitSubstitution = System.Globalization.DigitShapes.None;
                Thread.CurrentThread.CurrentCulture = cinfo;
                Thread.CurrentThread.CurrentUICulture = cinfo;
                Application.CurrentCulture = cinfo;     

    Unfortunately, the controls are not respecting this setting, instead they are still using the value set in the OS in the Region and Settings dialog.
    Robert


    Robert

    Wednesday, June 19, 2013 10:51 AM
  • Hi Robert,

    The control would respect the setting in future .net lib...

    In .net v4.0,  NumberFormat.DigitSubstitution is not supported.  You could review the remark part in MSDN document.

    Hope, there exists another  way to reach it....


    James

    Friday, June 21, 2013 3:28 AM