Globalization - UserDefaultCulture

Hi Experts,
While my Excel Add-in is executing some code I change the culture info to en-US. After the execution I want to change the culture info to its original value.

My first attempt was to read the culture info when the add-in is loaded. This worked in most cases. In some cases the variable that held the original culture info lost its value. I am now looking for a more reliable solution. I want to read the culture info from windows.

It seems that the information is available. If I type in VS 2010 into the Immediate Window: "My.Computer.Info.InstalledUICulture" the value is shown in UserDefaultCulture.

However during execution I cannnot read this property.

Do you have a example that shows how to access this property?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
We do not see the code that you use, but since you need to reset the original culture when finished, my guess is that you change the culture in the Control Panel. You should never do that. While your add-in is running, any other application started by the user will start with your culture instead of the culture that the user expect. And if your application crash, the user must manually reset it's original culture in the Control Panel. Many users do not know how to do that.

You should change the culture of your add-in instead.

I cannot be sure for an Excel Add-In, because I never use those (good old VBA is not dependant upon the version of Excel and is thus more convenient in my opinion), but for a Windows application, you can change the culture of the application with System.Globalization.CultureInfo.DefaultThreadCurrentCulture = New System.Globalization.CultureInfo("en-US"). It might also work with an add-in.

Since this has no impact on the Control Panel, you do no need to reset the culture afterward.
GeorgZHAuthor Commented:
Hi James,
I do change the culture info by code. Looks like that

Private ReadOnly newCI As CultureInfo = New CultureInfo("en-US")
Thread.CurrentThread.CurrentCulture = newCI

This works fine. The question is how to set it back to the orignal value. Currently I am reading the registry

Friend Shared ReadOnly Property oldCI
                Using _
                    international As RegistryKey = Registry.CurrentUser.OpenSubKey("Control Panel\International", False)
                    Dim userDefaultCulture As String = international.GetValue("LocaleName").ToString()
                    _oldCI = New CultureInfo(userDefaultCulture)
                    Return _oldCI
                End Using
            Catch ex As Exception

                MessageBox.Show("Get oldCI failed. " & ex.Message)

            End Try
            Return Nothing
        End Get
    End Property

Open in new window

However on some clients computer I got a Null reference. So this approach is not very reliable and I'm looking for anther solution.

Jacques Bourgeois (James Burger)PresidentCommented:
Why would you want to change the culture in the CurrentThread, and then bring back the default culture? That means that the culture will change in the application while it is running, This could be very disconcerting for the user.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

GeorgZHAuthor Commented:
Hi James
please check

<<You receive this error calling an Excel method when the following conditions are true: •The method requires an LCID (locale identifier).
•You run an English version of Excel. However, the regional settings for the computer are configured for a non-English language.
If the client computer runs the English version of Excel and the locale for the current user is configured for a language other than English, Excel will try to locate the language pack for the configured language. If the language pack is not found, the error is reported. >>
GeorgZHAuthor Commented:
Hi All,
finally I found a solution in the msdn forum:

The code looks now like this
Imports System.Threading
Imports System.Reflection

Dim CultureType As Type = GetType(Globalization.CultureInfo)
        Dim propci As PropertyInfo = CultureType.GetProperty("UserDefaultCulture", BindingFlags.GetProperty Or BindingFlags.NonPublic Or BindingFlags.[Static])
        Dim ciDefault As Globalization.CultureInfo = TryCast(propci.GetValue(Nothing, New Object(-1) {}), Globalization.CultureInfo)

Open in new window

Where ciDefault contains the User Default Culture Info.

Jacques Bourgeois (James Burger)PresidentCommented:
OK, I get it. I did not remember that problem. I got when the first version of .NET was still in beta, when we tried to get to an English version of Excel while the Control Panel was set to French. Since my customer was able to use the Multilingual User Interface Pack available, we installed that and it solved the problem.

Rereading the thread, I was drawn by the fact that reading the Locale info in the registry returned got you a Null. According to the documentation, you will receive a Null with OpenSubKey if the registry key does not exist. From there, I went on a little research and found out that a missing Control Panel\International entry is often missing in the registry.

I could not find the reason why, but I suspect the following. It would have to be tested, and since I do not have fresh installs of Windows in my environment, I cannot do it. If Windows is installed and no change is made to the Regional Settings, it is possible to assume that the International key is not created and that the system relies on the culture of the installed copy of Windows. I can this prove to be the case, when you would only have to get that culture when opening the SubKey gives you a Null, with system.Globalization.CultureInfo.InstalledUICulture.

I might have a Virtual PC with a default installation of Windows somewhere. I will try to find it and check.
Jacques Bourgeois (James Burger)PresidentCommented:
Hi. I am glad to see that you found a solution. But have you tried it on one of the systems that showed the problem? It might be possible that under the hood, this method also looks for the missing registry key. I would test before claiming victory.

As a follow up to my previous post, I installed a brand new Windows XP mode on one of my Windows 7 systems. And as I though could happen, the LocaleName entry is not there. The Locale entry that returns the LCID is there, but the LocaleName that returns the string representation of the culture is not.

I have tried different things such as changing the culture in the Control Panel, but still no LocaleName. I do not know what triggers its creation. If getting UserDefaultCulture happens not to be working everywhere, I suggest that you go through Locale instead of LocaleName, since it is there from the start.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GeorgZHAuthor Commented:
Hi James,
you are right, Also the new solution reads the value from the registry. To use LCID instead of LocaleName is a valuable hint. Thanks for your input.

Kind regards,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.