0

I can't figure out how to get the CultureInfo of the Installed System on the Client Machine.

There is the CultureInfo.InstalledUICulture Property, but it seems to be unavailable in Silverlight.

Regards Jonny

Johannes Wanzek
  • 2,825
  • 2
  • 29
  • 47
  • Wouldn't [Culture.CurrentCulture](http://msdn.microsoft.com/en-CA/library/system.globalization.cultureinfo.currentculture%28v=vs.95%29.aspx) be what you're looking for? EDIT: Mind you, that's the culture for the current thread (which can be programmatically changed), but I _think_ by default it's set to the culture of the user's machine/operating system. – Chris Sinclair Apr 26 '13 at 10:37
  • Hey, the CurrentCulture will be definitly changed during the Application. So I cant trust this Value. Sorry that I didn't mention this in my original post. – Johannes Wanzek Apr 26 '13 at 10:58
  • No other suggestions? – Johannes Wanzek Apr 30 '13 at 07:20
  • I updated my answer with another suggestion. – Chris Sinclair Apr 30 '13 at 11:09

1 Answers1

4

I believe that Culture.CurrentCulture will in fact provide you with the user's culture. It can however change or be programmatically set via current thread's Thread.CurrentCulture property. I'm not sure if Silverlight can access the user's machine/operating system culture/language settings beyond this mechanism.

As you mention in a comment, you cannot trust it as it will definitely change through the lifetime of the application. Perhaps then you should record the current culture when the application first starts up before it's programmatically changed, and store it indefinitely (statically or otherwise) to be referenced by your code.

EDIT: Another possibility is to leverage the hosting browser and its JavaScript. Googling around I see that you can access window.navigator.language which will report the language of the browser. Internet Explorer likes to do its own thing and reports the browserLanguage, userLanguage, and systemLanguage.

You can write up a small JavaScript method on the page (you will want to do more cross-browser tests, version tests, and operating system tests):

function GetUserLanguage()
{
    if (window.navigator.language)
        return window.navigator.language;
    else //yay IE
        return clientInformation.browserLanguage;
}

Then in Silverlight you might have something like:

string userLanguage = (string)HtmlPage.Window.Invoke("GetUserLanguage");
CultureInfo userCulture = new CultureInfo(userLanguage);

I'm not sure if all cultures reported by the browser (across all browsers/versions/operating systems) will match the culture listing in Silverlight.

Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • Hey, thanks again for your detailed answer. As a little workaround I now use CurrentCulture and save it into my Application Settings at Application Start. At this point I have no other choice than assume this is the current system language. – Johannes Wanzek Apr 30 '13 at 11:25
  • __ReferenceError: windows is not defined__ correct - **window.navigator.language** – Alex Skiffin Dec 15 '16 at 04:51
  • @AlexSkiffin: Thanks for picking up on that typo; I've fixed it now. – Chris Sinclair Dec 15 '16 at 17:37