Public Strongly Typed Resources With PublicResXFileCodeGenerator

One of the new localization features in Visual Studio 2008 is the PublicResXFileCodeGenerator. In both Visual Studio 2005 and 2008 you can create a resource file (e.g. ExceptionResources.resx) and Visual Studio will associate a .Designer.cs file (e.g. ExceptionResources.Designer.cs) with it. The .Designer.cs file contains a strongly typed resource class that allows you to refer to the resource entries as properties of the generated class (e.g. ExceptionResources.CustomExceptionMessage). This system works well but the class and its properties are marked as internal and consequently they can only be accessed from within the same assembly. Visual Studio 2008 allows these classes and their properties to be public. By default the resources are still internal and you can see this by selecting the .resx file in Solution Explorer and then bringing up the Properties Window. You will see a property called Custom Tool and it will be set to ResXFileCodeGenerator. This is the code generator that uses the .NET Framework's StronglyTypedResourceBuilder class to generate the code for the .Designer.cs file. The StronglyTypedResourceBuilder (and its associated command line tool ResGen) both accept a parameter to generate public resources but the ResXFileCodeGenerator sets this value to false and so the resources are internal. Visual Studio 2008, however, introduces a new generator called a PublicResXFileCodeGenerator and this generator sets this value to true. To generate public strongly typed resource classes instead of internal ones set the Custom Tool to PublicResXFileCodeGenerator.

Alternatively you can set the Access Modifier drop down in the Resource Editor to Public to achieve the same result (there are three values: Internal, Public and No code generation). It is worth knowing that this Visual Studio 2008 feature is simply a design time feature and the generated code is still .NET Framework 2.0 compatible.

Of course, it is also possible to write your own replacement for the ResXFileCodeGenerator and PublicResXFileCodeGenerator and you can download an example of this at http://www.dotneti18n.com/downloads.aspx.

Technorati Tags: Localization, Internationalization, ResXFileCodeGenerator, PublicResXFileCodeGenerator, Strongly Typed Resource Classes, resx

Currently rated 4.4 by 7 people

  • Currently 4.428571/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments