Search code examples
c#silverlightsystemcultureinfoculture

System Culture in a SIlverlight Application


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


Solution

  • 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.