<p>I don't know if this was the problem you were having but I was not including the site's first URL segment like <code>/en/</code> (in your example) with the controller action URL. So you'd want to post to <code>/en/actions/pluginHandle/controllerClass/actionMethod</code> to get the English site back when using <code>Craft::$app-&gt;getSites()-&gt;getCurrentSite()</code> in your plugin.</p>

<p>I posted a more thorough answer to the same problem in another ticket:
<a href="https://craftcms.stackexchange.com/a/33941/4433">https://craftcms.stackexchange.com/a/33941/4433</a></p>

CC BY-SA 4.0
2020-03-17T16:41:59.900Z
2020-03-17T16:41:59.900Z
Ryan
<p>For my form plugin I'm using a global set of fields. These can be set in 2 languages <code>default</code> and <code>en</code>. </p>

<p>Problem is whenever I try to output them I cant dynamically make them use the current site locale/multi-site.</p>

<p>This is the current code:</p>

<pre><code>$localeHandle = Craft::$app-&gt;getSites()-&gt;getCurrentSite()-&gt;handle;
    // form texts
    $entries = GlobalSet::find()
        -&gt;siteId('*')
        -&gt;site($localeHandle)
        -&gt;handle('popupContact')
        -&gt;asArray()
        -&gt;one();
</code></pre>

<p>If <code>$localeHandle</code> is set to either <code>default</code> or <code>en</code> it works. I cant figure out how to get the handle though. Documentation doesn't state anything either.</p>

<p>Edit:
<code>Craft::$app-&gt;getSites()-&gt;getCurrentSite()-&gt;handle;</code> returns the default site handle.</p>

<plugin-development><localization><php>
How to get current site (locale) api / plugin
# How to get current site (locale) api / plugin

(Asked by: username_0 on 2019-10-14)

For my form plugin I'm using a global set of fields. These can be set in 2 languages default and en. 
Problem is whenever I try to output them I cant dynamically make them use the current site locale/multi-site.
This is the current code:
$localeHandle = Craft::$app->getSites()->getCurrentSite()->handle;
    // form texts
    $entries = GlobalSet::find()
        ->siteId('*')
        ->site($localeHandle)
        ->handle('popupContact')
        ->asArray()
        ->one();

If $localeHandle is set to either default or en it works. I cant figure out how to get the handle though. Documentation doesn't state anything either.
Edit:
Craft::$app->getSites()->getCurrentSite()->handle; returns the default site handle.

**Tags:** plugin-development, localization, php


---

## 1 answers

### Answer by username_1 on 2020-03-17. Score: 3

I don't know if this was the problem you were having but I was not including the site's first URL segment like `/en/` (in your example) with the controller action URL. So you'd want to post to `/en/actions/pluginHandle/controllerClass/actionMethod` to get the English site back when using `Craft::$app->getSites()->getCurrentSite()` in your plugin.

I posted a more thorough answer to the same problem in another ticket:
<https://craftcms.stackexchange.com/a/33941/4433>


---

