ASP Net MVC-验证消息的本地化 [英] ASP Net MVC - Localization of Validation Messages

查看:82
本文介绍了ASP Net MVC-验证消息的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC 5应用程序中遇到了验证消息本地化的问题.

I ran into a problem with localization of validation messages in my asp net mvc 5 application.

我将其用于本地化

路由配置:

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

LocalizationAttribute:

LocalizationAttribute:

 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请求后显示验证消息.

This is inside a Html.BeginForm. The validation messages are shown after a POST request, if something is missing or not valid.

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

Web.config:

Web.config:

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

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

Localization works for everything on the website except for validation messages.

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

It always shows the resource string that matches the browsers language settings, although the user navigated to e.g.: site.de/en-EN/test.

web.config是否存在问题?我试图将enableClientBasedCulture设置为false,但问题仍然存在.

Is there a problem with the web.config? I tried to set enableClientBasedCulture to false, but the problem still occured.

亲切的问候, 马丁

推荐答案

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

I have found a solution to my problem here: http://afana.me/post/aspnet-mvc-internationalization.aspx

我要做的是删除我的国际化"属性,并创建一个"BaseController"类,该类基本上对每个请求执行与国际化"属性相同的操作.

What i did was to remove my "Internationalization" Attribute and create a "BaseController" class that basically does the same thing as the "Internationalization" Attribute on each request.

Basecontroller:

Basecontroller:

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

本地化现在可用于标签和验证消息.

Localization works now for labels and validation messages.

这篇关于ASP Net MVC-验证消息的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆