I have a multilanguage website and I'm using donutcache.
I would like to redirect the user depending their browser language and stored the language in a cookie. Then redirect automatically the user to the correct language ( /fr /nl or if english stay on /) if they are coming from the home page (/).
I used to have a simple code that checked the cookie and redirect the user to his cookie language or browser one. Similar to this:
if (Request.Url.AbsolutePath == @"/" || Request.Url.AbsolutePath == "/default.aspx")
{
string[] acceptedLanguages = { "en", "fr", "nl" };
string lang = "en";
if (HttpContext.Current.Request.Cookies["lang"] != null)
{
lang = HttpContext.Current.Request.Cookies["lang"].Value;
if (acceptedLanguages.Contains(lang) && lang != "en")
{
Response.Redirect(HttpContext.Current.Request.Cookies["lang"].Value);
}
}
else
{
var browserLanguages = HttpContext.Current.Request.UserLanguages;
foreach (string blang in browserLanguages)
{
foreach (string alang in acceptedLanguages)
{
if (blang.Contains(alang))
{
lang = alang;
//create cookie
var cookie = new HttpCookie("lang", lang);
cookie.Expires = DateTime.Now.AddDays(360);
cookie.Domain = HttpContext.Current.Request.Url.Host;
HttpContext.Current.Response.Cookies.Add(cookie);
if (lang != "en")
{ Response.Redirect(lang); }
}
}
}
}
}
The problem is the caching issue, it keeps redirect all the user to the first user choice of course...
How and where can I run this code in a donut hole ? If my home controller has to cached and if I don't want to use JS?
Is this intended to run only on the homepage? If so, you could create a controller/action method specific to the homepage that then is not decorated with [OutputCache]. That way, the homepage won't be cached and can run dynamic server side logic.
Alternatively, you could have the JavaScript make a request to a web method that then returns information indicating whether or not the JavaScript should redirect. Based on the cookie, the JavaScript can then avoid calling the web method on subsequent visits.
Yes it's only on the home page. The home controller is cached and I want to keep it cached.
The javascript is not the best option as the page has to be loaded for nothing before it happen.
The problem is that you can not redirect from a child action.
I could not cache the main home controller but the inside content by moving it into a surface controller. Is there another easier way ?
/// <summary>
/// The global.
/// </summary>
public class Global : UmbracoApplication
{
/// <summary>
/// The initialization.
/// </summary>
public override void Init()
{
var application = this as HttpApplication;
application.PreRequestHandlerExecute += this.PreRequestExecute;
base.Init();
}
private void PreRequestExecute(object sender, EventArgs e)
{
// logic for cookie detection and redirct goes here
}
}
}
And then in your global.asax file in your root change the directive to inherit from your custom global class
Donut cache and language redirection
Hi,
I have a multilanguage website and I'm using donutcache. I would like to redirect the user depending their browser language and stored the language in a cookie. Then redirect automatically the user to the correct language ( /fr /nl or if english stay on /) if they are coming from the home page (/).
I used to have a simple code that checked the cookie and redirect the user to his cookie language or browser one. Similar to this:
The problem is the caching issue, it keeps redirect all the user to the first user choice of course... How and where can I run this code in a donut hole ? If my home controller has to cached and if I don't want to use JS?
Thanks for your help
Is this intended to run only on the homepage? If so, you could create a controller/action method specific to the homepage that then is not decorated with
[OutputCache]
. That way, the homepage won't be cached and can run dynamic server side logic.Alternatively, you could have the JavaScript make a request to a web method that then returns information indicating whether or not the JavaScript should redirect. Based on the cookie, the JavaScript can then avoid calling the web method on subsequent visits.
Hi Nicholas,
Yes it's only on the home page. The home controller is cached and I want to keep it cached. The javascript is not the best option as the page has to be loaded for nothing before it happen. The problem is that you can not redirect from a child action. I could not cache the main home controller but the inside content by moving it into a surface controller. Is there another easier way ?
We did something similar, but early in the request pipeline.
Will post the code later, don't have my laptop with me
Hi Dave,
Thanks that would be nice! Was it in the route handler like before the controller is rendered ?
Hi,
We did this with a custom global.asax
The code looks like this :
namespace MyProject.Web.Core {
}
And then in your global.asax file in your root change the directive to inherit from your custom global class
Dave
Hi Dave,
Thanks it works nicely and it's clean thanks a lot!
Florent
is working on a reply...