Add a script that creates a PHP file based on a POT#5310
Conversation
This is a workaround to make Gutenberg translatable.
| @@ -0,0 +1,102 @@ | |||
| #!/usr/bin/env node | |||
There was a problem hiding this comment.
Should we run this script in ./bin/build-plugin-zip.sh?
There was a problem hiding this comment.
I swear I commented before your commit :P
There was a problem hiding this comment.
Yeah, just realized that myself. I have added it to there.
youknowriad
left a comment
There was a problem hiding this comment.
LGTM 👍
Might be good to have @aduth's opinion as well.
| status "Generating translation messages..." | ||
| npm run gettext-strings | ||
| status "Generating PHP file for wordpress.org to parse translations..." | ||
| npm run pot-to-php |
There was a problem hiding this comment.
Few lines below, we need to include this new PHP file in the generated ZIP archive.
bin/pot-to-php.js
Outdated
| #!/usr/bin/env node | ||
|
|
||
| const gettextParser = require( 'gettext-parser' ); | ||
| const _isEmpty = require( 'lodash/isEmpty' ); |
There was a problem hiding this comment.
What's with the _ prefix?
Minor: Might be slightly more optimized this way, but in a Node context we don't care as much about granular module includes, and could inadvertently encourage future maintainers to add more Lodash dependencies in a similar fashion, whereas the following would be simple to include more utilities in batch fashion:
const { isEmpty } = require( 'lodash' );There was a problem hiding this comment.
We use the _ prefix to designate lodash functions. Will change.
| const path = require( 'path' ); | ||
| const fs = require( 'fs' ); | ||
|
|
||
| const TAB = '\t'; |
There was a problem hiding this comment.
The value this constant provides is questionable 😄
There was a problem hiding this comment.
I think I found the code easier to read after introducing these constants.
bin/pot-to-php.js
Outdated
| let original = translation.msgid; | ||
| const comments = translation.comments; | ||
|
|
||
| if ( ! _isEmpty( comments ) ) { |
There was a problem hiding this comment.
Pretty sure that while we're including the file reference in the PHP file, we're omitting the comments that would be picked up in the PHP string extraction, both text of the original comment and file reference.
bin/pot-to-php.js
Outdated
| return php; | ||
| } | ||
|
|
||
| function convertPotToPHP( potFile, phpFile, options ) { |
There was a problem hiding this comment.
Mixed abbreviation capitalization. Should either be convertPotToPhp or convertPOTToPHP, my preference toward the latter.
|
I've fixed most of the comments, @aduth. In this paragraph:
What do you mean with |
|
For the purposes of string extraction, " Specifically: |
|
I guess for the automatic string extraction, it's going to automatically assign the PHP file as reference? In which case if a human were to follow it to the file, the "Reference:" comment to the JavaScript source could be valuable. We still want the text of the comments represented via |
aduth
left a comment
There was a problem hiding this comment.
Aside from one note, this is looking pretty great 👍
bin/pot-to-php.js
Outdated
| original = escapeSingleQuotes( original ); | ||
|
|
||
| if ( isEmpty( translation.msgid_plural ) ) { | ||
| php += TAB + `__( '${ original }', '${ textdomain }' )`; |
There was a problem hiding this comment.
We also need:
_x(determined bytranslation.msgctxt)_nx(as_nx_noop, determined bytranslation.msgctxt,translation.msgid_plural)
|
I don't think there is a way to tell the PHP parser that the reference to the file should be different. I think it would be confusing if we put that in the translator comment. So we have to make do with translators going to the PHP file then discovering that they need to go to the JS file. #5169 is the correct solution for this. I will address |
This will generate _x and _nx when appropriate.
aduth
left a comment
There was a problem hiding this comment.
Looks like recent changes might have broken the formatting of the PHP output. Reference comments are no longer on their own lines.
| @@ -0,0 +1,121 @@ | |||
| #!/usr/bin/env node | |||
There was a problem hiding this comment.
If we drop this and instead call from the npm script as node ./bin/pot-to-php.js, wouldn't we have the advantage of added Windows support?
There was a problem hiding this comment.
Sorry, for some reason email notifications for code reviews were turned off.
I can definitely see this change. I copied it from ./bin/create-php-parser.js. After this is merged I can create a PR that addresses both files.
|
Fixed the remaining issue. |
pento
left a comment
There was a problem hiding this comment.
This is such a wonderful hack, I love it. 😁
Obviously we need a long term solution, but I'm fine with this for now.
Description
This is a workaround to make Gutenberg translatable. translate.wordpress.org ignores the POT file provided by the plugin and parser the PHP file itself. We can work around this by providing a PHP file generated on the POT file.
How Has This Been Tested?
I've copied the code from wordpress-seo and adapted it for a non-grunt environment. So the code has been tested by wordpress-seo. I've run it once to see that it generates something sensible.
The real tests can only be done once it is on translate.wordpress.org. Which this PR + a release will accomplish.
See #5169. Note that this PR is the short-term solution.