Try our conversational search powered by Generative AI!

Dave Beattie
Mar 16, 2021
  1904
(1 votes)

Episerver AJAX and standard MVC calls with Localization

This post is inspired by Stefan’s solution here.

Recently I was involved in a project replacing a custom translation solution on a client’s site with a more standard Episerver solution using Episerver Languages to translate content and DblocalizationProvider for static text.

One issue we ran into was with API calls and AJAX requests.  The site is an ecommerce site and there were several places where requests where directed through a standard mvc controller/action route, rather than going through Episerver content routing.  This proved problematic when attempting to return translated content such as error messages as we found the current culture was lost and the site was using the default culture. 

We settled on using an ActionFilter to retrieve the language code from the url.   Initially we had used Stefan’s implementation tailored to AJAX calls.  However, we ran into further issues as a payment gateway implementation was using an api method as it’s return url in order to process the payment status and either complete the order or return an error message.  This we created something that would retrieve the language code from any request.   

   

 public class StandardMvcRequestSetLanguageAttribute : ActionFilterAttribute
   {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext?.RequestContext?.HttpContext.Request == null)
            {
                return;
            }

            HttpRequestBase httpRequest = filterContext.RequestContext.HttpContext.Request;

            if (filterContext.RequestContext.RouteData.Values["language"] != null)
            {
                var lang = filterContext.RequestContext.RouteData.Values["language"].ToString();

                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
            }         
        }
    }

  

We then discovered that Episerver wasn’t supported localized urls for non content paths (it’s perfectly fine for links to content that goes through Episerver’s routing.)  Therefore we also registered a basic path with the language code in.  Our ajax calls and the return urls for our payment gateway were now able to return translated error or success messages.

 

 

Mar 16, 2021

Comments

Johnny Mullaney
Johnny Mullaney Mar 22, 2021 05:08 PM

Nice post David!

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog