Umbraco - Change culture programmatically - umbraco

I'm trying to change Umbraco Culture with French/English button in top-left corner. When I click this button, I redirect to a function :
public ActionResult ChangeLanguage(string lang)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
var url = System.Web.HttpContext.Current.Request.UrlReferrer;
Response.Redirect(url.ToString());
return Content("");
}
I've debugged and my lang parameter is ok, but the CurrentCulture is never saved when the page is refreshed.

Related

Mvc View and HttpPost have different Culture Values

In my View the culture value is right. But in my ajax HttpPost method the culture is wrong. My goal is check url lang if empty check cookie if it is again empty I get the language from client(English,German,Turkish) default is TUrkish if client have another lang. I try to explain with example. My framework is 4.7.2
Here is my OnActionExecuting:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.HttpMethod == "GET")
{
var domainLang = RouteData.Values["lang"] as string;
if (!string.IsNullOrEmpty(domainLang) && !string.IsNullOrWhiteSpace(domainLang))
{
//CurrentInfo is my class it is getting values from DB for once
bool avaibleLang = CurrentInfo.AvailableLanguages.Count(x => x.DilKey == domainLang) > 0;
if (avaibleLang)
{
var langCookie = CurrentInfo.CurrentLang;
if (domainLang != langCookie.DilKey)
{
ClientCookieManagement.CreateCookie(CookieEnum.Language.GetStringValue(), domainLang, 7);
}
}
else
{
domainLang = CurrentInfo.CurrentLang.DilKey;
}
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(domainLang ?? CurrentInfo.CurrentLang.DilKey);
}
base.OnActionExecuting(filterContext);
}
I check my culture in view and it is right.
#Thread.CurrentThread.CurrentUICulture
#Thread.CurrentThread.CurrentCulture
But if I submit or post something with ajax the culture is wrong
CultureInfo.CurrentUICulture
My webconfig
<globalization culture="tr-TR" -uiCulture="tr-TR"/> Default lang is TR
PROBLEM:
I need get the right value in my controller if I submit or anything else. What am I doing wrong?

Switching Languages with resource files on Actionlink click without refreshing the page Contents

I am working on an asp.net website which has multi language functionality. Currently my code works with the language switch and it displays the form contents with the language that is selected but if user decides to switch language after filling the form partially, my code refreshes the page and the form data is lost in the process.
I have tried to set the changed language using cookie.
Language Controller code -
public ActionResult Change(String Language)
{
if(Language != null)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Language);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Language);
}
HttpCookie cookie = new HttpCookie("Language");
cookie.Value = Language;
Response.Cookies.Add(cookie);
return View("Index");
}
Index view -
<li>#Html.ActionLink("English", "Change", "Language", new { Language = "en"}, null)</li>
<li>#Html.ActionLink("Françe", "Change", "Language", new { Language = "fr" }, null)</li>
<li>#Html.ActionLink("spanish", "Change", "Language", new { Language = "es"}, null)</li>
Global.asax.cs
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
if(cookie != null && cookie.Value != null)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
}
else
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
}
}
Is it possible to switch the language and the page contents without refreshing the page ?
What you are talking about is client side translation. Which means the translation resources have to be present on the page when you change the language.
You can do that with some custom javascript [not the recommended way].
This leads to the question where will resources come from? For example if you have some content like "My project in English" how will the system know what to translate to?
One way is to have the browser translate it on the fly. e.g with google translate. Here is a way to do that.

How to retain page state when selecting language in Multi Language Implementation using ASP.Net MVC?

I am using the following controller method to store the selected language and return to the Home page when users changes the selected language.
Is there a way I can redirect to the same page which the user is viewing right now with a change in display language instead of redirecting to Home Page.
public ActionResult SetCulture(string culture)
{
string test = this.ControllerContext.RouteData.Values["action"].ToString();
// Validate input
culture = CultureHelper.GetImplementedCulture(culture);
// Save culture in a cookie
HttpCookie cookie = Request.Cookies["_culture"];
if (cookie != null)
cookie.Value = culture; // update cookie value
else
{
cookie = new HttpCookie("_culture");
cookie.Value = culture;
cookie.Expires = DateTime.Now.AddYears(1);
}
Response.Cookies.Add(cookie);
return RedirectToAction("Index");
}

How to redirect from AREA's cshtml page to normal controller's ActionResult using #Url.Action?

How can I redirect to an ActionResult of a normal controller from .cshtml page of my Area's view?
I have a common login screen for normal/admin user.
So based on the usertype json result will return the user type.
So now I want to redirect a user based on the usertype.
If a user is normal user then I can redirect him using
"var redirectUrl = '#Url.Action("Index", "Home")';" and then "window.location.href = redirectUrl;"
Above code is working fine.
But what to do in following case?
If a user is an admin, then I want to redirect him to "Index" ActionResult of "HomeController" which is present in "Admin" area.
To redirect outside the area use a blank string for the area:
#Url.Action("Index", "Home", new { area = "" })
This will default you back to the HomeController outside of the area.
If I understand correctly, you just need some conditional logic on your link.
#{
var redirectUrl = Url.Action("Index", "Home", new { area = "" })
if (this.User.IsInRole("Administrator"))
{
redirectUrl = Url.Action("Index", "Home", new { area = "Admin" })
}
}
<script>
var redirectUrl = '#redirectUrl';
window.location.href = redirectUrl;
</script>
Although, a better alternative might be to make your own HTML helper extension method so you can reuse the logic elsewhere.
Finally below syntax worked for me.
var adminHomePageUrl = '#Url.Action("Index","Home", new { area = "Admin" })';

mvc clear cache when logout (when click on back button remain in the login page)

after logout if i click the back button i want to remain in the login page.... but this code wont work when i click back button it will redirect to the previous page. can some help me.
public virtual ActionResult Logout()
{
FormsAuth.SignOut();
ActionResult homeAction = MVC.Account.Login();
string logoffUrl = Url.Action(homeAction);
string[] myCookies = Request.Cookies.AllKeys;
foreach (string cookie in myCookies)
{
Request.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(Response.Cookies[cookie]);
Request.Cookies.Add(Request.Cookies[cookie]);
}
HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Response.Cache.SetValidUntilExpires(false);
HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Response.Cache.SetNoStore();
return RedirectToAction(homeAction);
}
}

Resources