Friday, January 13, 2012

Localizing Sharepoint using Resource files

Share/Save/Bookmark
Resource files on a high level can be stored either under 14 hive or at {webappRoot}\app_globalResources


Shared Resource:

How To Create?

Create a mapped folder in VS to deploy the shared or localized resource file to a specific resources folder under 14 hive.

How To Use?

Feature.xml :- Resource(shared) file located at 14/Resources can be accessed in feature xml by setting the DefaultResourceFile property. Now feature will look for the resource file at 14/Resources but not under 14/template/features/featureName/resources(which is default behavior)

Code:- Programmatically same resource file can be accessed using

SPUtility.GetLocalizedString(Key, ResourceFileName, (uint)CultureInfo.CurrentUICulture.LCID);


Global Resource:

How To Create?

To have a Resource(global) file that will be located at {webappRoot}\app_globalResources
We can use a global resource template in VS2010. If its missing add an empty item to VSproject and under that add a resource file or which the deployment path will be set to {webappRoot}\app_globalResources

How To Use?

Feature.xml :- Resource(shared) file located at 14/Resources can be accessed in feature xml by setting the DefaultResourceFile property. By default feature will look for the resource file at 14/Resources but not under 14/template/features/featureName/resources

Code:- GlobalResources can be accessed using
HttpContext.GetGlobalResourceObject(“ResourceName”,”key”,CultureInfo.CurrentUICulture)

Markup :- <%$Resources:ResourceFilename,StringKey%>


Gotchas:

If aspx files(markup) are not picking up the resx files based on the culture but just the fallback(default) resource file, then you might need to check the following :

- Set UICulture=”auto” for the Page directive

- Add the below tag to web.config

<globalization uiCulture="auto" enableClientBasedCulture=”true” culture="auto" />

I’ll try to get a new post for the localized resources that reside under app_localresources(for .aspx files)




Subscribe

No comments:

Post a Comment