-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we run this script in ./bin/build-plugin-zip.sh
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I swear I commented before your commit :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, just realized that myself. I have added it to there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
Might be good to have @aduth's opinion as well.
@@ -81,6 +81,8 @@ status "Generating build..." | |||
npm run build | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use the _
prefix to designate lodash functions. Will change.
bin/pot-to-php.js
Outdated
const path = require( 'path' ); | ||
const fs = require( 'fs' ); | ||
|
||
const TAB = '\t'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value this constant provides is questionable 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
https://codex.wordpress.org/I18n_for_WordPress_Developers#Descriptions
bin/pot-to-php.js
Outdated
return php; | ||
} | ||
|
||
function convertPotToPHP( potFile, phpFile, options ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, " See also: https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.