Using Resource files with SharePoint

This post will explain how resource files can be used while creating visual web parts for SharePoint.
For this i will create a project which will have a drop down with two languages English and French.
It will also have a text box and label associated to it which asks for a name.
When i select a language from the drop down, the text in the label will change according to the language selected.

I will start off with creating a visual web part using Visual Studio 2010. The design view is shown below


Then i will add two new resource files to this project, English.resx and French.resx


Then i will add the key value pairs to the resource files. I will name the key as lblName and the value of it will be Name in the English resource file. In the French resource file, the key will remain the same lblName, but the value will be the french translation for name i. e Nom

This is how the English.resx resource file looks like

And this is how French.resx looks like

I will handle the selected index changed event for the drop down. Here i will write the logic to get the value of a particular key from a resource file. Which resource file to get the value from is decided from the selected language from the drop down. I will assign the value that is retrieved to the text property of label lblName


The function GetGlobalResourceObject(reourcefilename,key) will accpet two arguments.
First is the name of the resource file. Second is the key whose value is to be retrieved.

But for this to work, your resource files should be there in the App_GlobalResources folder of your virtual directory. So open the project in windows explorer. Copy both the resource files and paste them in the App_GlobalResources of your SharePoint web application.

Deploy your solution and add the web part to a page in your SharePoint site and try yourself. Now you know how simple it is to use resource files and apply variations in your SharePoint sites.

Comments

Popular posts from this blog

Create web part pages with custom layouts in SharePoint 2010

Create a Custom Timer Job in SharePoint 2010

SharePoint list forms display HTML instead of rendering