Geeks With Blogs
Brian Schroer Don't Call Me Mort!
Technorati Tags: ,,

 

I have several related projects that need the same localized text, so my global resources files are in a shared assembly that’s referenced by each of those projects.

It took an embarrassingly long time to figure out how to have my .resx files generate “public” properties instead of “internal” so I could have a shared resources assembly (apparently it was pretty tricky pre-VS2008, and my “googling” bogged me down some out-of-date instructions). It’s easy though – Just change the “Custom Tool” to “PublicResXFileCodeGenerator”:

resources2  

…which can be done via the “Access Modifier” dropdown of the resource file designer window:

resources1 (1)

 

A reference to my shared resources DLL gives me the ability to use the resources in code, but by default, the ASP.NET resource expression syntax:

<asp:Button ID="BeerButton" runat="server" Text="<%$ Resources:MyResources, Beer %>" />
 

…assumes that your resources are in your web site project.

 

To make resource expressions work with my shared resources assembly, I added two classes to the resources assembly:

1) a custom IResourceProvider implementation:

 
   1:  using System;
   2:  using System.Web.Compilation;
   3:  using System.Globalization;
   4:   
   5:  namespace DuffBeer
   6:  {
   7:      public class CustomResourceProvider : IResourceProvider
   8:      {
   9:          public object GetObject(string resourceKey, CultureInfo culture)
  10:          {
  11:              return MyResources.ResourceManager.GetObject(resourceKey, culture);
  12:          }
  13:   
  14:          public System.Resources.IResourceReader ResourceReader
  15:          {
  16:              get { throw new NotSupportedException(); }
  17:          }
  18:      }
  19:  }

 

2) and a custom factory class inheriting from the ResourceProviderFactory base class:

 

   1:  using System;
   2:  using System.Web.Compilation;
   3:   
   4:  namespace DuffBeer
   5:  {
   6:      public class CustomResourceProviderFactory : ResourceProviderFactory
   7:      {
   8:          public override IResourceProvider CreateGlobalResourceProvider(string classKey)
   9:          {
  10:               return new CustomResourceProvider();
  11:          }
  12:   
  13:          public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
  14:          {
  15:              throw new NotSupportedException(String.Format(
  16:                  "{0} does not support local resources.", 
  17:                  this.GetType().Name));
  18:          }
  19:      }
  20:  }

 

In the “system.web / globalization” section of my web.config file, I point the “resourceProviderFactoryType" property to my custom factory:

 
<system.web>
    <globalization culture="auto:en-US" uiCulture="auto:en-US"
    resourceProviderFactoryType="DuffBeer.CustomResourceProviderFactory, DuffBeer" />

 

This simple approach met my needs for these projects , but if you want to create reusable resource provider and factory classes that allow you to specify the assembly in the resource expression, the instructions are here.

Posted on Saturday, June 5, 2010 5:15 PM | Back to top


Comments on this post: ASP.NET Localization: Enabling resource expressions with an external resource assembly

# re: ASP.NET Localization: Enabling resource expressions with an external resource assembly
Requesting Gravatar...
Thank you for sharing this. This has saved me so much time!
Left by Alice on Jul 28, 2010 5:17 PM

# re: ASP.NET Localization: Enabling resource expressions with an external resource assembly
Requesting Gravatar...
Hi. I did an external resource assembly wich I can access from code behind without problems. But I'd like to be able to access to it from aspx page too.
I tried your approach but no success...
What If you hace more than one resx file??
this MyResources.ResourceManager.GetObject would not work.

Could you send me more info about it??

Thanks in advanced.
Left by Alejandro on Mar 03, 2011 7:06 AM

# re: ASP.NET Localization: Enabling resource expressions with an external resource assembly
Requesting Gravatar...
Hello

Is there some easy way to make this work in MVC 3 as well? using [Required attributes etc?

/Lasse
Left by Lasse Edsvik on Jun 01, 2011 9:49 AM

Your comment:
 (will show your gravatar)


Copyright © Brian Schroer | Powered by: GeeksWithBlogs.net