How does Gutenberg handle translations in React?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
7
down vote

favorite
1












I was going through the source code of Gutenberg, for e.g. this and cannot understand how they handle translations...



They do import this import __ from '@wordpress/i18n' and then in the source code they use this speak( __( 'Block settings closed' ) );.



Can anybody tell me how they manage these translations within ReactJS to be collected in a normal .po file?



I suppose they have some build process which goes through all of the files, including JS and collects them, but now sure.







share|improve this question




























    up vote
    7
    down vote

    favorite
    1












    I was going through the source code of Gutenberg, for e.g. this and cannot understand how they handle translations...



    They do import this import __ from '@wordpress/i18n' and then in the source code they use this speak( __( 'Block settings closed' ) );.



    Can anybody tell me how they manage these translations within ReactJS to be collected in a normal .po file?



    I suppose they have some build process which goes through all of the files, including JS and collects them, but now sure.







    share|improve this question
























      up vote
      7
      down vote

      favorite
      1









      up vote
      7
      down vote

      favorite
      1






      1





      I was going through the source code of Gutenberg, for e.g. this and cannot understand how they handle translations...



      They do import this import __ from '@wordpress/i18n' and then in the source code they use this speak( __( 'Block settings closed' ) );.



      Can anybody tell me how they manage these translations within ReactJS to be collected in a normal .po file?



      I suppose they have some build process which goes through all of the files, including JS and collects them, but now sure.







      share|improve this question














      I was going through the source code of Gutenberg, for e.g. this and cannot understand how they handle translations...



      They do import this import __ from '@wordpress/i18n' and then in the source code they use this speak( __( 'Block settings closed' ) );.



      Can anybody tell me how they manage these translations within ReactJS to be collected in a normal .po file?



      I suppose they have some build process which goes through all of the files, including JS and collects them, but now sure.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 22 at 11:19









      Community♦

      1




      1










      asked Aug 22 at 5:47









      Bologer

      383




      383




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          In the Gutenberg's GitHub repository you can see the source of the i18n package that is used. In this source, you'll see Jed getting imported (line 4 of gutenberg/packages/i18n/src/index.js) and then being used for most of the translation tasks under the hood.



          Jed introduces the "Gettext Style i18n for Modern JavaScript Apps" (or at least so it says on their site).



          Your question is for the .po files. Jed explains on their site:




          There are quite a few available .po to .json converters out there. Gettext .po files are standard output from most decent translation companies, as it's an old standard.



          I currently use: po2json



          However, I'd like to add this functionality to a separate Jed module in a future version.




          However, this does not seem to apply here.



          Further digging turns out, setLocaleData( data: Object, domain: string ) is used to pass the translations, in a manner like so:



          $locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
          wp_add_inline_script(
          'wp-i18n',
          'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );'
          );


          (gutenberg_get_jed_locale_data( $domain ) being more or less a wrapper for get_translations_for_domain( $domain ) )



          So it seems WordPress retrieves the translation data via PHP and then passes it to Jed. Jed itself does not seem to load any translation files.



          The package's readme also explains how to properly generate the .pot file containing the localized strings.




          The package also includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery since at the moment, WordPress.org is not capable of parsing strings directly from JavaScript files.



          npx pot-to-php languages/myplugin.pot languages/myplugin-translations.php text-domain






          share|improve this answer






















          • Does it mean that Gutenberg stores translations in some window property as JSON loaded via wp_add_inline_script by PHP and then retrieves it on React side and passes it to Jed? ... and Jed does further magic?
            – Bologer
            Aug 22 at 6:31










          • @Bologer Not necessarily a window property, but yes. PHP retrieves the values and passes them to JS via wp_add_inline_script
            – kero
            Aug 22 at 6:42






          • 2




            you should expand you answer with the information you added in the comment to mine. That comment actually seems to be more in line with what the OP looks for
            – Mark Kaplun
            Aug 22 at 6:53










          • @MarkKaplun Thanks, just edited it in
            – kero
            Aug 22 at 6:59

















          up vote
          2
          down vote













          At least for now, as long as there is no better automated process, I would suggest not to generate .pot files from JS at all.



          As @kero explains in his answer right now GB translations are being passed as a kind of blob array from the .mo file into JS. This workflow will break all localization tampering plugins which rely on filtering the results of __ and associates. A better workflow will be to have an explicit generation of the blob array from strings being translated with __ calls, similar to how you would do JS translation in non GB context. This will also solve the issue of generating .pot files.



          What is missing here is some automated process that will run over JS files and produce he relevant PHP code, which in turn can be analyzed by tools like poedit.






          share|improve this answer




















          • About your last paragraph: "includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery"
            – kero
            Aug 22 at 6:46






          • 1




            nice starting point, only part left is to auto generate the call to wp_add_inline_script, as right now it probably just generate php just for the benefit of pot generation, but do not actually use it (my guess)
            – Mark Kaplun
            Aug 22 at 6:48











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "110"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f312126%2fhow-does-gutenberg-handle-translations-in-react%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          In the Gutenberg's GitHub repository you can see the source of the i18n package that is used. In this source, you'll see Jed getting imported (line 4 of gutenberg/packages/i18n/src/index.js) and then being used for most of the translation tasks under the hood.



          Jed introduces the "Gettext Style i18n for Modern JavaScript Apps" (or at least so it says on their site).



          Your question is for the .po files. Jed explains on their site:




          There are quite a few available .po to .json converters out there. Gettext .po files are standard output from most decent translation companies, as it's an old standard.



          I currently use: po2json



          However, I'd like to add this functionality to a separate Jed module in a future version.




          However, this does not seem to apply here.



          Further digging turns out, setLocaleData( data: Object, domain: string ) is used to pass the translations, in a manner like so:



          $locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
          wp_add_inline_script(
          'wp-i18n',
          'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );'
          );


          (gutenberg_get_jed_locale_data( $domain ) being more or less a wrapper for get_translations_for_domain( $domain ) )



          So it seems WordPress retrieves the translation data via PHP and then passes it to Jed. Jed itself does not seem to load any translation files.



          The package's readme also explains how to properly generate the .pot file containing the localized strings.




          The package also includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery since at the moment, WordPress.org is not capable of parsing strings directly from JavaScript files.



          npx pot-to-php languages/myplugin.pot languages/myplugin-translations.php text-domain






          share|improve this answer






















          • Does it mean that Gutenberg stores translations in some window property as JSON loaded via wp_add_inline_script by PHP and then retrieves it on React side and passes it to Jed? ... and Jed does further magic?
            – Bologer
            Aug 22 at 6:31










          • @Bologer Not necessarily a window property, but yes. PHP retrieves the values and passes them to JS via wp_add_inline_script
            – kero
            Aug 22 at 6:42






          • 2




            you should expand you answer with the information you added in the comment to mine. That comment actually seems to be more in line with what the OP looks for
            – Mark Kaplun
            Aug 22 at 6:53










          • @MarkKaplun Thanks, just edited it in
            – kero
            Aug 22 at 6:59














          up vote
          4
          down vote



          accepted










          In the Gutenberg's GitHub repository you can see the source of the i18n package that is used. In this source, you'll see Jed getting imported (line 4 of gutenberg/packages/i18n/src/index.js) and then being used for most of the translation tasks under the hood.



          Jed introduces the "Gettext Style i18n for Modern JavaScript Apps" (or at least so it says on their site).



          Your question is for the .po files. Jed explains on their site:




          There are quite a few available .po to .json converters out there. Gettext .po files are standard output from most decent translation companies, as it's an old standard.



          I currently use: po2json



          However, I'd like to add this functionality to a separate Jed module in a future version.




          However, this does not seem to apply here.



          Further digging turns out, setLocaleData( data: Object, domain: string ) is used to pass the translations, in a manner like so:



          $locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
          wp_add_inline_script(
          'wp-i18n',
          'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );'
          );


          (gutenberg_get_jed_locale_data( $domain ) being more or less a wrapper for get_translations_for_domain( $domain ) )



          So it seems WordPress retrieves the translation data via PHP and then passes it to Jed. Jed itself does not seem to load any translation files.



          The package's readme also explains how to properly generate the .pot file containing the localized strings.




          The package also includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery since at the moment, WordPress.org is not capable of parsing strings directly from JavaScript files.



          npx pot-to-php languages/myplugin.pot languages/myplugin-translations.php text-domain






          share|improve this answer






















          • Does it mean that Gutenberg stores translations in some window property as JSON loaded via wp_add_inline_script by PHP and then retrieves it on React side and passes it to Jed? ... and Jed does further magic?
            – Bologer
            Aug 22 at 6:31










          • @Bologer Not necessarily a window property, but yes. PHP retrieves the values and passes them to JS via wp_add_inline_script
            – kero
            Aug 22 at 6:42






          • 2




            you should expand you answer with the information you added in the comment to mine. That comment actually seems to be more in line with what the OP looks for
            – Mark Kaplun
            Aug 22 at 6:53










          • @MarkKaplun Thanks, just edited it in
            – kero
            Aug 22 at 6:59












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          In the Gutenberg's GitHub repository you can see the source of the i18n package that is used. In this source, you'll see Jed getting imported (line 4 of gutenberg/packages/i18n/src/index.js) and then being used for most of the translation tasks under the hood.



          Jed introduces the "Gettext Style i18n for Modern JavaScript Apps" (or at least so it says on their site).



          Your question is for the .po files. Jed explains on their site:




          There are quite a few available .po to .json converters out there. Gettext .po files are standard output from most decent translation companies, as it's an old standard.



          I currently use: po2json



          However, I'd like to add this functionality to a separate Jed module in a future version.




          However, this does not seem to apply here.



          Further digging turns out, setLocaleData( data: Object, domain: string ) is used to pass the translations, in a manner like so:



          $locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
          wp_add_inline_script(
          'wp-i18n',
          'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );'
          );


          (gutenberg_get_jed_locale_data( $domain ) being more or less a wrapper for get_translations_for_domain( $domain ) )



          So it seems WordPress retrieves the translation data via PHP and then passes it to Jed. Jed itself does not seem to load any translation files.



          The package's readme also explains how to properly generate the .pot file containing the localized strings.




          The package also includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery since at the moment, WordPress.org is not capable of parsing strings directly from JavaScript files.



          npx pot-to-php languages/myplugin.pot languages/myplugin-translations.php text-domain






          share|improve this answer














          In the Gutenberg's GitHub repository you can see the source of the i18n package that is used. In this source, you'll see Jed getting imported (line 4 of gutenberg/packages/i18n/src/index.js) and then being used for most of the translation tasks under the hood.



          Jed introduces the "Gettext Style i18n for Modern JavaScript Apps" (or at least so it says on their site).



          Your question is for the .po files. Jed explains on their site:




          There are quite a few available .po to .json converters out there. Gettext .po files are standard output from most decent translation companies, as it's an old standard.



          I currently use: po2json



          However, I'd like to add this functionality to a separate Jed module in a future version.




          However, this does not seem to apply here.



          Further digging turns out, setLocaleData( data: Object, domain: string ) is used to pass the translations, in a manner like so:



          $locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
          wp_add_inline_script(
          'wp-i18n',
          'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );'
          );


          (gutenberg_get_jed_locale_data( $domain ) being more or less a wrapper for get_translations_for_domain( $domain ) )



          So it seems WordPress retrieves the translation data via PHP and then passes it to Jed. Jed itself does not seem to load any translation files.



          The package's readme also explains how to properly generate the .pot file containing the localized strings.




          The package also includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery since at the moment, WordPress.org is not capable of parsing strings directly from JavaScript files.



          npx pot-to-php languages/myplugin.pot languages/myplugin-translations.php text-domain







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 22 at 6:59

























          answered Aug 22 at 6:07









          kero

          2,6471823




          2,6471823











          • Does it mean that Gutenberg stores translations in some window property as JSON loaded via wp_add_inline_script by PHP and then retrieves it on React side and passes it to Jed? ... and Jed does further magic?
            – Bologer
            Aug 22 at 6:31










          • @Bologer Not necessarily a window property, but yes. PHP retrieves the values and passes them to JS via wp_add_inline_script
            – kero
            Aug 22 at 6:42






          • 2




            you should expand you answer with the information you added in the comment to mine. That comment actually seems to be more in line with what the OP looks for
            – Mark Kaplun
            Aug 22 at 6:53










          • @MarkKaplun Thanks, just edited it in
            – kero
            Aug 22 at 6:59
















          • Does it mean that Gutenberg stores translations in some window property as JSON loaded via wp_add_inline_script by PHP and then retrieves it on React side and passes it to Jed? ... and Jed does further magic?
            – Bologer
            Aug 22 at 6:31










          • @Bologer Not necessarily a window property, but yes. PHP retrieves the values and passes them to JS via wp_add_inline_script
            – kero
            Aug 22 at 6:42






          • 2




            you should expand you answer with the information you added in the comment to mine. That comment actually seems to be more in line with what the OP looks for
            – Mark Kaplun
            Aug 22 at 6:53










          • @MarkKaplun Thanks, just edited it in
            – kero
            Aug 22 at 6:59















          Does it mean that Gutenberg stores translations in some window property as JSON loaded via wp_add_inline_script by PHP and then retrieves it on React side and passes it to Jed? ... and Jed does further magic?
          – Bologer
          Aug 22 at 6:31




          Does it mean that Gutenberg stores translations in some window property as JSON loaded via wp_add_inline_script by PHP and then retrieves it on React side and passes it to Jed? ... and Jed does further magic?
          – Bologer
          Aug 22 at 6:31












          @Bologer Not necessarily a window property, but yes. PHP retrieves the values and passes them to JS via wp_add_inline_script
          – kero
          Aug 22 at 6:42




          @Bologer Not necessarily a window property, but yes. PHP retrieves the values and passes them to JS via wp_add_inline_script
          – kero
          Aug 22 at 6:42




          2




          2




          you should expand you answer with the information you added in the comment to mine. That comment actually seems to be more in line with what the OP looks for
          – Mark Kaplun
          Aug 22 at 6:53




          you should expand you answer with the information you added in the comment to mine. That comment actually seems to be more in line with what the OP looks for
          – Mark Kaplun
          Aug 22 at 6:53












          @MarkKaplun Thanks, just edited it in
          – kero
          Aug 22 at 6:59




          @MarkKaplun Thanks, just edited it in
          – kero
          Aug 22 at 6:59












          up vote
          2
          down vote













          At least for now, as long as there is no better automated process, I would suggest not to generate .pot files from JS at all.



          As @kero explains in his answer right now GB translations are being passed as a kind of blob array from the .mo file into JS. This workflow will break all localization tampering plugins which rely on filtering the results of __ and associates. A better workflow will be to have an explicit generation of the blob array from strings being translated with __ calls, similar to how you would do JS translation in non GB context. This will also solve the issue of generating .pot files.



          What is missing here is some automated process that will run over JS files and produce he relevant PHP code, which in turn can be analyzed by tools like poedit.






          share|improve this answer




















          • About your last paragraph: "includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery"
            – kero
            Aug 22 at 6:46






          • 1




            nice starting point, only part left is to auto generate the call to wp_add_inline_script, as right now it probably just generate php just for the benefit of pot generation, but do not actually use it (my guess)
            – Mark Kaplun
            Aug 22 at 6:48















          up vote
          2
          down vote













          At least for now, as long as there is no better automated process, I would suggest not to generate .pot files from JS at all.



          As @kero explains in his answer right now GB translations are being passed as a kind of blob array from the .mo file into JS. This workflow will break all localization tampering plugins which rely on filtering the results of __ and associates. A better workflow will be to have an explicit generation of the blob array from strings being translated with __ calls, similar to how you would do JS translation in non GB context. This will also solve the issue of generating .pot files.



          What is missing here is some automated process that will run over JS files and produce he relevant PHP code, which in turn can be analyzed by tools like poedit.






          share|improve this answer




















          • About your last paragraph: "includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery"
            – kero
            Aug 22 at 6:46






          • 1




            nice starting point, only part left is to auto generate the call to wp_add_inline_script, as right now it probably just generate php just for the benefit of pot generation, but do not actually use it (my guess)
            – Mark Kaplun
            Aug 22 at 6:48













          up vote
          2
          down vote










          up vote
          2
          down vote









          At least for now, as long as there is no better automated process, I would suggest not to generate .pot files from JS at all.



          As @kero explains in his answer right now GB translations are being passed as a kind of blob array from the .mo file into JS. This workflow will break all localization tampering plugins which rely on filtering the results of __ and associates. A better workflow will be to have an explicit generation of the blob array from strings being translated with __ calls, similar to how you would do JS translation in non GB context. This will also solve the issue of generating .pot files.



          What is missing here is some automated process that will run over JS files and produce he relevant PHP code, which in turn can be analyzed by tools like poedit.






          share|improve this answer












          At least for now, as long as there is no better automated process, I would suggest not to generate .pot files from JS at all.



          As @kero explains in his answer right now GB translations are being passed as a kind of blob array from the .mo file into JS. This workflow will break all localization tampering plugins which rely on filtering the results of __ and associates. A better workflow will be to have an explicit generation of the blob array from strings being translated with __ calls, similar to how you would do JS translation in non GB context. This will also solve the issue of generating .pot files.



          What is missing here is some automated process that will run over JS files and produce he relevant PHP code, which in turn can be analyzed by tools like poedit.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 22 at 6:41









          Mark Kaplun

          19.6k52551




          19.6k52551











          • About your last paragraph: "includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery"
            – kero
            Aug 22 at 6:46






          • 1




            nice starting point, only part left is to auto generate the call to wp_add_inline_script, as right now it probably just generate php just for the benefit of pot generation, but do not actually use it (my guess)
            – Mark Kaplun
            Aug 22 at 6:48

















          • About your last paragraph: "includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery"
            – kero
            Aug 22 at 6:46






          • 1




            nice starting point, only part left is to auto generate the call to wp_add_inline_script, as right now it probably just generate php just for the benefit of pot generation, but do not actually use it (my guess)
            – Mark Kaplun
            Aug 22 at 6:48
















          About your last paragraph: "includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery"
          – kero
          Aug 22 at 6:46




          About your last paragraph: "includes a pot-to-php script used to generate a php files containing the messages listed in a .pot file. This is useful to trick WordPress.org translation strings discovery"
          – kero
          Aug 22 at 6:46




          1




          1




          nice starting point, only part left is to auto generate the call to wp_add_inline_script, as right now it probably just generate php just for the benefit of pot generation, but do not actually use it (my guess)
          – Mark Kaplun
          Aug 22 at 6:48





          nice starting point, only part left is to auto generate the call to wp_add_inline_script, as right now it probably just generate php just for the benefit of pot generation, but do not actually use it (my guess)
          – Mark Kaplun
          Aug 22 at 6:48













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f312126%2fhow-does-gutenberg-handle-translations-in-react%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Mutual Information Always Non-negative

          Why am i infinitely getting the same tweet with the Twitter Search API?