Skip to content
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

Ensure that module header fields can be translated #60

Merged
merged 20 commits into from
Dec 23, 2021

Conversation

felixarntz
Copy link
Member

@felixarntz felixarntz commented Dec 21, 2021

Fixes #59

This PR implements an npm run translations command (via the JS infrastructure introduced in #51), which makes the two module header fields Module Name and Description translatable by extracting the values from all module files and putting them into a generated module-i18n.php file. This file allows wordpress.org to recognize these translations as if they were normally used in the plugin. The PR then uses a similar approach to WordPress core when it comes to the headers themselves in the production code: By calling translate it will technically go through the same function, which in combination with the generated file ensures that those are translatable in GlotPress.

The implementation of the script is heavily based on a combination of WordPress/gutenberg#5310 and wp-pot/wp-pot@c46b8cb, both of which do in parts similar tasks.

The generated file is .gitignored, since it doesn't need to be in the repository (it isn't even required anywhere, it just needs to "exist" when the plugin is deployed to wordpress.org).

To test the script locally, run npm run translations.

@felixarntz felixarntz added [Type] Bug An existing feature is broken Infrastructure Issues for the overall performance plugin infrastructure labels Dec 21, 2021
@felixarntz felixarntz added this to the Initial plugin release milestone Dec 21, 2021
bin/plugin/config.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
for await ( const line of rl ) {
headers.forEach( ( header, index ) => {
const regex = new RegExp(
'^(?:[ \t]*<?php)?[ \t/*#@]*' + header + ':(.*)$',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the capturing group can be adjusted to only get valid words.

Suggested change
'^(?:[ \t]*<?php)?[ \t/*#@]*' + header + ':(.*)$',
'^(?:[ \t]*<?php)?[ \t/*#@]*' + header + ':\s*([\w|\s]*).*$',

It also removes the need to call trim() on the next iteration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I get the idea, but there's something off with that regex. I tried to add it, but afterwards no matches were found anymore - note that the fields may contain spaces. If that's crucial, feel free to revise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just ^\s*\*\s*${ header }:\s*(.*)$? https://regex101.com/r/BpmfsY/1
It will help to avoid doing an additional cleanup afterwards:

 					if ( match ) {
-						const headerValue = match[ 1 ]
-							.replace( /\s*(?:\*\/|\?>).*/, '' )
-							.trim();
-						if ( headerValue ) {
-							headers.splice( index, 1 );
-							translationStrings.push( headerValue );
-						}
+						translationStrings.push( match[1] );
 					}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, can you please try that locally? That suggestion doesn't work for me either.

@felixarntz
Copy link
Member Author

@mitogh Addressed all your feedback, except #60 (comment) (see comment).

@@ -24,10 +24,12 @@
"@wordpress/scripts": "^19.0",
"chalk": "4.1.1",
"commander": "4.1.0",
"fast-glob": "^3.2.7",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixarntz, we also need to update the package-lock.json to make sure everybody uses the same version of this dependency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have been doing something wrong, but when I tried it, the package-lock.json was unchanged. It looks like the same version of this dependency was already previously required via another dependency.

bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
for await ( const line of rl ) {
headers.forEach( ( header, index ) => {
const regex = new RegExp(
'^(?:[ \t]*<?php)?[ \t/*#@]*' + header + ':(.*)$',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just ^\s*\*\s*${ header }:\s*(.*)$? https://regex101.com/r/BpmfsY/1
It will help to avoid doing an additional cleanup afterwards:

 					if ( match ) {
-						const headerValue = match[ 1 ]
-							.replace( /\s*(?:\*\/|\?>).*/, '' )
-							.trim();
-						if ( headerValue ) {
-							headers.splice( index, 1 );
-							translationStrings.push( headerValue );
-						}
+						translationStrings.push( match[1] );
 					}

bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
bin/plugin/commands/translations.js Outdated Show resolved Hide resolved
@felixarntz
Copy link
Member Author

@eugene-manuilov Updated based on your feedback!

Copy link
Member

@mitogh mitogh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✔️

@felixarntz felixarntz merged commit cae6050 into trunk Dec 23, 2021
@tillkruss tillkruss deleted the fix/59-module-headers-translatable branch March 7, 2022 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Infrastructure Issues for the overall performance plugin infrastructure [Type] Bug An existing feature is broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Module header information is not translatable
5 participants