HowTo use the resource files from SharePoint

How about using the available resources from SharePoint to translate some basic words and sentences? Well, it is quite easy to use the available resources. You need the Microsoft.SharePoint.Intl.dll and some lines of code to use the already translated resources:

// define yourself variables
private readonly CultureInfo _Cult;
private readonly Assembly _SharePoint_Intl_Assembly;
private readonly ResourceManager _SharePoint_RM;
private readonly ResourceManager _SharePoint_WebPartPage_RM;
 
// initialize them in your constructor
_SharePoint_Intl_Assembly = Assembly.Load("Microsoft.SharePoint.intl, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
_SharePoint_RM = new ResourceManager("Microsoft.SharePoint", _SharePoint_Intl_Assembly);
_SharePoint_WebPartPage_RM = new ResourceManager("Microsoft.SharePoint.WebPartPages.strings", _SharePoint_Intl_Assembly);
 
// get the current culture
internal static CultureInfo GetCultureInfo()
{
	CultureInfo newCultureInfo;
	try
	{
		uint localeID = SPContext.Current.RegionalSettings.LocaleId;
		newCultureInfo = new CultureInfo((int)localeID);
	}
	catch
	{
		newCultureInfo = new CultureInfo(1033);
	}
	return newCultureInfo;
}
 
// use the resources
string test = _SharePoint_RM.GetString("ExpireDateInPast", _Cult);
// will result in 'The Expire date is in the past.'

But how do you know how the resource name for your translated text might be? Your friend and helper is the Reflector. Open up the ‘Microsoft.SharePoint.Intl.dll’ from the ‘C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SharePoint.intl\12.0.0.0__71e9bce111e9429__71e9bce111e9429c’ folder (navigate to that path with cmd.exe and copy the dll to some safe place like c:\temp).

A big thanks goes to Janne Mattila for the information in the comment for this blog entry.