Chapter 12. Custom Resource Managers


SO FAR, ALL OF THE EXAMPLES THAT YOU have seen have used one of the two resource managers that are included with the .NET Framework 1.1 and 2.0namely, System.Resources.ResourceManager and System.ComponentModel.ComponentResourceManager. In this chapter, I show how you can write your own resource managers. You might want to do this to read/write resources from a different format (e.g., a database) or to provide additional functionality to a resource manager. I start by explaining how the "file-based" resource manager embedded in the ResourceManager class works. This feature has been largely ignored until now, as its implementation is somewhat flawed. However, the basic idea is sound, and knowledge of what it does helps us to understand how the ResourceManager class works. It also paves the way for writing a replacement resource manager.

I go on to explain how the ResourceManager works internally, as this will help us to write our own resource managers. Our first custom resource manager reads resources from a database. This is a relatively simple and useful resource manager that we cover in two stages: reading resources from a database and, later, writing resources to a database. We follow this with two related resource managers to read/write file-based resources in .resources and .resx files. The former offers the same functionality as the file-based resource manager hidden in ResourceManager, but without the limitations. With these resource managers in place, we write a "translation" resource manager. This resource manager uses the translation engine that we wrote in Chapter 9, "Machine Translation," to translate missing resources on the fly. This resource manager is particularly useful for resources that frequently change where it is impractical to return to the development team for new releases whenever content changes. The last resource manager example is a resource manager to set properties such as Font, ImeMode, and RightToLeft on an application-wide basis.

Our job does not end with the writing of the resource managers, though. I introduce a resource manager provider class that does for resource managers what the DbProviderFactories class does for data providers in the .NET Framework 2.0. The last piece of the jigsaw is to use the custom resource manager. Both Windows Forms developers and ASP.NET developers have separate additional steps that must be taken to successfully use a custom resource manager. Windows Forms developers have the problem that the code generated in a form's InitializeComponent method includes a hard-coded reference to the ResourceManager or ComponentResourceManager classes. I show how to create a component to overcome this problem. ASP.NET developers must use ASP.NET's resource provider mechanism to get ASP.NET to adopt the new custom resource manager. Finally, we return to the subject of strongly typed resources and show how to generate strongly typed resource classes for our new resource managers.

But before we begin on our journey, a word of warning: If performance is an issue to you, you might want to stay with the ResourceManager or Component-ResourceManager classes. These classes outperform all of the solutions in this chapter, and it is unlikely that other solutions will offer better performance than the existing resource managers already do. Of course, they don't offer the same functionality as custom resource managers, so you need to weigh the benefits.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net