gpt4 book ai didi

c# - ASP Net MVC - 验证消息的本地化

转载 作者:太空狗 更新时间:2023-10-29 23:29:28 25 4
gpt4 key购买 nike

我在我的 asp net mvc 5 应用程序中遇到了验证消息的本地化问题。

我用它来进行本地化:

路由配置:

[Internationalization]
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute("DefaultLocalized",
"{language}-{culture}/{controller}/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = "",
language = "de",
culture = "DE"
});

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

本地化属性:

 class InternationalizationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string language = (string)filterContext.RouteData.Values["language"] ?? "de";
string culture = (string)filterContext.RouteData.Values["culture"] ?? "DE";

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
}
}

模型属性示例:

    [Display(Order = 0, ResourceType = typeof(resourceProjectName.ApplicationStrings), Name = "LabelText")] // this works
[Required(ErrorMessageResourceType = typeof(resourceProjectName.ApplicationStrings), ErrorMessageResourceName = "ValidationText")] //this does not work
public string Property { get; set; }

HTML 示例:

这是在 Html.BeginForm 中。如果某些内容丢失或无效,则会在 POST 请求后显示验证消息。

<div>
<div class="form-group row">
<div>
@Html.LabelFor(x => x.Property)
</div>
<div class="col-sm-6">
@Html.TextBoxFor(x => x.Property, new { @class = "form-control" })
</div>
@if (ViewData.ModelState.Where(i => i.Key.Equals("Property")).Any(x => x.Value.Errors.Any()))
{
<div class="col-sm-6">
@Html.ValidationMessageFor(x => x.Property, "", new { @class = "alert-validation alert-danger" })
</div>
}
</div>
</div>

网络配置:

<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true"/>

本地化适用于网站上的所有内容,验证消息除外。

它始终显示与浏览器语言设置匹配的资源字符串,尽管用户导航到例如:site.de/en-EN/test。

web.config 有问题吗?我尝试将enableClientBasedCulture设置为false,但问题依旧。

亲切的问候,马丁

最佳答案

我在这里找到了解决问题的方法:http://afana.me/post/aspnet-mvc-internationalization.aspx

我所做的是删除我的“国际化”属性并创建一个“BaseController”类,该类基本上与每个请求的“国际化”属性做同样的事情。

基地 Controller :

 public class BaseController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
//Localization in Base controller:

string language = (string)RouteData.Values["language"] ?? "de";
string culture = (string)RouteData.Values["culture"] ?? "DE";

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));


return base.BeginExecuteCore(callback, state);
}
}

本地化现在适用于标签和验证消息。

关于c# - ASP Net MVC - 验证消息的本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36353265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com