Try our conversational search powered by Generative AI!

How to Change EPiServer UI and System Language in Runtime

Dan
Dan
Vote:
 

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 ?

#116983
Feb 09, 2015 14:14
Vote:
 

Hi Dan,

You could try to change the value of the 'editlanguagebranch' cookie.

Patrick

#116999
Feb 09, 2015 17:10
Dan
Vote:
 

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").

#117004
Edited, Feb 09, 2015 19:32
Vote:
 

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?

#117005
Feb 09, 2015 19:51
Dan
Vote:
 

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. 

#117016
Feb 10, 2015 7:35
Vote:
 

Dan,

This is default behavior of EPiServer, EPiServer is using the order below for determing the language:

  • If there is a language indicator in the URL (a friendly URL like http://company.com/en/info, or a classic URL like http://company.com/templates/page.aspx?id=23&epslanguage=en), that language is used (en).
  • If you are in the edit view and have a language selection from the language selection drop-down list, that language is used.
  • If you have defined the host name to be associated with a specific language, that language is used, see information about section <siteHosts> in web.config for more information.
  • If the requests contain a cookie named epslanguage, use the language defined by the cookie.
  • If the web.config setting pageUseBrowserLanguagePreferences is true, then the language preference from the web browser is used.
  • Fetch the setting from the uiCulture attribute on the globalization node in web.config if defined.
  • If nothing else is discovered, use the first enabled language branch as defined in Admin / Language Branches, which means that this could be viewed as the default 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);
                }
            }
#117018
Feb 10, 2015 8:50
Vote:
 
#117024
Feb 10, 2015 10:10
Dan
Vote:
 

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)

#117030
Feb 10, 2015 12:13
Dan
Vote:
 

Any Help ?

#117096
Feb 11, 2015 15:36
Vote:
 

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.

#117112
Feb 11, 2015 22:02
Dan
Vote:
 

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.



#118480
Edited, Mar 06, 2015 22:03
Vote:
 

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?

#187022
Jan 10, 2018 14:18
Vote:
 

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?

#187075
Edited, Jan 11, 2018 11:29
Vote:
 

Any help?

#187169
Jan 15, 2018 11:37
Vote:
 

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);
        }
    }
#187306
Jan 18, 2018 10:14
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.