Changing the 'Region and Language' OS setting programmatically
Asked Answered
M

6

7

I'd like to be able to change the Region and Language settings of the operating system (Windows 7) from a C# program. I'm not against executing command-line commands but I've only gotten as far as discovering how to launch the Region and Language dialog: control /name Microsoft.RegionAndLanguage

This is a language localisation problem where Controls like DateTimePicker can only use the Windows Region and Language settings (see here for details); however updating the operating system to conform to the application's language settings extends beyond this and is ultimately the desired goal.

Suggestions and/or workarounds would be appreciated.

Monogenesis answered 2/3, 2012 at 8:46 Comment(4)
I think you shouldn't change the system's language settings. The best you can do is create a new, localizable, DateTimePicker control, or search for a ready-made one.Chloramine
Just a guess, won't setting up a custom format for the Date picker control solve your purpose ?Simdars
@Simdars no, the custom format is not enough, he might want to change other settings, like the first day of the week, etc.Chloramine
The software I'm developing effectively replaces the desktop; changing the language within the software should seamlessly update the OS - which is only accessible from within the software. So in this case it's ideal that the OS conforms. I've also consider using alternatives to the DateTimePicker (which is a bit undesirable) but while responding I realize that changing the OS language would be important. I'll update my original post accordingly.Monogenesis
M
4

The only solution I managed to implement was to modify the registry. In Windows 7, when the language is changed, a new entry is added to the Registry in the subkey: HKEY_CURRENT_USER\Control Panel\Desktop. This key will contain the entry PreferredUILanguagesPending of type REG_MULTI_SZ and its value will determine the UI language. For the change to be applied the current user needs to log off and log in again. This can be done using the following code:

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
string[] lang = {"en-ZA"};
key.SetValue("PreferredUILanguagesPending", lang, RegistryValueKind.MultiString);

The language pack needs to be installed before it can be set. For a list of language packs check here or here. When more than 1 language pack is installed option to change the UI language will appear in Control Panel > Region and Language > Keyboards and Languages > Display language.

Monogenesis answered 5/3, 2012 at 11:31 Comment(0)
S
3

Sounds to me as if changing the Culture/UICulture of your application should be sufficient

e.g.

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Sunset answered 2/3, 2012 at 9:2 Comment(2)
don't think so, in the provided link Microsoft says: This behavior occurs because the DateTimePicker control and the MonthCalendar control are Microsoft Windows common controls. Therefore, the operating system's user locale determines the user interface of these controls.Chloramine
I stand corrected and agree with your comment to the OPS question, build a new controlSunset
C
1

I've found a good replacement for the DateTimePicker: http://www.visualhint.com/fieldpackeditor

You will have the same problems with all the system controls and system dialogs like OpenFileDialog, PrintDialog, etc., they are not localizable in .NET.

But thinking about it, why would you want to change the culture for your application? The user can change his region and language settings by himself using the control panel, why should your application overwrite those settings?

Chloramine answered 2/3, 2012 at 9:14 Comment(1)
We only use the DateTimePicker in a single form; I don't think our company would approve of purchasing new software to achieve this goal. However, I have realized that my goal extends beyond DateTimePicker. Thank you for your reply.Monogenesis
E
1

I found a solution here: https://support.microsoft.com/de-de/help/2764405/how-to-automate-regional-and-language-settings-in-windows-vista-window

You need to create an XML file with the following content:

<gs:GlobalizationServices xmlns:gs="urn:longhornGlobalizationUnattend">
    <gs:UserList>
        <gs:User UserID="Current"/>
    </gs:UserList>
    <gs:UserLocale>
        <gs:Locale Name="en-US" SetAsCurrent="true"/>
    </gs:UserLocale>
</gs:GlobalizationServices>

Then run this command:

control intl.cpl,, /f:"<path to XML file>"

This would set the format to English (United States) for the currently logged in user.

Although the article is about Windows Vista, this also works in Windows 7.

Episcopal answered 7/2, 2019 at 16:12 Comment(2)
to Change decimal symbol, What we have to add to this XML?Perrie
I updated the link to the source. There is an option <gs:sDecimal>, but I didn't get it working...Episcopal
T
0

I think, you are thinking in opposite to what you need. Often application(s) may need to help/honor/satisfy user OS settings than changing them for app's convenience. Have a look at the post with problem similar to your's : How can I change a Windows user's regional settings/date format?

Teflon answered 2/3, 2012 at 9:22 Comment(1)
Thank you for your reply. I had also found a similar post here. It seems that modifying the registry is the only possibility.Monogenesis
P
0

This is a language localisation problem where Controls like DateTimePicker can only use the Windows >Region and Language settings

NUTS. Seriously. Think about what you do here; I start your program and you reconfigure my whole computer to make a small UI element work? Preferably without asking or telling me?

There are laws against that - this can be seen as unauthorized manipulation of a computer.

Use a proper control and properly program; but do not commit sabotage on that level.

Pyosis answered 2/3, 2012 at 9:26 Comment(4)
I'm sorry you feel so strongly about this. I have updated my post to provide a better context: ultimately this behaviour would be the expected result from the end user.Monogenesis
Agree. That said iyou ahve a veryu nusual piece of software, so ... bear with me tha this was not obvious.Pyosis
I understand; I hadn't defined my situation well enough to ask for advice. We live and we learn :)Monogenesis
This was an extremely unhelpful response. While the original question may have been poorly framed, the general issue of automated changes to Windows regional settings is still of interest and deserves an appropriate (if qualified) response. It's fair to observe that doing this in an unwanted way would be upsetting to a user, however, it's perfectly reasonable to develop date handling utilities. In my case, I would like to change settings to enable automated testing under different (genuine) culture settings - an answer here would still serve me.Visitant

© 2022 - 2024 — McMap. All rights reserved.