SharePoint Internals: Resources

In a country where English is not the native tongue, localisation can be pretty important. Localisation within SharePoint is achieved by using resources and resource files. Although the use of resources is not mandatory, it’s usually good practise to use them anyway. You don’t want to hard code strings in your application, and, moreover, you never know when your application should be localized. Setting up and using these resources in SharePoint can be quite confusing. So here is a little article covering this topic.

Resources

Resources – in this case: strings – are contained within XML based .resx files. Every resource in such a file is identified by a fixed name. (quite like a HashTable) Here is a little example.

<root>
<data name="FieldManagerPageDescription">
<value>Manage the field of this application.</value>
</data>
</root>

For every new localization, you need a new .resx file with the same names as keys. You can just copy the original .resx file to achieve this quickly. In this new resource file you translate the original values within the value tag. The new resource file has to be named as follows: <original_name>.<culture>.resx.
eg. – myresource.resx
- myresource.en-US.resx
- myresource.fr-FR.resx

SharePoint & Resources

First thing you need to know, SharePoint defines two kinds of Resource files: Application resources and Provisioning resources. Application resources are resources used within the normal execution of the SharePoint application. Normal SharePoint execution include: Application Pages, Web Parts and Controls. SharePoint also makes a difference between application resources used in normal web applications and application resources used in the central administration. Don’t forget that. Provisioning resources, on the other hand, are used when provisioning elements, so you have to use them within features, site definitions and list definitions. Ok, now let’s see the practical side of it: deployment and usage.

1. Deployment

Resource files in SharePoint are located in different folders. Here is a list:

  • C:\Inetpub\wwwroot\wss\VirtualDirectories\<port>\App_GlobalResources\
  • <hive>\12\Resources\
  • <hive>\12\CONFIG\Resources\
  • <hive>\12\CONFIG\AdminResources\
  • <hive>\12\TEMPLATE\FEATURES\<feature>\Resources\

So, how do you know where to put your resource files? Well, every type of resource has its own folders.

Provisioning resources

  • <hive>\12\TEMPLATE\FEATURES\<feature>\Resources\Resources.<culture>.resx
  • <hive>\12\TEMPLATE\FEATURES\<feature>\Resources\
  • <hive>\12\Resources\

Every feature uses the resources file located in its Resources folder. You can however use another resource file or even share resources. To share resource files you have to put them in the 12\Resources\ folder. Site definitions and list definitions also get their resources from this folder.

Application resources

  • <hive>\12\CONFIG\Resources\
  • C:\Inetpub\wwwroot\wss\VirtualDirectories\<port>\App_GlobalResources\

Application resources are located in CONFIG\Resources folder. For a web application to use those resources, they have to be copied to their App_GlobalResources folder. (each web application has its own Global Resources folder) How is this done? At creation of the web application, the resources are initially copied to the App_GlobalResources folder. When adding new resources to the CONFIG\Resources folder, the resources have to be copied to existing web applications. You can do this manually or use the STSADM command: copyappbincontent.

Application resources: admin

  • <hive>\12\CONFIG\AdminResources\
  • C:\Inetpub\wwwroot\wss\VirtualDirectories\<port>\App_GlobalResources\

Application resources for the central administration work the same way as normal application resources, except that the base folder is CONFIG\AdminResources.

2. Usage

This last part will focus on how to use resources within SharePoint elements. Luckily it doesn’t really matter which kind of resource you are using. Here are the different ways:

In C#:
HttpContext.GetGlobalResourceObject("MyResource", "MyName").ToString();

In ASPX properties:
<%$Resources:MyResource, MyName%>

In ASPX as text:
<asp:literal runat="server" Text="<%$Resources:MyResource, MyName%>" />

In XML:
$Resources:MyResource, MyName

In XML features, using the default resource file:
$Resources:MyName

3. Conclusion

There you go. Everything you will ever want to know about resources in SharePoint.

Part of this article is derived from the excellent article of Mikhail Dikov. You can consider this article as some sort of extension of his article. Be sure to read it. Also, I’d like to thank Tom Verhelst for the heads up on the copyappbincontent. Thanks man!

Have a great week!

15 Responses to “SharePoint Internals: Resources”

  1. Koryhy Says:

    omg.. good work, brother

  2. Sajjad Says:

    Very good.
    But I still have problem using App_GlobalResources.
    I copy all files in this folder and paste them there and rename them to the right format of my culture.
    then translate contents of files to my language.
    then reset iis and reload page. but the page still read data from us-EN culture and nothing change.
    I change the web.config file and web browser’s language, but nothing happens.
    how can I read from my language resource files in SharePoint server 2007 web sites?
    Thank you…

  3. Hilary Says:

    realy great, thanks alot!!

  4. Tombo Says:

    Sajjad>
    Try installing the language pack for your culture of SharePoint (You can find them on the microsoft site). Now create a new site with this language pack. It will now use the correct language.

    Another way is to change the web’s culture internally through the object model!

    Good luck!

  5. sajjad Says:

    Language pack is not implement for my language.
    I want to translate MOSS 2007.
    I translate resource files. But menu toolbar text not change.
    Installation of other language pack don’t change anything.
    But menu texts are correctly translated and view in that language.
    Do you know how to translate all menu and context menu?

    Can you help me?

  6. rlasker Says:

    Thanks for this article. The information out there is so piece meal so it is nice to see everything in a single context.

  7. Jeroen Ritmeijer Says:

    Rather than calling the STSADM command to copy the resources during deployment, automate this by including the following in your FeatureActivated event in your feature receiver.

    For Central Administration resources (and sitemaps): SPWebService.AdministrationService.ApplyApplicationContentToLocalServer();

    For ‘regular’ Application Page resources (and sitemaps):

    SPFarm.Local.Services.GetValue().ApplyApplicationContentToLocalServer();

  8. Nimi Says:

    Hi,
    I just wanna localize my entire site not one webpart. and that too depending upon the user who access. Depending upon the user’s sulture i have to localizze accroding to that culture. Is it possbile to go around with out resource file. Can i override the existing regional settings of the user based on the current user culture ???

  9. arch Says:

    i have the default, en-US and fr-FR resource files. My webparts and layouts seem to pick up the en-US resources but when i change my lcal to fr-FR nothing happens. The site still keeps pickng the data from en-US. I tried deleting the en-US and then it reverts to the default one. If i delete that too, it gives me error that the resource was not found.

    Any ideas?

    Thanks -
    arch

  10. arch Says:

    ok nvm, my buddy just found out the solution – change the locale on the preRequest handler NOT the BeginRequest.

  11. Using resource files in your SharePoint customisations « Brian Farnhill Says:

    [...] first of all, I have to credit Mikhail Dikov and Tom for helping get this all figured out. You should really go check out their blog posts there, as I [...]

  12. SharePoint Internals: Resources « Trungpv's Blog Says:

    [...] SharePoint Internals: Resources Filed under: SharePoint, SharePoint 2010 — trungpv @ 7:35 am Ref: http://tomblog.insomniacminds.com/2008/02/25/sharepoint-internals-resources/ [...]

  13. Balaji Says:

    Hi,

    I am having the silverlight web part and need to localize the content insider the silverlight web part. How to implement this logic.

  14. 123 SharePoint » Blog Archive » Resource Files Deployment Says:

    [...] Resource files in SharePoint have to be placed at different places in the file system of the server. For more information check out this excellent description: http://tomblog.insomniacminds.com/2008/02/25/sharepoint-internals-resources/ [...]

  15. SharePoint and Resources | Sharepoint 2012 Says:

    [...] http://tomblog.insomniacminds.com/2008/02/25/sharepoint-internals-resources/ This entry was posted in Uncategorized.If you require a short url to link to this article, please use http://lncn.eu/twy3. [...]

Leave a Reply