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

Related posts

Comments

mtaulty.com

Tuesday, November 04, 2008 6:35 AM

pingback

Pingback from mtaulty.com

Mike Taulty's Blog : Silverlight 2 and Localisation/Globalisation

emil.silverlightshow.net

Thursday, August 13, 2009 1:18 PM

pingback

Pingback from emil.silverlightshow.net

Code generation for Resources in Silverlight (and how to get rid of the internal constructor) « Emil Stoychev's Blog