Create a custom $Resources file .resx for SharePoint.
Posted by PANVEGA on November 12, 2007
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 – 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.
<value>Manage the field of this application.</value>
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
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.
Resource files in SharePoint are located in different folders. Here is a list:
So, how do you know where to put your resource files? Well, every type of resource has its own folders.
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 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
Application resources for the central administration work the same way as normal application resources, except that the base folder is CONFIG\AdminResources.
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 ASPX properties:
In ASPX as text:
<asp:literal runat="server" Text="<%$Resources:MyResource, MyName%>" />
In XML features, using the default resource file:
Here is a short eaxample how to develope your own .resx file in Visual studio and referenecs the values in a feature and elements.xml file:
Add your custom Keys and valus in the resource file:
default english file:
German translation for the values:
copy the files into <hive>\12\TEMPLATE\FEATURES\<feature>\Resources\
insert the $Resources into your field with the appropriate name
<ElementManifest Location=“elements.xml“ />
<?xml version=“1.0“ encoding=“utf-8“?>