<$BlogRSDURL$>
My Blog Links
Site Links
Currently Reading
Blogroll
Archives
Tools
  • This page is powered by Blogger. Isn't yours?
  • Weblog Commenting and Trackback by HaloScan.com
  • 2RSS.com :: RSS directory
  • Listed on BlogShares
  • Blog Directory, Find A Blog, Submit A Blog, Search For The Best Blogs

Tuesday, August 03, 2004

.NET: Loading Resource Strings in a ClassLibrary 

I just started working on a class library and one of the first things that came up was using a string resource from that assembly rather than a hard-coded string. It's always a good idea to start new projects with localization in mind, that way you don't have to go back and retroactively do it. To that end, I developed a little helper class that lets you find the resources in a class library.

using System;
using System.Resources;
using System.Globalization;

namespace MyClassLibrary
{
/// <summary>
/// Manages localized string resources for the DesignSurface.
/// </summary>
internal sealed class ResourceStringManager
{
/// <summary>
/// Constructor - for the singleton pattern it's made private.
/// </summary>
private ResourceStringManager()
{
}

static ResourceManager instance = null;

/// <summary>
/// Singleton ResourceManager for the class library assembly.
/// </summary>
internal static ResourceManager Instance
{
get
{
if (instance == null)
{
lock (typeof(ResourceStringManager))
{
if (instance == null)
{
instance = new ResourceManager("MyClassLibrary.Strings",
typeof(ResourceStringManager).Assembly);
}
}
}

return instance;
}
}

/// <summary>
/// Convenience method for retrieving a string from the Resource Manager
/// without having to specify the CultureInfo.
/// </summary>
/// <param name="id">String id to retrieve.</param>
/// <returns>String in resource.</returns>
internal static string GetString(string id)
{
return Instance.GetString(id, CultureInfo.CurrentUICulture);
}
}
}

The code is pretty straight-forward. It creates an instance of a ResourceManager based on the name of the .resource section in the assembly and the type of this class, which it uses to locate the assembly. Although this particular class is focused on retrieving strings from the resource file, you can actually retrieve any type of resource: strings, icons, bitmaps, etc. The other interesting attribute of this class is that it's implemented as a Singleton pattern.

You can use this class by calling:
string displayText = ResourceStringManager.Instance.GetString("MyResourceString"); 


You need to be sure that you've added a Strings.resx file to your project, added a resource string with the specified name ("MyResourceString"), and that you've done a full rebuild on the class library project. The full rebuild is sometimes needed because I've run into a couple of instances when just doing a build doesn't build in the resource file, if the project had been previously built without any resources in it.

I have used similar resource classes in the past, but since I just ran into this requirement again, I thought it would be a good topic to blog about.

Comments: Post a Comment