none
How to change current UI culture at runtime?

    Question

  • HI.

    I have a form and I want to change its UI culture at runtime.
    The method System.Threading.Thread.CurrentThread.CurrentUICulture = ... does not working in .Net CF.
    I am using VS .Net 2005Beta2, C#.

    Thanks.
    Friday, September 09, 2005 12:05 PM

Answers

All replies

  • That is correct. Changing the UI culture at runtime is not supported (in v1 or v2):
    http://www.danielmoth.com/Blog/2004/11/cultureinfo.html

    Cheers
    Daniel
    Saturday, September 10, 2005 4:17 PM
  • Is there some way to set the UI culture in the environment to test the various languages?

    ---Judy
    Saturday, November 19, 2005 11:12 PM
  • So you are asking if anything has changed since I wrote that blog entry? The answer is no - please read it again for what is possible and what not (be sure to follow the MSDN links).

    Cheers
    Daniel
    Saturday, November 19, 2005 11:19 PM
  • Well, no, that wasn't what I was asking.
    I read and accepted the fact that I cannot change the culture at run time. I was hoping that something in the development environment would allow me to test other cultures during development, which is a separate issue.

    ---Judy
    Sunday, November 20, 2005 2:10 AM
  • Judy, your question is still not clear. During development, the obvious way to test things is
    1. Change the language on the device
    2. Run your app & test
    3. Go to step 1 and repeat

    You accept that you cannot change it at runtime. If you read the link, you know can change it at runtime *but* it would not take automatic effect on your forms without restarting - let's be specific here.

    So if the runtime route is out, the only thing left is the design time. At design time, you can change your form's Localizable property and assign new strings (again, following the links from my article covers that).

    Other than that, there is not anything else to say on the topic.

    If I am still not getting your question, try rephrasing it please.

    Cheers
    Daniel
    Sunday, November 20, 2005 10:52 AM
  • Daniel:

    Maybe there is a really easy step that I'm not seeing. I want to make a test run of a localized app in the MS emulator. I have made the form localizable and set the language strings, but cannot find a way to test it. I'd like to run it once in, say, French. Then stop execution and run it in Spanish. Then a different language. I don't expect to change it during the run, but would just like to be able to see the output.

    Is there a way?

       ---Judy
    Monday, November 21, 2005 1:20 AM
  • Have you tried between runs changing the language on the device (through the Settings->System->Regional settings)?

    Cheers
    Daniel

    Saturday, November 26, 2005 9:48 AM
  • why don't you try this code:Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");The first line is used for changing the culture on the server and the 2nd line is used for changing the user interface.Have fun,Tidus
    Tuesday, April 18, 2006 7:27 AM
  • that doesnt work!!
    Friday, September 01, 2006 2:29 PM
  • It works!!! But!!! You must modify this property before the form has loaded.
    For example:

    [STAThread]
    public static void Main(string[] args)
    {
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("it");
    Application.Run(new MyMainForm);
    }

    That's why you can use it only for testing. But if I want to change it during run-time. Like selecting menu item "Language->Italian", how I can do it (the simplest way please ) ?
    Friday, March 02, 2007 10:26 AM
  • Of course it does not work, there's no such property on NETCF. It won’t even compile for that matter. Just change your device locale to whatever it should be.

     

    Alternatively you could use reflections to change locale for entire application. This is hack which can stop working at any moment so use it only if you absolutely have to. If you're VB user translate it with one of these online translators.

     

            public static void SetDefaultLocale( System.Globalization.CultureInfo locale)

            {

                if (null == locale)

                {

                    throw new ArgumentNullException("locale");

                }

                System.Reflection.FieldInfo fi = typeof(System.Globalization.CultureInfo).GetField("m_userDefaultCulture", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

               

                if (null == fi)

                {

                    throw new NotSupportedException("Setting locale is not supported in this version of the framework.");

                }

                   

                fi.SetValue(null, locale);

            }

    • Proposed as answer by Shammi Theodore Thursday, December 25, 2008 3:38 PM
    Friday, March 02, 2007 8:48 PM
    Moderator
  •  

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    Monday, September 10, 2007 1:33 PM
  •  Saurabh11 wrote:

     

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");



    you must chang your forum!
    Tuesday, June 24, 2008 7:00 AM
  •  AMD wrote:
    HI.

    I have a form and I want to change its UI culture at runtime.
    The method System.Threading.Thread.CurrentThread.CurrentUICulture = ... does not working in .Net CF.
    I am using VS .Net 2005Beta2, C#.

    Thanks.


    If your culture is not supported in PocketPC, just use another Culture string name which is supported in your device, like the following code in line 6.


    Code Snippet

    public static void LoadResource(Form form, string lan)

        {

            ResourceManager rm = new ResourceManager(form.GetType().FullName, Assembly.GetExecutingAssembly());

            CultureInfo ci;

            if (lan == "fa")

            {

                ci = new CultureInfo("en-GB");

            }

            else if (lan == "en")

            {

                ci = new CultureInfo("en-US");

            }

            else if (lan == "de")

            {

                ci = new CultureInfo("de-DE");

            }

            else

            {

                ci = new CultureInfo("en-US");

            }

     

            IDictionaryEnumerator id = rm.GetResourceSet(ci, true, true).GetEnumerator();

     

            while (id.MoveNext())

            {

                string key = (string)id.Key;

                if (key.StartsWith("$"))

                {

                    key = key.Substring(1);

                }

                int index = key.IndexOf('.');

                string cName = key.Substring(0, index);

                string cProperty = key.Substring(index + 1);

     

                if (cName == "this")

                {

                    form.GetType().GetProperty(cProperty).SetValue(form, id.Value, null);

                }

                else

                {

                    //= new Control();// = form.Controls[cName];                   

                    IEnumerator ie = form.Controls.GetEnumerator();

                    while (ie.MoveNext())

                    {

                        if (((Control)ie.Current).Name == cName)

                        {

                            Control control = (Control)ie.Current;

                            control.GetType().GetProperty(cProperty).SetValue(control, id.Value, null);

                        }

                    }

                }

            }

     

        }

     




    Tuesday, July 01, 2008 1:06 PM

  • Thanks to the reflections solution, I am able to overcome a big headache associated with registration for my software. I can now change the CultureInfo during runtime, just when its required and revert it back immediately.

    Ilya Tumanov said:

    Of course it does not work, there's no such property on NETCF. It won’t even compile for that matter. Just change your device locale to whatever it should be.

     

    Alternatively you could use reflections to change locale for entire application. This is hack which can stop working at any moment so use it only if you absolutely have to. If you're VB user translate it with one of these online translators.

     

            public static void SetDefaultLocale( System.Globalization.CultureInfo locale)

            {

                if (null == locale)

                {

                    throw new ArgumentNullException("locale");

                }

                System.Reflection.FieldInfo fi = typeof(System.Globalization.CultureInfo).GetField("m_userDefaultCulture", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

               

                if (null == fi)

                {

                    throw new NotSupportedException("Setting locale is not supported in this version of the framework.");

                }

                   

                fi.SetValue(null, locale);

            }



    Thursday, December 25, 2008 3:46 PM