Kennis Enable Langur JavaScript only when needed

Enable Langur JavaScript only when needed

When you want to use Langur, you have to add a bit of JavaScript into your page. However, I wanted to include that script only when the application was called by Langur.

We first tried to use an extra query parameter (langur=true), and add that to the application URL in the branch configuration of Langur. Then I could use something like the following to include the script in my Freemarker template:

<#if (RequestParameters.langur???string == 'true')>
… Langur script …

This, however, caused all sorts of trouble. When a request gets redirected or when you click on a link, the extra query parameter is lost and you have to re-enter it manually.

Then I realized that when Langur calls the application, it sets the locale to the special LRF locale - in our case: zxx. We use the Spring SessionLocaleResolver, so you can change the locale by adding locale=zxx to the query string: our application URL in the branch configuration ends in ?locale=zxx.

Now I added a new message key to my default message bundle:


I regenerated the LRF file, which of course contains something completely different:


And now I could add the script to the template as:

<#assign langurMode><@spring.message "langur.mode" /></#assign>
<#if (langurMode != 'false')>
… Langur script …

Without any additional query parameters, the script only shows up when you use the LRF locale for Langur!