Wednesday, April 4, 2012

Passing Display Language from SharePoint 2010 to a Silverlight app

This is related to cultures, my current obsession.  Running a Silverlight app in a browser will allow you to change the browser language and have the Silverlight app respond appropriately.

SharePoint 2010 has a "Display Language" option for each user - as long as the appropriate SharePoint 2010 language pack has been installed, the SharePoint site will be changed to show translations that have been assigned for that language.  However, as the browser language hasn't changed, Silverlight web parts don't reflect that change.

The solution?  Create a new Silverlight web part control that inherits from System.Web.UI.WebControls.WebParts.WebPart, and in the Render section (that outputs to a supplied HtmlTextWriter called writer) -

            string s = String.Format(@"              
                <div id='SilverlightObjectDiv_{0}' style='display: block; overflow:hidden'>
                    <object class='ms-dlgDisable' id='SilverlightObjectTag_{0}' width='100%' height='{2}'
                        data='data:application/x-silverlight-2,'
                        type='application/x-silverlight-2' >
                        <param name='source' value='{1}'/>
                        <param name='initParams' value='MS.SP.url={3},culture={4}'/>
                        <param name='culture' value='{4}' />
                        <param name='uiculture' value='{4}' />
                        <!-- Display installation image. -->
                        <a href='http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0'
                            style='text-decoration: none;'>
                            <img src='http://go.microsoft.com/fwlink/?LinkId=108181'
                                alt='Get Microsoft Silverlight'
                                style='border-style: none'/>
                        </a>
                    </object>
               <iframe id='_sl_historyFrame' style='border-bottom: 0px; border-left: 0px; width: 0px; display: block; height: 0px; visibility: hidden; border-top: 0px; border-right: 0px'></iframe>&#160;
                </div>      
                ",
                 this.ClientID.ToString(),
                 _xapFileUrl,
                 (this.Height.Value - 20).ToString(),
                 SPContext.Current.Web.Url, System.Threading.Thread.CurrentThread.CurrentUICulture.ToString()                
                 );


            writer.Write(s);

The important thing to note here is the "culture" and "uiculture" params - System.Threading.Thread.CurrentThread.CurrentUICulture would normally pass the browser language but when running as a SharePoint web part, seems to pass the chosen display language instead.

No comments:

Post a Comment