I have an ajax call that return a localized success message based on httpcontext,
public ActionResult ReportViolation(string DiscussID) {
Enumeration.Lang rLang = (Request.Cookies["MAKANI.Localization.CurrentUICulture"].Value.Equals("ar-EG")) ?
Enumeration.Lang.Arabic : Enumeration.Lang.English;
if (Session["sId"] == null) return Json(false,
HttpContext.GetLocalResourceObject("~/Views/Home/Index.cshtml", "msgDiscussViolationRegister").ToString(),
0,
null);
else {
long userId = AuthenticationModule.GetUserId(Convert.ToInt32(Session["sId"]));
bool rReport = Discuss_BL.ReportViolation(Convert.ToInt32(DiscussID), userId);
string rError = rReport
? HttpContext.GetLocalResourceObject("~/Views/Home/Index.cshtml", "msgDiscussViolationSuccess").ToString()
: HttpContext.GetLocalResourceObject("~/Views/Home/Index.cshtml", "msgDiscussViolationExist").ToString();
return Json(rReport, rError, 0, null);
}
}
However, the resources always sends back the english version, is this the right way to use HttpContext from the controller?
—
Regards.
Yehia A.Salam
HttpContextworks as the server context, it will by default return the locale of the operating system (CultureInfo.CurrentUICulture, to be precise). You need to read the users locale somehow – either by queryingHttpContext.Current.Request.UserLanguages, which may be empty, or by asking user for this information explicitly.As Scott Hanselman writes in his excellent article on globalization, ASP.Net can actually set the worker thread culture (
Thread.Current.UICultureandThread.Current.Culture) for you, parsing theAccept-LanguagesHTTP header. Just add this snippet to yourweb.configfor<globalization>:Just be aware that user might have not setup his browser correctly, so he should always have the option to override the language settings.