-
Notifications
You must be signed in to change notification settings - Fork 849
In progress - Config: Add a class that customizes the textdomain in packages #14650
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
Conversation
|
Caution: This PR has changes that must be merged to WordPress.com |
|
Thank you for the great PR description! When this PR is ready for review, please apply the Scheduled Jetpack release: March 3, 2020. |
fc76d84 to
adae740
Compare
|
This is nice. From a quick read through, I ask |
Yes, definitely. I used 'default' so that there would be a valid textdomain if the consuming plugin doesn't provide one in composer.json. A better approach would be to use something unique, as you suggested, and then change it to 'default' when a textdomain isn't provided. |
Add a new class, Textdomain_Customizer, that customizes the textdomain of strings in the packages. It uses the textdomain given in the extra section of the plugin's composer.json file. Update Jetpack's composer.json file with the post_install method and Jetpack's textdomain. Provide a preliminary unit test for the new class.
adae740 to
8bec3d0
Compare
|
kbrown9, Your synced wpcom patch D38741-code has been updated. |
|
I made a lot of changes to this, so I force-pushed a new commit.
A couple of things need to be done:
|
zinigor
left a comment
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 love where this is going! This will solve the i18n problem inside packages thanks to how flexible it promises to be! I left a couple of comments, but nothing major.
| } | ||
|
|
||
| foreach ( $files as $file ) { | ||
| $file_path = $this->vendor_dir . $file; |
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 tried googling for best practices of combining path segments into a single path in PHP, and threw up a little :)
Let's at least do trailingslashit for vendor_dir and realpath for the entire thing?
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 class is used when composer install, composer update, or composer dump-autoload are run, so trailingslashit isn’t available. Maybe we can just use rtrim() to remove any trailing slashes, and then add one?
I’ll also use realpath() on the file paths.
| return; | ||
| } | ||
|
|
||
| $file_contents = file_get_contents( $file_path ); |
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.
Do you think loading the whole file into memory is going to be scalable? I don't know how to do it line by line yet, I'm sure it's going to be more fuss, do you think it's worth looking into?
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.
That’s a good point, and I’ll look into it.
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.
Actually, it's going to be a lot of fuss with all the file renaming, etc. Let's just keep what we have here and add error handling for file not being writable.
| foreach ( $files as $file ) { | ||
| $file_path = $this->vendor_dir . $file; | ||
|
|
||
| if ( ! file_exists( $file_path ) ) { |
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 have three different places here that check for either file_exists or is_file, maybe that all can be moved to customize_textdomain_in_file? This will also help with the "cognitive complexity" in CodeClimate analysis.
…paths Use rtrim() to remove trailing slashes and then add a slash to $vendor_dir. This ensures that $vendor_dir has at least on trailing slash in case $file doesn't have a starting slash. Use realpath() to prepare the file paths. Refactor customize_textdomain_in_files() to separate the recursive directory work into its own method. This should reduce the cognitive complexity a bit.
|
This PR has been marked as stale. This happened because:
No further action is needed. But it's worth checking if this PR has clear testing instructions, is it up to date with master, and it is still valid. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation. |
|
I'm closing this in favor of the approach introduced in PR #18003. The approach described in this PR is too prone to errors. For example, it would be easy for developers to forget to use the textdomain placeholder when adding strings to the packages. The approach introduced in PR #18003 is much easier to work with. |
This is a POC to explore a possible solution for using textdomains in the packages. All of the implementation details are up for debate, but I think the general idea works.
The Consuming Plugin
The consuming plugin must set two items in its composer.json file:
the post-autoload-dump script in the scripts section.
the textdomain in the extra section.
For example, the scripts and extra sections of Jetpack’s composer.json file would look like:
The Jetpack Packages
The Jetpack packages must provide a list of their translatable files in the extra section of their composer.json files. This PR includes a temporary example file in the Config package. The Config package composer.json includes this extra section listing the example file:
How it works
composer dump-autoloadis run (it's run after both install and update), theTextdomain_Customizer::post_autoload_dump()method is called at the end of the dump-autoload process.post_autoload_dump()method just returns. This prevents the files from being changed and causing headaches during development.textdomainentry in the extra section. If it exists, that textdomain will be used in the package files. If it does not exist, 'default' will be used.translatableentry in each Jetpack package's composer.json file. If a Jetpack package has atranslatableentry, the listed files are processed.JETPACK_CUSTOMIZE_TEXTDOMAINwith either the plugin's textdomain or 'default'.Changes proposed in this Pull Request:
Textdomain_Customizer, that customizes the textdomain of strings in the packages. I put this in the Config class because it might be useful for multiple packages.Is this a new feature or does it add/remove features to an existing part of Jetpack?
Testing instructions
composer installand check the example file included in this PR,packages/config/src/example-file.php. The textdomain in this file should still be the placeholderJETPACK_CUSTOMIZE_TEXTDOMAINbecause you're using a development version of the Config package.composer installand check filepackages/config/src/example-file.php. The textdomain should be 'jetpack'.Proposed changelog entry for your changes:
To do
A couple of things need to be done: