Local and Global Resources for an asp.net 3.5 web application (Part 1)

No.of Views2733
Bookmarked1 times
Downloads 
Votes0
By  oudinia   On  16 Feb 2010 03:02:41
Tag : ASP.NET , How to
Local and Global Resources for an asp.net 3.5 web application (Part 1)
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 


Introduction

in asp.net, starting from version 2.0, for each application we have two ways of storing resources for globalization of applications :

1. Global Resources folder :

in this folder, we store application wide localization elements. For example, localization of error and confirmation messages. localization of html text.

2. Local Resources folder :

this folder stores page and user control specific resource files, which store language specific property values, inlcuding text, ui properties, images and more.

3. Finally, as one requirements in some applications and websites, changing the culture dynamically by clicking on an image button. For this effect, on of the solutions, is to override the page function InitializeCulture(), then we use the session object, to store this preference.

This "InitializeCultre()" technique, is suitable for applications which require no profile based localization.

following will be some screen shots of the folder structure, and some code. Attached to this article, is the visual studio website, containing complete code.


For The global resources folder, here's how we create it :

right click on the project, and select add asp.net folder, then App_AppGlobalResources :


this will add the folder, with add an empby App_GlobalResources folder

Image Loading

next we add a resource file (.resex extension) to this folder

Image Loading

The global resource files are accessbile to all objects application wide.


For The local resources folder, here's how to create it :

Local resources are file specific, and can be either created, or generated. They must be in the root of the folder containing the file as follows :

Image Loading

as you may notice in the images, we have, to App_LocalResources Folder, one is for the root folder (especially for Default.aspx), and the other is for the controls folder (specifically for PersonnalInformation.ascx)

Here's how to generate a resource file for an ascx or aspx file (in this case, we'll do it for the DataEntryControls Folder) :

1. open the SimpleTextBox.ascx in design mode

 

Image Loading

2. Select Tools >> Generate Local Resource

Image Loading

3. the folder and it's corresonding file will be generated under the DataEntryControls Folder

Image Loading

What's particular about the file generated is that it's generated with main properties already in created for you in the file, ready to be filled.

Here's what it looks like in Design View :

Image Loading

Finally on resource files, they are very easily editable from xml code. all you do is :

right-click on the file >> Open With >> XML Editor :

Image Loading

This is what the file looks like

Image Loading

Scroll down to the bottom, until you reach the beginning for "

Image Loading

that's where you can add, delete or edit resource elements for the file

This might be useful for those who wish to generate the support for resources from database or so.

Image Loading

That will do it for global and local resource files.

Conclusion

In an asp.net project, It is really better to differentiate the use of local and global resources. One bad practice, is to use global resources for all reource for both file specific resources and application specific resources. this can lead to huge, unmanageable resex files for each language.

In part 2, we will look at resources files in more detail, looking at the code to access these files, as well as how we can store resources about images, and what are the best practices for those kind of file types.

Sample Project Source

Download source files -19 kb

 
Sign Up to vote for this article
 
About Author
 
oudinia
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-13 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top