MVC data annotation level: where to put the CurrentUICulture statement?

I'm going crazy about localizing an MVC application.

After my recent question, I took this approach:

  • The language is saved in the session ["lang"]
  • Each controller inherits from my own BaseController, which overrides OnActionExecuting and in this method reads the session and sets CurrentCulture and CurrentUICulture

This works fine until the level of data annotation appears. It seems to be called before the action is executed, and therefore it always gets error messages in the default language!

Field declarations are as follows:

[Required(ErrorMessageResourceName = "validazioneRichiesto", ErrorMessageResourceType = typeof(Resources.Resources))]
public string nome { get; set; }

So, is there a reasonable place where I can call?

I initialize the data annotation model binding in my controller constructor.

public CardController() : base() {
    ModelBinders.Binders.DefaultBinder =
    new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
}

So, since Session is always null in the controller constructor, and the action override is called AFTER the data annotation has checked the fields , where can I set CurrentCulture and CurrentUICulture to get localized errors?

I tried putting CurrentCulture and CurrentUiCulture in Application_ * (e.g. Application_AcquireRequestState or Application_PreRequestHandlerExecute) seems to have no effect ...

+3
source share
4 answers

, global.asax.cs Application_BeginRequest.

( cookie), .

EDIT:

/by Brock Allen: http://www.velocityreviews.com/forums/t282576-aspnet-20-session-availability-in-globalasax.html/

PreRequesthandlerExecute.

, , (, WebResourxe.axd) Session ( IRequireSessionState). , , .

, :

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
    if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
        SetCulture();    
}

, , mvc

+2

, , .

Resource.resx, Build Action Embedded Resource Custom Tool PublicResXFileCodeGenerator

alt text http://img208.imageshack.us/img208/2126/captuream.png

- . , .

+1

, , lang URL-, :

  • .
  • URL- .

Application_BeginRequest Global.asax

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim lang As String
    If HttpContext.Current.Request.Path.Contains("/en/") Then
        lang = "en"
    Else
        lang = "es"
    End If
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End Sub

, , .

0
source

The OP sent the final solution as follows, thanks to twk's accepted answer:

void Application_PreRequestHandlerExecute(object sender, EventArgs e) {
  if (Context.Handler is IRequiresSessionState || 
      Context.Handler is IReadOnlySessionState) {
    if (Session["lang"] == null) {
      Session["lang"] = "it";
    }

    if (Request.QueryString["lang"] == "it" || Request.QueryString["lang"] == "en") {
      Session["lang"] = Request.QueryString["lang"];
    }

    string lang1 = Session["lang"].ToString();
    string lang2 = Session["lang"].ToString().ToUpper();

    if (lang2 == "EN")
      lang2 = "GB";

    System.Threading.Thread.CurrentThread.CurrentCulture = 
        System.Globalization.CultureInfo.CreateSpecificCulture(lang1 + "-" + lang2);
    System.Threading.Thread.CurrentThread.CurrentUICulture = 
        System.Globalization.CultureInfo.CreateSpecificCulture(lang1 + "-" + lang2);
  }
}
0
source

Source: https://habr.com/ru/post/1718987/


All Articles