ASP.NET MVC:语言全球化 - 必需的错误消息始终为英语

时间:2015-05-27 22:16:11

标签: asp.net-mvc razor resources globalization multilingual

我正在学习如何进行全球化,一切都运作良好,除了一件事。我有一些必填字段,他们的错误信息总是英文。其他所有内容都会交换语言,并在回发后保持正确的语言,但无论如何,该错误信息都是英语。我真的希望你能帮助我发现我的错误!

型号:

namespace xxxx.Models
{
    public class PatientSatisfactionSurveyPage : Page
    {

        public Boolean Page1Complete { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question1Required")]
        public int? LikelyToReturn { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.CustomerSatisfactionSurvey), ErrorMessageResourceName = "Page1Question2Required")]
        public int? RecomendToFriend { get; set; }
    }

查看:

<div><input type="submit" id="page1-submit" name="page1-submit" value="continue" class="btn green2"></div>
<div>@Html.ValidationSummary()</div>

控制器:

    [HttpGet]
    public ActionResult PatientSatisfactionSurvey(string ApptID, string LanguageCode)
    {

        PatientSatisfactionSurveyPage pss = new PatientSatisfactionSurveyPage();

        // Create list of available language options
        pss.LanguageOptions = new List<string> { "en", "es", "fr" };

        // Validate and set the language code
        if (!string.IsNullOrEmpty(LanguageCode))
        {
            // Check the incoming Language Code (LanguageCode) against the list off approved options
            if (pss.LanguageOptions.Contains(LanguageCode))
            {
                // Set the culture based on the language code
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(LanguageCode);
                pss.LanguageCode = LanguageCode;
            }
        }
    }

    [HttpPost]
    public ActionResult PatientSatisfactionSurvey([Bind]PatientSatisfactionSurveyPage pss, string ApptID)
    {

        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(pss.LanguageCode);

        if (ModelState.IsValid)
        {
            pss.Page1Complete = true;
        }

        return View(pss);

1 个答案:

答案 0 :(得分:1)

解决方案是使用cookie在Gobal.asax.cs文件的Application_BeginRequest()中设置文化。我还修改了控制器以设置cookie

    protected void Application_BeginRequest()
    {
        HttpCookie LanguageCookie = Request.Cookies["LanguageCode"];
        if (LanguageCookie != null)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(LanguageCookie.Value);
        }
    }