Adrian Hara

Working through the .NET maze

  Home  |   Contact  |   Syndication    |   Login
  42 Posts | 0 Stories | 83 Comments | 10 Trackbacks

News

Archives

Post Categories

A colleague of mine asked me today if there is a better way to set the CurrentUICulture in asp.net 2.0 than handling  Application_BeginRequest and doing Thread.CurrentUICulture = new CultureInfo(foo). He asked me this because he was doing something like CommonStrings.Culture = new CultureInfo(whatever), where CommonStrings is a .resx file residing in our App_GlobalResources folder. As you might know, asp.net 2.0 generates a wrapper class around string resources for strongly typed access, so instead of doing ResourceManager.GetString(“SomeKey“) you do Strings.SomeKey. The actual implementation of the wrapper properties is this:

public static string SomeKey
{
      get
      {
            return CommonStrings.ResourceManager.GetString("SomeKey", CommonStrings.resourceCulture);
      }
}

I had no idea what the Culture property of the generated CommonStrings class did, so I used Reflector to look it up. Here's the implementation:

[EditorBrowsable(EditorBrowsableState.Advanced)]
public static CultureInfo Culture
{
      get
      {
            return CommonStrings.resourceCulture;
      }
      set
      {
            CommonStrings.resourceCulture = value;
      }
}
... where resource culture is:
private static CultureInfo resourceCulture;
 
Now that “static” modifier had me raising an eyebrow. As far as I know, declaring such
a variable static means it's static for everybody, not per-context or per-request 
static. That means that once I set the generated wrapper class' Culture property, it 
“stays” set for all future users, meaning that, for example, if one user changes his
language to German, and the codebehind implements this using the above mechanism, all 
other users will get the German interface as well. Wondering if I might be missing 
something, I created a small test application and sure enough, the behaviour 
is what i suspected.
 
So, as far as I can see, the Culture property and other properties' implementation,
which calls ResourceManager.GetString(“key“, resourceCulture) is pretty useless.
Why is the generated resources wrapper class even implemented this way? I
cannot see any realistic scenario where this implementation would be of use, given
that the resourceCulture private variable is declared as static. Am I missing something 
really big?
 
ps: if you're not touching the generated wrapper class' Culture property and setting the
Thread.CurrentUICulture instead, the strings will be retrieved correctly because, as you
can see in the example property's implementation above, ResourceManager.GetString is 
called with the second parameter null and the GetString implementation does:
      if (culture == null)
      {
            culture = CultureInfo.CurrentUICulture;
      }
posted on Tuesday, May 09, 2006 1:25 PM

Feedback

# re: Global Resources weirdness 3/29/2007 11:25 PM Samir
You need resourceCulture in case you want to select a specific culture yourself, not the thread culture

# re: Global Resources weirdness 3/30/2007 8:44 AM Adrian Hara
Ok, but if you do set that Culture (and underlying resourceCulture property), then, since it's static, all other users of the application will get the resources for the same culture you selected, which is wrong. Unless i'm missing something here...

# re: Global Resources weirdness 11/26/2008 11:46 PM Dries
It's added in case you want to override the current thread culture. If you leave it to null it will take the CurrentUICulture. The magic sit's in the ResourceManager.GetObject(string, CultureInfo, bool)

Regards
Dries

# re: Global Resources weirdness 11/27/2008 1:01 AM Dries
Sorry didn't read the complete post. Whe the ResourceManager.GetString(“key“, resourceCulture) exists, I can understand. When you always want to show a resource in a certain language you need the be able to specify a culture.

But why they have a static culture is a mystery to me too. I guess that in an ASP.Net environment this is pretty useless. Except when you create resource files within a winform or other type of applications. With only one user, setting it as a static can be argued.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: