Asp.net多语言网站验证

时间:2016-09-22 09:11:08

标签: asp.net asp.net-mvc

我做了多语言网站。所有工作都很棒。我将翻译保存在.resx文件中。但验证信息只显示在一个语言中。 我的代码是

public class CultureAttribute : FilterAttribute, IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        string cultureName = null;
        // Получаем куки из контекста, которые могут содержать установленную культуру
        HttpCookie cultureCookie = filterContext.HttpContext.Request.Cookies["lang"];
        if (cultureCookie != null)
            cultureName = cultureCookie.Value;
        else
            cultureName = "ru";

        // Список культур
        List<string> cultures = new List<string>() { "ru", "en" };
        if (!cultures.Contains(cultureName))
        {
            cultureName = "ru";
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName);
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //не реализован
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用.resx文件在模型上提供验证消息:

[Required(
    ErrorMessageResourceType = typeof(ErrorMessages), 
    ErrorMessageResourceName = "UsernameIsRequiredMessage")]
public string Username { get; set; }