5

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...

craft()->getLanguage()

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

5

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 }}";
</script>

Then in my JS, I have this:

$.post(
    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.

|improve this answer|||||
  • 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 Aug 1 '17 at 5:42
  • 1
    Yes, exactly. Include the en or fr in the AJAX URL. – Lindsey D Aug 1 '17 at 5:53
2

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.

|improve this answer|||||
  • 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 Jul 31 '17 at 20:27
  • No luck, unfortunately. The controller action ignored the define, and just fell back to English. Thanks anyway! – Lindsey D Jul 31 '17 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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