I have a requirement for a project I’m working on to make all strings in the UI of a SharePoint 2007 site localized. The problem was that the rest of the site collection needed to stay in English, this meant all the system and admin pages needed to stay in English while all the user interface needed to be localized. The requirement also stated that an admin of the site collection could change this is the admin pages.
I immediately turned to ASP.Net resource files as this would achieve exactly what I wanted, I wanted to use the Regional Settings -> Locale setting in the site collection to determine the locale to show. The problem with this was that changing the sites Regional Settings Locale had no effect on the CurrentUICulture that the current ASP.Net thread was running under, this meant that ASP.Net would load the resource file for the user’s browser not the resource file for the locale of the site collection.
I used the following blog post from Mikhail Dikov which is a good example of how to use ASP.Net resource files in a SharePoint site. The blog post fails to mention however that for the culture to change within a SharePoint site you need the language packs installed and this would affect the whole site collection, admin pages and all.
So the following is an outline of how I achieved my goal:
First I created a default resource file which contained all the English strings and then resource files for each locale I wanted to support.
- Next I deployed these resource files into my SharePoint web applications App_GlobalResources folder (see Mikhail’s blog post on how to deploy your resource files using a SharePoint feature).
- I then added the following code to my page layout which changed the current ASP.Net threads UI culture to the same culture of the SharePoint site collection:
protected override void InitializeCulture()
System.Threading.Thread.CurrentThread.CurrentUICulture = Microsoft.SharePoint.SPContext.Current.Web.Locale;
This made sure that each time the page loads the ASP.Net culture was set to the correct site collection culture.
Now that the correct ASP.Net culture is selected in the page we can go ahead and localize our strings in the page layout like this:
<asp:Literal runat=”server” Text=”<%$Resources:MyResourceFile,MyLocalizedStringKey %>“ />
Note that we can also just use “<%$Resources:MyResourceFile,MyLocalizedStringKey%>” in any ASP.Net web controls property if you don’t want to use a Literal control.
And that’s all there is to it, I think it’s a clean and simple way to localize the SharePoint UI without using language packs and localizing the whole site collection including admin and system pages.