2

如何获取页面当前使用的 .resx 文件?例如,如果我在 Default.aspx 上将区域性设置为 fr-FR,它应该给我 Default.aspx.fr.resx 或 Default.aspx.fr-FR.resx 或 Default.aspx.resx 取决于哪个存在。

他们在 ASP.NET 中有类似的东西还是我必须自己编写?

4

2 回答 2

3

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");
    }
}
于 2009-07-09T03:03:30.673 回答
0

I'm not aware of a built-in property that would return the current resource file in this fashion.

CultureInfo.CurrentUICulture.Name returns the current UICulture in use, and the Name property is the shortened form. You could use this to build up the info yourself, for example:

string pageResx = VirtualPathUtility.GetFileName(Request.Path) + "." + 
CultureInfo.CurrentUICulture.Name + ".resx";

Depending on what you plan to do with this info I would be somewhat cautious and you should test this approach for your scenario.

于 2009-07-09T01:41:58.613 回答