EpiServer localization for AJAX pages

Vote:
 

I have a site which uses EpiServer CMS 7.

I have a problem with the language of content which is returned after ajax call. For example, on some page I have a link, after clicking on it the AJAX-request will be sent to the server and content will be returned and inserted in special container. I have following url for AJAX request:

var urlStr= "/Folder1/Ajax/AddSomething.aspx?id=53&epslanguage=en&";

This link always contains the CORRECT language in "epslanguage" parameter. But content, which is returned after ajax call, is always in the default language(Swedish).

I have tried to debug and found that on AddSomething.aspx page I have following globalization settings:

System.Globalization.CultureInfo.CurrentUICulture == "sv";
System.Threading.Thread.CurrentThread.CurrentUICulture == "sv";
EPiServer.Globalization.ContentLanguage.PreferredCulture == "en";

Question: how can we set the correct language for the page content? As I understand, EpiServer knows the correct language, but still use "sv" when I call method

EPiServer.Core.LanguageManager.Instance.Translate(string str);

Thank you all in advance for the help.

#81183
Feb 11, 2014 11:09
Vote:
 

Looking at Translate() from LanguageManager -> it uses current UI culture (which is in your case is "sv").

return this.Translate(key, Thread.CurrentThread.CurrentUICulture.IetfLanguageTag);

    

 

Try to "restore" desired culture just before calling Translate().

AFAIK EPiServer content language really does not influence resource translation language.

#81188
Feb 11, 2014 11:39
Vote:
 

EPiServer's current language context is lost when posting using ajax. Here is how you can set language context inside a web api call:

var language = ControllerContext.RouteData.Values["language"].ToString();
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
            
EPiServer.BaseLibrary.Context.Current["EPiServer:ContentLanguage"] = new CultureInfo(language);

    

#81192
Feb 11, 2014 12:12
Vote:
 

Thank you all for the help. I have overridden InitializeCulture() method in the base class(for ajax pages) and used following code:

protected override void InitializeCulture()
{ base.InitializeCulture(); Thread.CurrentThread.CurrentUICulture = EPiServer.Globalization.ContentLanguage.PreferredCulture; Thread.CurrentThread.CurrentCulture = EPiServer.Globalization.ContentLanguage.PreferredCulture; }

   

Property 

EPiServer.BaseLibrary.Context.Current["EPiServer:ContentLanguage"]

     contain the correct culture even without any changes.

#81204
Edited, Feb 11, 2014 14:27
Vote:
 

Running into the same issue. Where do you override InitializeCulture()?

#111603
Oct 16, 2014 12:41
Vote:
 

If you are using WebForms, it's an override of PageBase.

If you are using MVC, take a look at http://stackoverflow.com/questions/21160599/custom-language-handling-episerver

#111610
Oct 16, 2014 13:56
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.