Search code examples
c#locale

Change system default language programmatically with c#


We have a virtual keyboard (for touch screen) which its language layout is configured via the windows default language.

I have seen numerous answers which involves InputLanguageManager and CultureInfo.
They're not useful to me, didn't do the job.

There is this one method - SystemParametersInfo function with the SPI_SETDEFAULTINPUTLANG flag that i'm trying to check.
So far, didn't find any useful usage examples besides this one here, but it changes the keyboard layout from Dvorak to Marshal.

Can you give me an example (hopefully with the SystemParametersInfo) that converts the default system language to en-US?

Edit

A brief clarification.
This program replaces the explorer as windows shell, hence all keyboard settings such as setting default keyboard layout should be handled from my program.
Moreover, my wish is to replace between different installed languages such as English, Swedish, Portuguese and so on..
I don't want to change between Dvorak and Qwerty layout of the keyboard.

The purpose of this post is to ask for examples for changing between different languages and not for layout of English symbols on keyboard.

Thanks!


Solution

  •    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref uint pvParam, uint fWinIni);
    
        public void Foo()
        {
            uint localeUS = 0x00000409;
            uint localeNL = 0x00000403;
            SetSystemDefaultInputLanguage(localeUS);
        }
    
        public bool SetSystemDefaultInputLanguage(uint locale)
        {
            return SystemParametersInfo(SPI_SETDEFAULTINPUTLANG, 0, ref locale, 0);
        }
    
        public uint GetSystemDefaultInputLanguage()
        {
            uint result = uint.MinValue;
            bool retVal = SystemParametersInfo(SPI_GETDEFAULTINPUTLANG, 0, ref result, 0);
    
            return result;
        }
    

    This seems to work fine for me.

    Sources: