I've got a plugin which is defaulting to English during AJAX calls, but I need it to stick to whatever the current language is. I can send the current locale through via AJAX data, but I'm not sure how to change the locale in PHP.

It's possible to get the current locale like this...


Is there a way to set the current locale in PHP?

2 Answers 2


I think I got this resolved the proper way... looks like you just need to make sure that your AJAX calls are localized.

In my main _layout template:

<script type="text/javascript">
window.siteUrl = "{{ siteUrl }}";

Then in my JS, I have this:

    siteUrl + 'actions/myPlugin/myAction',

As long as your siteUrl is being localized properly, it should trickle down into the JS... and the localized AJAX path can then load a properly localized template.

  • Just to make sure i understood what you ment, if I'm using example.com/en & example.com/fr I should do the ajax request to those actual urls instead of example.com?
    – mrsmn
    Commented Aug 1, 2017 at 5:42
  • 1
    Yes, exactly. Include the en or fr in the AJAX URL.
    – Lindsey D
    Commented Aug 1, 2017 at 5:53

In a project I'm currently using define('CRAFT_LOCALE', 'nl_be'); inside index.php to set it based on the request URI.

I imagine this should also work by getting a parameter from the request and setting it based on that.

  • Interesting, thanks! My understanding is that this only works within the context of the index.php file, but I'll give it a quick try and report back.
    – Lindsey D
    Commented Jul 31, 2017 at 20:27
  • No luck, unfortunately. The controller action ignored the define, and just fell back to English. Thanks anyway!
    – Lindsey D
    Commented Jul 31, 2017 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.