出错“无法找到适合指定文化或中立文化的资源。”

时间:2013-09-17 07:40:15

标签: asp.net-mvc localization

我在访问enum的本地化值时遇到错误,如

@(((FeeType)Model[i].FeeType).GetDescription())

Could not find any resources appropriate for the specified culture or the neutral culture.
Make sure "Resources.Admin.resources" was correctly embedded or linked into assembly "Auction" at compile time, or that all the satellite assemblies required are loadable and fully signed.

我的枚举

public enum FeeType
{
    [LocalizedDescriptionAttributre("Site_Fee_Type_MonthlyServiceFee", typeof(Resources.Admin))]
    ClientFees = 1
}

和Extention类是

public class LocalizedDescriptionAttributre : DescriptionAttribute
{
    private readonly string _resourceKey;
    private readonly ResourceManager _resource;
    public LocalizedDescriptionAttributre(string resourceKey, Type resourceType)
    {
        _resource = new ResourceManager(resourceType);
        _resourceKey = resourceKey;
    }

    public override string Description
    {
        get
        {
            string displayName = _resource.GetString(_resourceKey);

            return string.IsNullOrEmpty(displayName)
                ? string.Format("[[{0}]]", _resourceKey)
                : displayName;
        }
    }
}

public static class EnumExtensions
{
    public static string GetDescription(this Enum enumValue)
    {
        FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute),
            false);

        if (attributes != null &&
            attributes.Length > 0)
            return attributes[0].Description;
        else
            return enumValue.ToString();
    }
}

0 个答案:

没有答案