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
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.