Try our conversational search powered by Generative AI!

EpiFind, AJAX and language


I'm little puzzled here. I have a multilingual MVC website which uses AJAX and EpiFind to search and display results for user. It works find on single language, the default language, but when I change language I stumbled to two problems.

Let's say I change from fi (default) to sv. Page is reloaded and all the text are in Swedish. User enters search phrase which is posted to server using AJAX. First problem comes here. Server thinks that client's language is still Finnish (ContentLanguage.PreferredCulture = Finnish) because AJAX call doesn't contain language prefix. I managed to tackle this problem by rendering user's language in html element's lang attribute and post this value in every AJAX call.

When AJAX call reaches the server I figure out desired language and feed it to EpiFind.

var language = Language.GetAll().First(f => f.FieldSuffix == HttpContext.Current.Request["lang"]);
var pages = SearchClient.Instance.Search<FooPage>(language).FilterOnLanguages(new[] { language.FieldSuffix }).Filter(f => f.Something == query).GetContentResult();

Second problem comes here. It seems that EpiFind searches pages in Swedish but returns pages in Finnish! Every page's LanguageID is "fi". What do I need to do to get pages in Swedish (or any other language user has selected)? Or am I doing something completely wrong here?

May 03, 2013 13:01


Your code looks perfectly fine and I did run it and cannot reproduce your problem. Can you verify that the language that you set is really Swedish? 


May 13, 2013 11:25

The thing is that when AJAX call is made EPiServer switches language back to site's default language. This is because there's no language identifier in the URL. ContentLanguage.PreferredCulture changes to Finnish. I solved the problem by writing a little routine that figures out correct language, which is given as parameter in AJAX call, and changes ContentLanguage.PreferredCulture. Like this:


    public class LocalizableServiceBase
        public LocalizableServiceBase()
            var lang = HttpContext.Current.Request["language"] ?? ContentLanguage.PreferredCulture.EnglishName;
            this.Language = Language.GetAll().First(f => f.FieldSuffix == lang || f.Name == lang);

            if (Language.FieldSuffix != ContentLanguage.PreferredCulture.IetfLanguageTag)
                ContentLanguage.PreferredCulture = new CultureInfo(Language.FieldSuffix);

        protected Language Language { get; private set; }
        public string LanguagePrefix { get { return Language.FieldSuffix; } }
        protected CultureInfo CurrentCultureInfo { get { return new CultureInfo(LanguagePrefix); } }


May 13, 2013 11:30
* 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.