我正在开发一个Asp.net mvc webSite,其要求是用户应该能够更改语言并且页面内容应该自动翻译 . 问题是:我在My Model中使用了Custom Attribute来显示本地化,如下所示:[CommonCustomAttributes.LocalizedDisplayName(“Register”,“UserEmail”)] .
LocalizedIdsplayname实现如下:[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter,AllowMultiple = false)] public sealed class LocalizedDisplayNameAttribute:DisplayNameAttribute {public LocalizedDisplayNameAttribute(string resourceFileName,string keyName):base(GetMessageFromResource(resourceFileName,keyName) )){}
private static string GetMessageFromResource(string resourceFileName, string keyName)
{
try
{
return CustomLocalizationUtility.GetKeyValue(HttpContext.Current, resourceFileName, keyName);
}
catch (Exception)
{
return "";
}
}
}
因此,当我在语言之间切换时,它不会改变 . 我的意思是使用它的模型的显示名称,如@ Html.LabelFor(m => m.UserEmailAddress,new {@class =“col-md-3 control-label” }) .
但是在我使用@placeholder = @ CustomLocalizationUtility.GetKeyValue(“Register”,“EnterEmail”)的.cshtml中,当我改变语言时,它工作正常 . 我错过了什么严肃吗?我已经使用Cookie来设置当前文化,我也在基础控制器中覆盖了BeginExecuteCore方法 . 我已经从Action方法设置了当前文化的Cookie . 我们是否需要根据当前的文化变化为JQuery Obstrusive验证和模型Displayname更改添加额外的javascript?
1 回答
除非您在更改lang时以及在调用上述标签之前更改应用程序中的文化,否则它将无法工作,即UserEmailAddress . 您可以创建一个actionfilter属性并在控制器中调用它 .
这是我的应用程序的代码
这些资源可以提供进一步的帮助
它讨论了您可以设置文化的所有地方
Best place to set CurrentCulture for multilingual ASP.NET MVC web applications
另一个
Set Culture in an ASP.Net MVC app
更新