ASP.NET MVC 3: - 使用数据库而不是资源文件作为本地化存储
我们在数据库中有本地化的字符串,并且想知道extending the ASP.NET Resource Provider Model是否可以与ASP.NET MVC 3 Razor视图引擎一起使用。ASP.NET MVC 3: - 使用数据库而不是资源文件作为本地化存储
请让我知道ASP.NET MVC 3 Razor视图引擎是否支持从数据库中检索本地化的字符串,只要我们扩展了ASP.NET资源提供程序模型。或者它只适用于Classic ASP.NET,而不适用于ASP.NET MVC。
谢谢
SatyaprakashĴ
你是幸运的,因为里克已经做了你!
Westwind.Globalization Data Driven Resource Provider for ASP.NET
该示例不适用于Asp.Net MVC 3.0 – doganak 2012-07-23 15:21:49
@doganak:数据库资源提供程序对于所有ASP.net都很常见 – 2012-07-23 18:39:07
到目前为止,我已经找到了干净的解决方案是:http://www.codeproject.com/Tips/514321/A-Simple-and-Effective-Way-to-Localize-ASP-Net-MVC。
欢迎评论/反馈。
编辑1:根据评论,我添加了代码示例并将链接用作参考。
我创建了一个customDataAnnotationsProvider类:
public class CustomDataAnnotationsProvider: DataAnnotationsModelMetadataProvider
{
private ResourceManager resourceManager = new ResourceManager();
protected override ModelMetadata CreateMetadata(
IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor,
Type modelType,
string propertyName)
{
string key = string.Empty;
string localizedValue = string.Empty;
foreach (var attr in attributes)
{
if (attr != null)
{
if (attr is DisplayAttribute)
{
key = ((DisplayAttribute)attr).Name;
if (!string.IsNullOrEmpty(key))
{
localizedValue = resourceManager.GetLocalizedText(key);
((DisplayAttribute)attr).Name = localizedValue;
}
}
else if (attr is ValidationAttribute)
{
key = ((ValidationAttribute)attr).ErrorMessage;
if (!string.IsNullOrEmpty(key))
{
localizedValue = resourceManager.GetLocalizedText(key);
((ValidationAttribute)attr).ErrorMessage = localizedValue;
}
}
}
}
return base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
}
}
然后,我在Global.asax中
引用上ApplicationStart自定义提供ModelMetadataProviders.Current = new Project.Web.Helpers.CustomDataAnnotationsProvider();
你不必改变你的模型,并可以使用显示注解:
[Display(Name = "CustomerAccountNumber")]
public string CustomerAccountNumber { get; set; }
请注意,仅提供链接的答案是不鼓励的,引用会随着时间的推移而变得陈旧。考虑在这里添加一个独立的简介,保持链接作为参考。 – kleopatra 2013-03-20 12:24:04
[ASP.NET MVC 2 Localization/Globalizatio n存储在数据库中吗?](http://stackoverflow.com/questions/2568129/asp-net-mvc-2-localization-globalization-stored-in-the-database) – jrummell 2011-12-19 14:50:05