【问题标题】:.resx file the page is currently using页面当前使用的 .resx 文件
【发布时间】:2009-07-09 00:33:58
【问题描述】:

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

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

【问题讨论】:

    标签: asp.net localization globalization resources resx


    【解决方案1】:

    您可以使用 ResourceManager 类的GetResourceFileName() 方法来构造一个有效的资源文化名称。通过反射器快速查看方法实现向我们展示了该方法利用调用者传递的 CultureInfo 对象的 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();
    }
    

    GetResourceFileName() 方法调用内部静态方法 VerifyCultureName() 以确保我们有一个有效的资源文化名称。看一下 VerifyCultureName() 方法,我们可以看到发生了一些简单的验证。

    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;
    }
    

    要使用 GetResourceFileName() 方法,您必须从 ResourceManager 类型派生一个类并覆盖基类中的虚拟方法。 GetResourceFileName() 方法是受保护的,因此我们必须将其包装在一个公共方法中以将其公开给外界。

    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");
        }
    }
    

    【讨论】:

    • 这与我从自定义方法中得到的结果很接近,该方法将返回 Default.aspx.en-US.resx。但该页面实际上使用的是 Default.aspx.resx,因为 en-us.resx 不存在。我目前正在检查是否存在 en-us.resx 或 en.resx,然后回退到默认的 .resx,它可以正常工作。只是想知道是否有更好/标准的方法来找到它。
    【解决方案2】:

    我不知道会以这种方式返回当前资源文件的内置属性。

    CultureInfo.CurrentUICulture.Name 返回当前正在使用的 UICulture,Name 属性是缩写形式。您可以使用它自己构建信息,例如:

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

    根据您打算如何处理此信息,我会有些谨慎,您应该针对您的场景测试这种方法。

    【讨论】:

    • 这是我开始的。它总是会告诉你 .aspx.en-US.resx,即使页面使用的是 .aspx.resx。我添加了代码来检查 en.resx 和 en-US.resx 是否存在,以解决后备情况。如果有一种标准的方法可以找到它,那就太好了......
    猜你喜欢
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2011-11-08
    • 2015-10-07
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多