Created
July 28, 2017 00:44
-
-
Save jcorderodr/8289b191df5f35c19ccc5b653ae29ce7 to your computer and use it in GitHub Desktop.
Get/Set User Browser Language C# - ASP.NET MVC (Razor)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This, could be in your _ViewStart | |
| @{ | |
| var lang = Session[Controllers.XController.SessionLangKey]; | |
| Culture = lang != null ? UICulture = lang.ToString() : UICulture = "en-US"; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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