Skip to content

Instantly share code, notes, and snippets.

@jcorderodr
Created July 28, 2017 00:44
Show Gist options
  • Select an option

  • Save jcorderodr/8289b191df5f35c19ccc5b653ae29ce7 to your computer and use it in GitHub Desktop.

Select an option

Save jcorderodr/8289b191df5f35c19ccc5b653ae29ce7 to your computer and use it in GitHub Desktop.
Get/Set User Browser Language C# - ASP.NET MVC (Razor)
<div>[@{
// This, in your skin/theme/layout
var es = (UICulture.Equals("es-ES")) ? "active" : "inactive";
var en = (UICulture.Equals("en-US")) ? "active" : "inactive";
@Html.ActionLink("Español", "Language", "Home", new {lang = "es-ES", returnUrl = Request.RawUrl}, new {Class = es})
<strong>|</strong>
@Html.ActionLink("English", "Language", "Home", new {lang = "en-US", returnUrl = Request.RawUrl}, new {Class = en})
}]
</div>
// This, could be in your _ViewStart
@{
var lang = Session[Controllers.XController.SessionLangKey];
Culture = lang != null ? UICulture = lang.ToString() : UICulture = "en-US";
}
// This final block, in your controller.
public const string SessionLangKey = "UsrLang";
public ActionResult Language(string lang, string returnUrl)
{
if (String.IsNullOrWhiteSpace(lang)) View("Index");
switch (lang.ToLower())
{
case "en-EN":
case "es-ES":
Session.Add(SessionLangKey, lang);
break;
}
return Redirect(returnUrl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment