<p>I tried solving my problem by passing the additional parameters to the <code>Craft::t()</code> function mentioned by Brad. For static translations this worked fine, but it still left me with wrong formatted dates inside of twig templates, that were rendered by my transformer.</p>

<p>I found another solution to the problem: Setting the current language globally on the craft instance by using <code>craft()-&gt;setLanguage()</code>.</p>

<p>Now my code looks like this inside <code>craft/config/elementapi.php</code>:</p>

<pre><code>return [
    'endpoints' =&gt; function () {
        'api/galleries' =&gt; function () {
            $locale = craft()-&gt;request-&gt;getQuery('locale', craft()-&gt;getLanguage());
            craft()-&gt;setLanguage($locale);

            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; [
                    'section' =&gt; 'galleries',
                    'locale' =&gt; $locale
                ],
                'transformer' =&gt; new GalleryTransformer()
            ];
        }
    }
];
</code></pre>

<p>If you set the language globally like this, <code>Craft:t()</code> works like expected, without specifying the additional parameters.</p>

CC BY-SA 3.0
2016-09-03T14:04:59.927Z
2016-09-03T14:04:59.927Z
periodical
<p><a href="https://github.com/pixelandtonic/ElementAPI" rel="nofollow"><code>Craft::t()</code></a> has a whole bunch of parameters you can pass into it.</p>

<pre><code>public static function t($message, $variables = array(), $source = null, $language = null, $category = 'craft')
</code></pre>

<p>It sounds like what you're looking for is the 4th one.</p>

<pre><code>Craft::t('my string', null, null, 'es')
</code></pre>

<p>Where <code>'es'</code> would be a target locale other than your main one.</p>

CC BY-SA 3.0
2016-09-02T03:16:11.887Z
2016-09-02T03:16:11.887Z
Brad Bell
<pre><code>//get element in the specifict language site        
function langEntry($entry_id) {
    return GlobalSet::find()-&gt;id($entry_id)-&gt;site("site_name_lang")-&gt;one();
}
</code></pre>

<p>Can replace GlobaSet for Entry, Section, etc</p>

CC BY-SA 4.0
2018-10-15T09:57:28.670Z
2018-10-15T10:14:10.450Z
2018-10-15T10:14:10.450Z
Alejandro Aranda
<p>I’m using the <a href="https://github.com/pixelandtonic/ElementAPI" rel="nofollow">Element API</a> plugin to retrieve my entries as JSON. It is available under the <code>/api</code> route. For getting entries in a different locale I pass a <code>locale</code> query param and use it inside the ElementQuery.</p>

<p>All works fine so far, BUT when I want to use <code>Craft::t</code> inside of a transformer, I get the translations from the main locale, because this is where my <code>/api</code> namespace lives.</p>

<p>Is it possible to modify the <code>locale</code> that Craft is set to, from inside the Element API plugin?</p>

<plugin-development><localization><plugin-elementapi>
Modify Craft locale inside Element API plugin?
# Modify Craft locale inside Element API plugin?

(Asked by: username_0 on 2016-09-02)

I’m using the [Element API](https://github.com/pixelandtonic/ElementAPI) plugin to retrieve my entries as JSON. It is available under the `/api` route. For getting entries in a different locale I pass a `locale` query param and use it inside the ElementQuery.

All works fine so far, BUT when I want to use `Craft::t` inside of a transformer, I get the translations from the main locale, because this is where my `/api` namespace lives.

Is it possible to modify the `locale` that Craft is set to, from inside the Element API plugin?

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

---

## 3 answers

### Answer by username_0 on 2016-09-03. Score: 1 | Accepted answer

I tried solving my problem by passing the additional parameters to the Craft::t() function mentioned by Brad. For static translations this worked fine, but it still left me with wrong formatted dates inside of twig templates, that were rendered by my transformer.
I found another solution to the problem: Setting the current language globally on the craft instance by using craft()->setLanguage().
Now my code looks like this inside craft/config/elementapi.php:
return [
    'endpoints' => function () {
        'api/galleries' => function () {
            $locale = craft()->request->getQuery('locale', craft()->getLanguage());
            craft()->setLanguage($locale);

            return [
                'elementType' => 'Entry',
                'criteria' => [
                    'section' => 'galleries',
                    'locale' => $locale
                ],
                'transformer' => new GalleryTransformer()
            ];
        }
    }
];

If you set the language globally like this, Craft:t() works like expected, without specifying the additional parameters.


---

### Answer by username_1 on 2016-09-02. Score: 1

Craft::t() has a whole bunch of parameters you can pass into it.
public static function t($message, $variables = array(), $source = null, $language = null, $category = 'craft')

It sounds like what you're looking for is the 4th one.
Craft::t('my string', null, null, 'es')

Where 'es' would be a target locale other than your main one.


---

### Answer by username_2 on 2018-10-15. Score: 1

//get element in the specifict language site        
function langEntry($entry_id) {
    return GlobalSet::find()->id($entry_id)->site("site_name_lang")->one();
}

Can replace GlobaSet for Entry, Section, etc


---

