You can use the GetResourceFileName() method of the ResourceManager class to construct a valid resource culture name. A quick peek at the methods implementation via reflector shows us that the method utilizes the name property of the CultureInfo object passed by the caller to build the resource file name.
protected virtual string GetResourceFileName(CultureInfo culture)
{
StringBuilder builder = new StringBuilder(0xff);
builder.Append(this.BaseNameField);
if (!culture.Equals(CultureInfo.InvariantCulture))
{
CultureInfo.VerifyCultureName(culture, true);
builder.Append('.');
builder.Append(culture.Name);
}
builder.Append(".resources");
return builder.ToString();
}
The GetResourceFileName() method calls the internal static method VerifyCultureName() to ensure we have a valid resource culture name. Taking a look into VerifyCultureName() method shows us that some simple validation takes place.
internal static bool VerifyCultureName(CultureInfo culture, bool throwException)
{
if (culture.m_isInherited)
{
string name = culture.Name;
for (int i = 0; i < name.Length; i++)
{
char c = name[i];
if ((!char.IsLetterOrDigit(c) && (c != '-')) && (c != '_'))
{
if (throwException)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", new object[] { name }));
}
return false;
}
}
}
return true;
}
To make use of the GetResourceFileName() method you will have to derive a class from the ResourceManager type and override the virtual method in the base class. The GetResourceFileName() method is protected so we will have to wrap it in a public method to expose it to the outside world.
public class ResxResourceManager : ResourceManager
{
protected override string GetResourceFileName(System.Globalization.CultureInfo culture)
{
return base.GetResourceFileName(culture);
}
public string GetResxFileName(System.Globalization.CultureInfo culture)
{
return GetResourceFileName(culture).Replace(".resources", ".resx");
}
}