Localization in ASP.NET 2.0 and Visual Studio .NET 2005 : Application Wide Resources & Code Changes
page 1 of 1
Published: 21 Apr 2004
Unedited - Community Contributed
Abstract
This article will cover some more of the Visual Studio 2005 enhancements for making Resources easier to include in your project, as well as showing the various ways that Resources can be included through code within your applications.
by Phil Winstanley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 16816/ 26

Part 2 : Application Wide Resources & Code Changes
Localization in ASP.NET 2.0 and Visual Studio .NET 2005
 
This topic is pre-release documentation and is subject to change in future releases.
Tested against Visual Studio .NET 2005, v8.0.40301.9 and the .NET Framework v2.0.40301
 
Part 2 : Application Wide Resources & Code Changes
 
In the previous article of this series, Code Free Resources, the Visual Studio .NET 2005 features for easily Localizing a web site were explored without using any code.
 
This article will cover some more of the Visual Studio 2005 enhancements for making Resources easier to include in your project, as well as showing the various ways that Resources can be included through code within your applications.
 
In Visual Studio 2005 Web Site's there is the concept of a "Resources" folder living on the root of your web site. In this folder you can place, surprisingly enough, resources. Unlike the Resources discussed in the Previous Article, any Resources placed in the Resources folder have scope across the whole Application, in that they can be accessed from any Web Form or Code, whereas the Resources stored in "LocalResources" folders are only associated with a specific .aspx.
 
Visual Studio 2005 not only supports the use of the Resources folder, it's embraced and encouraged, this is done by offering Developers Strongly Typed access right in to their Resource files through the Resources Namespace, for example if there is a .resx in the Resources folder called "Bob" and within Bob.resx there is a Resource String called "Alice" with a value of Cake, in your .aspx all you need to do to access the Resource String "Alice" (with full intellisense of course) is type: -
 
string AlicesFavFood = Resources.Bob.Alice.ToString();
 
Oh, ResGen is Dead, RIP ResGen.
 
Now you've picked yourself up off the floor after that moment of enthusiastic bouncing let's continue.
 
The above happens without the need to recompile, all that you the developer needs to do, is Save the .resx file and you instantly get all the Intellisense within your application.
 
Intellisense on your Resources is really usful, especially if you're no the person that put together the Resource files, this allows you to quickly get hold of the right Resources within your applications. However. And it's a Big However. To me, it's very rare that I will know the name a of Resource for my controls before I come to actually write the controls them selves, this leaves us in somewhat of a Chicken and Egg situation.
 
I see this functionality being useful mainly where you have General resources, such as you might have on an e-commerce site, strings such as "Price", "Shopping Basket" and "Checkout" might be strings you regularly use in many different e-commerce applications, I see having a Resource file that you drop in to each of your projects with General Resources being used with the Intellisense, otherwise I think it may be an otherwise unused piece of Visual Studio 2005 functionality.
 
Accessing resources in code can be achieved in a variety of ways, mainly depending on the source of the Resource. Along with the meta Resources discussed in the previous article it's also possible to explicitly include Text from Resource Files stored in the LocalResources folder as so: -
 
<asp:Label ID="CodeTest" Runat="server" Text="<%$ Resources:LabelResource1.Text %>"></asp:Label>
 
Or in Code Beside/Behind you can access the LocalResources of a page by using the GetPageResourceObject() method of the Page.
 
string LocalResource = (string)GetPageResourceObject("LabelResource1.Text");
 
It's also possible to Localize static content, i.e. just basic strings that don't need to have Server Side access.
 
<localize runat="server" meta:resourcekey="MyResourceKey">Some Default Content</localize>
 
Using Application level resources is possible through the Resources member as shown above, but it can also be accessed programatically in pages via the GetAppResourceObject() method, as so: -
 
string ApplicationResource = (string)GetAppResourceObject("Bob", "Alice");
 
As well as the Programatic access, it's possible to retreive the Application level Resources declaritavly like this: -
 
<asp:label runat="server" id="foo" text="<%$ Resources:Bob, Alice %>" />
 
So there you have some more of the new features in ASP.NET 2.0, everything about Localization in ASP.NET 2.0 is so much easier. The next articles in this series will discuss .Resource files in ASP.NET 2.0 and Database stored Resources.


User Comments

Title: great tips   
Name: kok hong
Date: 2009-01-19 11:28:51 AM
Comment:
hi, i'm new with asp.net and this is a great post. Thanks for sharing.
Title: Amazing tool of knowledge in Localization in .NET 2.0   
Name: Adnan Khalid
Date: 2006-08-28 5:13:58 AM
Comment:
This article really helped the new developer in .NET 2.0 in me, who was surprised enough with the capabilities .resx files provide already. Inspite of working deep, i hadn realised the various API's and strict access to .resx file from anywhere in the application is so possibly easier here in ASP.NET 2.0 !

Amazing tip Phil...
Title: Cool   
Name: Kate
Date: 2006-05-05 9:18:24 AM
Comment:
Cool... That was the exact thing .. that I've searched :)
Title: well   
Name: prashanth
Date: 2006-03-23 5:10:41 AM
Comment:
I want to have a clear description about having resource file
Title: Thanks   
Name: Hermitess
Date: 2006-02-09 11:44:15 AM
Comment:
This was the just concise answer I was looking for. Good tip.
Title: Thank you   
Name: Liam Turner
Date: 2006-01-05 8:29:54 AM
Comment:
i was trying to force the asp.net 2.0 with resgen to work and getting rather red in the face! i have seen the light and finished the job and am back searching for another fix!

Liam

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-08-21 7:40:33 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search