Why is my Sitecore.Context.Language resetting in Controller?
Asked Answered
D

1

8

Sitecore appears to be doing something freaky with the language variable in it's Context object. If I load a CMS page using the url ?sc_Lang=ru-RU (get the Russian version of my site), by the time it get's to my MVC controller it's reset to the language back to en

public PartialViewResult Navigation()
{
    //en
    var language = Sitecore.Context.Language;
}

I know that sitecore does set this at some point because if I add a HTTP pipeline I can see Sitecore.Context.Language as ru-RU:

public class LanguageResolver
{
    public void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
    {
        //ru-RU
        var language = Sitecore.Context.Language;
    }
}

Registered:

<processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
<processor type="namespace.LanguageResolver,RR.Web.Sc.Extensions" patch:source="Languages.config"/>

as recommended on the sitecore blog post

I did some digging into the sitecore dlls and I noticed that Sitcore.Context.Language is basically a wrapper for HttpContext.Current.Items["sc_Language"]. When I inspect this I see the same results (i.e. in the pipeline it's ru-RU in the controller it's en)

So something somewhere is turning this HttpContext Item to "en".

As an experiment I dropped a new variable into the Items collection. I can then see this again (in it's correct state) in Controller, so the Items collection is getting passed into the controller correctly.

I didn't originally develop this site but I can't see anywhere in the code base that changes this language variable.

Has anyone else experienced this? Anything I've missed? Anything I need to configure, check?

The sitecore docs (as usual) are pretty woeful on this subject.


To follow up on the comment from @jammykam:

I can see the lang cookie the Response Set-Cookie: redrow#lang=ru-RU; path=/ and the languages are set up:

enter image description here

enter image description here

Derwin answered 4/11, 2015 at 12:32 Comment(11)
Just to be sure something funky is not happening, can you try clearing your cookies (or check them and make sure website#lang=ru-RU). Have you added ru-RU to Sitecore? Does your local system support it?Concord
@jammykam, added details above. I'm intrigued if anyone has view'd this in an MVC environment before? I'm guessing it's worked for someone, somewhere?Derwin
Can you move your language resolver before item resolver? Default order in Sitecore is: <processor type="Sitecore.Pipelines.HttpRequest.LanguageResolver, Sitecore.Kernel"/> other procesors ... and after <processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>Dab
The language resolver isn't actually doing anything at the moment though, I'm just using it as an intercept to see what the language is set to further up the pipeline. @sitecoreclimber Now that said it might be interesting to move it around in the pipeline...mmmDerwin
Just check if is working and if yes I will move my comment like an answerDab
Moving the pipeline around hasn't really helped. As far as I can tell the language is correct in all the httpRequestBegin pipelines. I even overrode a few to make sure that the standard one's weren't changing this.Derwin
And just to be sure, the language that has been installed is ru-RU and not just ru? Did you add the language via the Sitecore Control Panel?Concord
yes and yes @ConcordDerwin
I've given up, I'm implementing my own and storing it in the context items myself. I wanted a non-standard location for the language anyway.Derwin
You can open User Manager and set up languages that you prefer per users. It will work for Content Editor. However in you controller language is get from site context. Go to your site configuration and set language that you need .Eustacia
Are you logged with a sitecore user at the same time ?Journalize
G
0

There are a few places in code where Language setter is used OOB in Sitecore 10:

enter image description here

Even though callees from Sitecore.MVC namespace on top of suspected list, I'd recommend to debug Sitecore.Context.Language setter by restoring PDBs and loading them to debugger to get the actual culprit. You'll also see the mechanics/motivation behind the language-switch logic.

Even though getter might be optimized, you still could disable optimizations to ease debugging.

Gladdie answered 12/2, 2021 at 16:1 Comment(1)
I haven't worked on sitecore for about 6 years so I can't really upvote or accept this answer as I don't know if it's helpful or not. So I'll have to see what other people thinkDerwin

© 2022 - 2024 — McMap. All rights reserved.