November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
Hi Dan,
You could try to change the value of the 'editlanguagebranch' cookie.
Patrick
Thanks Patrick for the reply
But "editlanguagebranch" cookie is used in the "admin or edit page" context. So, it is not effecting the ordinary users that using the site (with reference to : http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-CMS/7/Globalization/Handling-Languages/)
I have already used "epslanguage" cookie and the problem is, even though if I set "epslanguage" cookie to "no" (Norwegian), if I click a some other language direct link (just say "http://mysite/en/link-to-an-english-page"), then after site is fall-back to that particular language (in this example it is "en").
Sorry for that I misunderstood, I thought you meant changing the language of the CMS. So if I get this right, you set the epslanguage cookie to 'no' (what should be right) after that you browse to a English page that will show the page in English. I'm not sure what the problem is?
Patrick, what I mean was, if I set the "epslanguage" to "no", yes that will work initially (i.e. site will load Norwegian UI/System/Content). But if I navigate to a another language (say English) page (via a link or something ....), then after whole site is change to that language (UI/System/Content). Actually I don't need this sort of behaviour.
Dan,
This is default behavior of EPiServer, EPiServer is using the order below for determing the language:
http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-CMS/75/Globalization/Globalization/
You can use code in your base controller to check whether the current page is loaded in a different language then the language defined in the 'epslanguage'. That would be something like this:
var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>(); var content = urlResolver.Route(new UrlBuilder(this.Request.Url)); if (content != null) { var page = content as PageData; if (!page.LanguageID.Equals(Request.Cookies["epslanguage"].Value, StringComparison.InvariantCultureIgnoreCase)) { var newUrl = urlResolver.GetUrl(content.ContentLink, Request.Cookies["epslanguage"].Value); Response.Redirect(newUrl); } }
Problem is I do have a menu system that will pick page link like following and create the menu (in run time)
http://mysite/no/sportclub/sport
http://mysite/no/sportclub/events
http://mysite/no/memberclub/groupsharing
etc ....
but if I click on a link (while site is in "no") like http://mysite/en/sportclub/games then all the links in the menus become "en". that is
http://mysite/en/sportclub/sport
http://mysite/en/sportclub/events
http://mysite/en/memberclub/groupsharing
etc ....
So how do I prevent this behaviour (that is, if I set system language as "no", it need to be last for every request, even though temporary clicked on "en" content)
You could prevent this by creating your own implementation of the IUpdateCurrentLanguage interface. The interface contains the method UpdateLanguage, this method updates the ContentLanguage. THe UpdateLanguage is called by the routing classes of EPiServer. I've tested the below code sample:
public class MyUpdateCurrentLanguage : IUpdateCurrentLanguage { public void UpdateLanguage(string languageId) { if (!string.IsNullOrEmpty(languageId) && !languageId.Equals(HttpContext.Current.Request.Cookies["epslanguage"].Value, StringComparison.InvariantCultureIgnoreCase)) { ContentLanguage.Instance.SetCulture(HttpContext.Current.Request.Cookies["epslanguage"].Value); } else { ContentLanguage.Instance.SetCulture(languageId); } SystemLanguage.Instance.SetCulture(); UserInterfaceLanguage.Instance.SetCulture(); } public void UpdateReplacementLanguage(EPiServer.Core.IContent currentContent, EPiServer.Core.ILanguageSelectionSource languageSource) { var updatecurrentLanguage = new UpdateCurrentLanguage(); updatecurrentLanguage.UpdateReplacementLanguage(currentContent, languageSource); } }
The UpdateLanguage checks whether the passed languageId is not equals to the 'epslanguage' cookie, is this is true then the ContentLanguage is set to the value in the cookie. So for example if the user browse to the URL http://mysite/en/sportclub/sport while the 'epslanguage' cookie value is set to 'no' then the ContentLanguage is set to 'no' instead of 'en'. That means that the page is just rendered in the culture 'no'.
You also need to configur your IOC like this:
container.For<IUpdateCurrentLanguage>().EnrichAllWith(y => new MyUpdateCurrentLanguage());
If you still would like to display some properties of the page in English you could get the language from the URL (http://mysite/en/sportclub/sport) and just get a PageData with the IContentLoader.Get method by passing the ContentReference and the ILanguageSelector.
Hope this helps.
Thanks Patrick for your suggestion of implementing "IUpdateCurrentLanguage" interface and handle language handling mechanism. I have tried it. But it didn't resolve my problem completely
As an example (after doing what you have suggested), if my site is in Norwegian language (setting "epslanguage" cookie to "no") and if I want to access some English content (like "http://mysite/en/sportclub/sport/the-game") or if I try to access page that is in English and it's fallback or replacement language for Norwegian is set to English, I'll get following error
Feilaktig kobling Koblingen som du angav, virker ikke. Siden er enten fjernet eller flyttet. Hvis du klikket på en kobling, bør du melde fra til webansvarlig for det aktuelle området om at det er en feil med koblingen.
I implemented the IUpdateCurrentLanguage in a similar way to the one given by Patrick, but it is only the block content on my pages that are displayed in the language set in this implementation. The page content (properties found directly on the page) is always given in the language stated in the URL. How can I fix this?
If I debug and check the page content injected in my page controller's actions, its language is always the language from the URL. Debugging the block controller's actions, the language is set correctly to the one given in my implementation of IUpdateCurrentLanguage. I suspect that the language is not changed soon enough for all content being in the correct language. When is the IUpdateCurrentLanguage implementation invoked in the pipeline? Is it before RouteData is populated with content from Epi?
I found this forum post and this GitHub entry which indicates that the language should not be changed in IUpdateCurrentLanguage. Can anyone confirm this?
If I still need to change the language, how do I go about doing that, then? Overridding the MultiplexingRouteHandler somehow so it uses the language that I set in IUpdateCurrentLanguage?
My solution was to implement af custom MultiplexingRouteHandler and set the language in there:
public class LanguageRouteHandler : MultiplexingRouteHandler { public LanguageRouteHandler(IContentLoader contentLoader, IPermanentLinkMapper permanentLinkMapper, TemplateResolver templateResolver, IUpdateCurrentLanguage updateCurrentLanguage, LocalizationService current, LanguageResolver languageResolver) : base(contentLoader, permanentLinkMapper, templateResolver, updateCurrentLanguage, current, languageResolver) { } public override IRouteHandler GetRouteHandler(RequestContext requestContext) { var language = requestContext.GetLanguage(); UpdateLanguage(language); requestContext.SetLanguage(ContentLanguage.PreferredCulture?.Name); return base.GetRouteHandler(requestContext); } private static void UpdateLanguage(string languageId) { // Set the language in your own way here var myLanguage = "en"; ContentLanguage.Instance?.SetCulture(myLanguage); } }
I just want to implement runtime language (User Interface Language and System Language) changer. Like giving a drop-down to the user and based on user selection, change the UI & System language (not a single page UI language). Have tried with several techniques after doing some googling. But didn't work either (like setting EPiServer.BaseLibrary.Context.Current["EPiServer:ContentLanguage"])
Can anyone help me with some coding example ?