<p>This is the scenario: I need the translations strings in both Twig and JS (For React). Of course I'm covered in the <a href="https://craftcms.com/support/static-translations" rel="nofollow noreferrer">Twig part.</a>, and I don't want to maintain two different files (php and json). So I've created a custom module, where I can read the files, like:</p>

<pre><code>public function getTranslations($language, $translationsFileName = 'site')
{
    if (file_exists(CRAFT_BASE_PATH . '/translations/' . $language . '/' . $translationsFileName . '.php')) {
        return require_once(CRAFT_BASE_PATH . '/translations/' . $language . '/' . $translationsFileName . '.php');
    }
}
</code></pre>

<p>Usage:</p>

<pre><code>{{ craft.translations.getTranslations(craft.locale) | json_encode | raw }}
</code></pre>

<p>It works, but, is this a good way? Is there a default Craft/Yii way?</p>

CC BY-SA 4.0
2018-07-11T19:51:12.557Z
Would it be an option for you to do it like Craft does it in the CP (I use the same way in my frontend) instead of just serializing the entire array?
Robin Schambach
CC BY-SA 4.0
2018-07-12T12:16:06.947Z
Like {{ "var" | translate }} you mean? Sorry, I didn't understand.
Jorge Anzola
CC BY-SA 4.0
2018-07-12T12:29:10.873Z
No I mean by using the `RegisterTranslation` https://docs.craftcms.com/api/v3/craft-web-view.html#method-registertranslations function in your frontend. You only need to define a Craft object and copy the `Craft.t` function
Robin Schambach
CC BY-SA 4.0
2023-06-22T07:48:28.310Z
@RobinSchambach do you have an example of how to use registerTranslations?  I don't understand what the $category and $messages arguments are.  I tried passing in site handles and strings to translate but no go.
biko
CC BY-SA 4.0
2023-06-22T13:01:02.257Z
@biko This comes from Yii2 https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n You have different translation categories, for example `site` for everything site related or `app` )that's yii2 internal category. The message argument is the thing you want to translate.
For example `registerTranslations('site', ['foo', 'bar'))` would include translations for the word `foo` and `bar` in the category `site` (default one)
Robin Schambach
CC BY-SA 4.0
2023-06-23T00:22:05.177Z
@RobinSchambach thank you!  I think that this might not work well because I want the whole object map available.  I've worked around it and made the php object map available in element-api.php.  Would be nice if there was an official way to do this.
biko
CC BY-SA 4.0
2023-06-23T06:03:06.043Z
I usually grab all translations and return them all in my JS driven projects too. Just not via elements-API but via YII2 REST API.
Robin Schambach
<plugin-development><templating><localization>
Access array from the static translation file
# Access array from the static translation file

(Asked by: username_0 on 2018-07-11)

This is the scenario: I need the translations strings in both Twig and JS (For React). Of course I'm covered in the Twig part., and I don't want to maintain two different files (php and json). So I've created a custom module, where I can read the files, like:
public function getTranslations($language, $translationsFileName = 'site')
{
    if (file_exists(CRAFT_BASE_PATH . '/translations/' . $language . '/' . $translationsFileName . '.php')) {
        return require_once(CRAFT_BASE_PATH . '/translations/' . $language . '/' . $translationsFileName . '.php');
    }
}

Usage:
{{ craft.translations.getTranslations(craft.locale) | json_encode | raw }}

It works, but, is this a good way? Is there a default Craft/Yii way?

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

> **Comments:**
> **username_1**: Would it be an option for you to do it like Craft does it in the CP (I use the same way in my frontend) instead of just serializing the entire array?  
> **username_0**: Like {{ "var" | translate }} you mean? Sorry, I didn't understand.  
> **username_1**: No I mean by using the `RegisterTranslation` https://docs.craftcms.com/api/v3/craft-web-view.html#method-registertranslations function in your frontend. You only need to define a Craft object and copy the `Craft.t` function  
> **username_2**: @RobinSchambach do you have an example of how to use registerTranslations?  I don't understand what the $category and $messages arguments are.  I tried passing in site handles and strings to translate but no go.  
> **username_1**: @username_2 This comes from Yii2 https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n You have different translation categories, for example `site` for everything site related or `app` )that's yii2 internal category. The message argument is the thing you want to translate. For example `registerTranslations('site', ['foo', 'bar'))` would include translations for the word `foo` and `bar` in the category `site` (default one)  
> **username_2**: @RobinSchambach thank you!  I think that this might not work well because I want the whole object map available.  I've worked around it and made the php object map available in element-api.php.  Would be nice if there was an official way to do this.  
> **username_1**: I usually grab all translations and return them all in my JS driven projects too. Just not via elements-API but via YII2 REST API.  

---

## 0 answers

