问题
I have two root nodes in my Umbraco website.. one is set to English and other is set to German using Manage Hostnames ..
|- en
|---- english page1
|---- english page2
|- de
|---- german page1
|---- german page2
http://mywebsite.com is set to en node and http://mywebsite.de is set to de node.
I need to change the German node's language to English in certain conditions.. Is this possible and how?
For example if someone calls an English page using German hostname, I need to change the locale to English
For example
http://mywebsite.de/english-page1.aspx should be in English locale.. so the dictionary etc need to be loaded from English
http://mywebsite.com/german-page1.aspx should be in German locale.. so the dictionary etc need to be loaded from German
I have written an HttpModule to change the locale on PreRequestHandlerExecute but without no success
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-CH");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-CH");
}
回答1:
I think the PreRequestHandler is too early in the page cycle. On the call to the default.aspx page the culture is set by Umbraco. I myself added the culture change to the constructor of my base MasterPage, the masterpage which is always called on any page. You could also change culture at Page Init or Page Load.
Kind regards,
Corné Hogerheijde
回答2:
You can check for the host on Session_Start
and redirect them to particular language page without much hassle
void Session_Start(object sender, EventArgs e)
{
// Your logic will go here
}
回答3:
I realise this is very old, but I found it when looking for an answer and thought I'd share what I've done. I'm using Umbraco 7.5 and MVC.
First I created a Filter:
public class LanguageFilterAttribute : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var httpContext = filterContext.RequestContext.HttpContext;
if (!string.IsNullOrEmpty(httpContext?.Request.QueryString["lang"]))
{
if (httpContext.Request.QueryString["lang"].ToLower().StartsWith("en"))
httpContext.Session["lang"] = "en";
else if (httpContext.Request.QueryString["lang"].ToLower().StartsWith("fr"))
httpContext.Session["lang"] = "fr";
}
if (httpContext.Session["lang"] != null)
{
switch (httpContext.Session["lang"].ToString())
{
case "en":
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
break;
case "fr":
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");
break;
}
}
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
}
}
Then added the filter on in OnApplicationStarted
public class MyUmbracoApplication : Umbraco.Web.UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
GlobalFilters.Filters.Add(new LanguageFilterAttribute());
}
}
Whenever I want to change the lang/locale I just add ?lang=en
or ?lang=fr
to any url. This also changes which text I display. Each of my text fields are prefixed with the simple language code eg. 'fr_pageTitle' and 'en_pageTitle'. I then have an extension method to pull out the correct text from my MVC view
public static class PublishedContentExtensions
{
public static T GetPropertyLangValue<T>(this IPublishedContent content, string fieldName)
{
var lang = CoreHelper.GetSessionLanguage();
if (string.IsNullOrEmpty(lang))
return content.GetPropertyValue<T>(fieldName);
return content.GetPropertyValue<T>($"{fieldName}_{lang}");
}
}
Hope this helps someone
来源:https://stackoverflow.com/questions/8786339/programmatically-change-locale-language-of-a-page