Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-07-11T19:51:12.557Z
Comments.Text: 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?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-07-12T12:16:06.947Z
Comments.Text: Like {{ "var" | translate }} you mean? Sorry, I didn't understand.
Comments.UserDisplayName: Jorge Anzola
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-07-12T12:29:10.873Z
Comments.Text: 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
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-22T07:48:28.310Z
Comments.Text: @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.
Comments.UserDisplayName: biko
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-22T13:01:02.257Z
Comments.Text: @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)
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-23T00:22:05.177Z
Comments.Text: @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.
Comments.UserDisplayName: biko
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-23T06:03:06.043Z
Comments.Text: 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.
Comments.UserDisplayName: Robin Schambach
Tags: <plugin-development><templating><localization>
Title: Access array from the static translation file
ThreadText: # 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.](https://craftcms.com/support/static-translations), 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**: @username_1 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**: @username_1 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
  
  