Silverlight And Strongly Typed Resource Classes

If you have tried to localize Silverlight applications using .resx files you will probably have noticed that strongly typed resource classes don't work straight out of the box. This post describes how you can overcome this.

New in Visual Studio 2008 is the PublicResXFileCodeGenerator, a code generator for creating public strongly typed resource classes. This code generator does indeed generate public classes with public properties and for all applications except Silverlight this is exactly what is wanted of a public strongly typed resource class. But for Silverlight applications it is not enough. The problem is that the constructor in the generated code is still internal. Normally this doesn't make any difference because nothing ever constructs a strongly typed resource class directly; only the static properties are used. But the constructor is used in a Silverlight application and that's where the internal constructor is unhelpful. The manual solution to this problem is to edit the generated code and this is a very undesirable solution because you have to continually re-edit the generated code every time it is re-generated. A more permanent solution is needed and it is available in the form of the PublicResourceCodeGenerator. I have written a number of code generators for strongly typed resource classes that solve various problems. You can download the code generators here and the source code is included in the source code for my .NET Internationalization book here. Here is the current list of code generators for strongly typed resource classes:-

Code Generator Class Description
ResourceCodeGenerator Generates the exact same code as the ResXFileCodeGenerator and offers no additional benefit.
PublicResourceCodeGenerator Generates almost the same code as the PublicResXFileCodeGenerator except that the constructor is public instead of internal.
ResourceProviderCodeGenerator Generates the same code as the ResXFileCodeGenerator except that it uses a provider model instead of the ResourceManager.
PublicResourceProviderCodeGenerator Generates the same code as the PublicResXFileCodeGenerator except that it uses a provider model instead of the ResourceManager and the constructor is public.
GlobalResourceProviderProxyGenerator Generates the same code as the GlobalResXFileProxyGenerator (used for ASP.NET global resources) except that it uses a provider model instead of the ResourceManager.

So to generate strongly typed resource classes that can be used in Silverlight applications select the .resx file in Solution Explorer and then in the Properties Window set the Custom Tool to PublicResourceCodeGenerator.

Currently rated 4.2 by 9 people

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

Posted by: GuySmithFerrier
Posted on: Saturday, September 27, 2008 at 8:10 AM
Tags: , ,
Categories: Internationalization
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed

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