Answers.Body: <p>In a project I'm currently using <code>define('CRAFT_LOCALE', 'nl_be');</code> inside index.php to set it based on the request URI.</p>
  
  <p>I imagine this should also work by getting a parameter from the request and setting it based on that.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-31T20:27:00.533Z
Answers.Text: 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.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-31T20:31:09.607Z
Answers.Text: No luck, unfortunately. The controller action ignored the `define`, and just fell back to English. Thanks anyway!
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-31T20:22:50.397Z
Answers.LastActivityDate: 2017-07-31T20:22:50.397Z
Answers.UserDisplayName: Rias
Answers.Body: <p>I think I got this resolved the <em>proper</em> way... looks like you just need to <strong>make sure that your AJAX calls are localized.</strong></p>
  
  <p>In my main <code>_layout</code> template:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;script type="text/javascript"&gt;
  window.siteUrl = "{{ siteUrl }}";
  &lt;/script&gt;
  </code></pre>
  
  <p>Then in my JS, I have this:</p>
  
  <pre class="lang-javascript prettyprint-override"><code>$.post(
      siteUrl + 'actions/myPlugin/myAction',
      ...
  );
  </code></pre>
  
  <p>As long as your <code>siteUrl</code> is being localized properly, it should trickle down into the JS... and the localized AJAX path can then load a properly localized template.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-01T05:42:58.730Z
Answers.Text: 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?
Answers.UserDisplayName: mrsmn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-01T05:53:59.297Z
Answers.Text: Yes, exactly. Include the `en` or `fr` in the AJAX URL.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-31T20:57:35.603Z
Answers.LastActivityDate: 2017-09-21T17:06:22.430Z
Answers.LastEditDate: 2017-09-21T17:06:22.430Z
Answers.UserDisplayName: Lindsey D
Body: <p>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.</p>
  
  <p>It's possible to get the current locale like this...</p>
  
  <pre><code>craft()-&gt;getLanguage()
  </code></pre>
  
  <p>Is there a way to <strong>set</strong> the current locale in PHP?</p>
  
Tags: <plugin-development><localization><ajax>
Title: Possible to change locale in PHP?
ThreadText: # Possible to change locale in PHP?
  
  (Asked by: username_0 on 2017-07-31)
  
  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?
  
  **Tags:** plugin-development, localization, ajax
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-07-31. Score: 5 | Accepted answer
  
  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:
  
  ```html
  <script type="text/javascript">
  window.siteUrl = "{{ siteUrl }}";
  </script>
  
  ```
  
  Then in my JS, I have this:
  
  ```javascript
  $.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.
  
  > **Comments:**
  > **username_2**: 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?  
  > **username_0**: Yes, exactly. Include the `en` or `fr` in the AJAX URL.  
  
  ---
  
  ### Answer by username_1 on 2017-07-31. Score: 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.
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_0**: No luck, unfortunately. The controller action ignored the `define`, and just fell back to English. Thanks anyway!  
  
  ---
  
  