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

WordPress Core support #69

Merged
merged 20 commits into from
Aug 6, 2018
Merged

WordPress Core support #69

merged 20 commits into from
Aug 6, 2018

Conversation

swissspidy
Copy link
Member

@swissspidy swissspidy commented Aug 5, 2018

Changes in this PR:

  • Allows passing more than one file to --merge
  • Does not error when no plugin or theme file has been found
  • Adds glob support to --exclude (see Ignore minified JavaScript files #45)
  • Adds a new --include argument as an addition to --exclude
  • Adds new --copyright-holder and --package-name arguments
    These values are used for the copyright notice comment at the top of the POT file.
    The naming of the arguments and the usefulness of that comment as a whole should be discussed.
  • Adds a new --except --subtract option that accepts one or more POT files
    Strings that are on this "blacklist" won't be extracted

To do:

  • Add necessary features for supporting string extraction in WordPress core (and perhaps any other project)
  • Cover these new features in tests

The following commands can be used to generate the necessary POT files for WordPress core.

Note: the Report-Msgid-Bugs-To URL can be overridden using the --header argument.

Continents & Cities

wp i18n make-pot /path/to/wordpress/core /path/to/target/wp-tz.pot \
--include="wp-admin/includes/continents-cities.php" \
--package-name="WordPress" \
--copyright-holder="WordPress" \
--debug \
--skip-js \
--ignore-domain

WordPress (Development 4.9.x on translate.wordpress.org)

wp i18n make-pot /path/to/wordpress/core /path/to/target/wordpress.pot \
--exclude="wp-admin/*,wp-content/themes/*,wp-includes/class-pop3.php,wp-content/plugins/akismet/" \
--package-name="WordPress" \
--copyright-holder="WordPress" \
--debug \
--skip-js \
--ignore-domain

Administration

This one involves some more steps as it should include all files except for WordPress and Network Admin files. Also, the Hello Dolly plugin, which is bundled with WordPress core, needs to be translated as well.

  1. Hello Dolly:
wp i18n make-pot /path/to/wordpress/core/wp-content/plugins /path/to/target/hello-dolly.pot \
--include="hello.php" \
--exclude="akismet" \
--debug \
--skip-js \
--ignore-domain
  1. WordPress Administration
wp i18n make-pot /path/to/wordpress/core /path/to/target/wp-admin.pot \
--exclude="wp-admin/network/*,wp-admin/network.php,wp-admin/includes/class-wp-ms*,wp-admin/includes/network.php,wp-admin/includes/continents-cities.php" \
--include="wp-admin/*" \
--merge="/path/to/target/hello-dolly.pot" \
--subtract="/path/to/target/wp-frontend.pot" \
--package-name="WordPress" \
--copyright-holder="WordPress" \
--debug \
--skip-js \
--ignore-domain

Network Admin

wp i18n make-pot /path/to/wordpress/core /path/to/target/wp-network-admin.pot \
--include="wp-admin/network/*,wp-admin/network.php,wp-admin/includes/class-wp-ms*,wp-admin/includes/network.php" \
--subtract="/path/to/target/wp-frontend.pot,/path/to/target/wp-admin.pot" \
--package-name="WordPress" \
--copyright-holder="WordPress" \
--debug \
--skip-js \
--ignore-domain

Fixes #36.
Fixes #45.

@swissspidy
Copy link
Member Author

Here's the output of the WP-CLI command and makepot.php for a quick comparison:

makepot.zip
wp-cli-i18n-command.zip

The POT files for continents and cities have the same number of strings (524). The others seem to differ quite a bit.

Tested both with current WordPress master/trunk

Type WP-CLI makepot.php
Frontend 2917 2665
Continents & Cities 524 524
Administration 2975 2645
Network Admin 405 311

I took the numbers on the right from translate.wordpress.org as the local output with makepot.php seems to be quite different. Not sure why though.

Perhaps @ocean90 can help me find the cause for this :-)

@swissspidy
Copy link
Member Author

Instead of the nightly build I ran this now against 4.9.8 as downloaded straight from WordPress.org/download.

The numbers are much better now:

Type WP-CLI makepot.php
Frontend 2665 2665
Continents & Cities 524 524
Administration 2981 2645
Network Admin 404 311

Administration now includes Hello Dolly (as does makepot.php / translate.wordpress.org), that's why that number is higher than before.

@swissspidy
Copy link
Member Author

So, the reason for the difference between the admin files seems to be that makepot.php removes any strings that are already part of the frontend POT file to avoid any duplicates.

It uses the msgcat utility for that. So when generating the admin POT file it actually does this:

  1. Generate frontend POT file
  2. Generate admin POT file
  3. Merge frontend with admin file and only keep the common strings
  4. Merge admin file with the common file and only keep the unique strings

I'm not sure this is something that could or should be done within this command.

I think if WordPress.org is going to use the WP-CLI command at some point for string extraction, they might add a little wrapper script around it to use msgcat as outlined above to keep existing behavior.

@schlessera
Copy link
Member

Instead of using msgcat, maybe it would be worthwhile to add a --deduplicate flag to --merge ?

@swissspidy
Copy link
Member Author

@schlessera So you'd pass a file name to --deduplicate, e.g. --deduplicate=wp-frontend.pot?

Then we could use something like this:

wp i18n make-pot /path/to/wordpress/core /path/to/target/wp-admin.pot \
--exclude="wp-admin/network/*,wp-admin/network.php,wp-admin/includes/class-wp-ms*,wp-admin/includes/network.php,wp-admin/includes/continents-cities.php" \
--include="wp-admin/*" \
--merge="/path/to/target/hello-dolly.pot" \
--deduplicate="/path/to/target/wp-frontend.pot" \
--package-name="WordPress" \
--copyright-holder="WordPress" \
--debug \
--skip-js \
--ignore-domain

@schlessera
Copy link
Member

I'm not sure, I ignore how the files relate to each other in detail, and what will be considered the source of truth.

But generally, if you have a --merge command, it makes sense in my hand to specify how duplicates are handled.

@schlessera
Copy link
Member

Hmm, I see the problem now. What you need is not deduplication, but set operations like union and most of all intersection.

@swissspidy
Copy link
Member Author

I added an experimental --except argument now.

You can pass one or more exceptions. The command extracts all strings from them and compares them against the ones found by the command.

Strings that are contained in the exception list will be removed from final output.

This way, I got the resulting POT file for wp-admin down to 2733 strings (compared to 2981 before).

@swissspidy
Copy link
Member Author

Alright, got it down to 2647! That's only 2 differences.


Note regarding exceptions: this might be useful as a new merge flag in the upstream gettext library instead, see https://github.com/oscarotero/Gettext#merge-translations.

@swissspidy
Copy link
Member Author

I'm not sure why, but the output generated by makepot.php seems to be missing the following two strings:

  • <strong>ERROR</strong>: "Table Prefix" can only contain numbers, letters, and underscores.
    source: wp-admin/setup-config.php:262
  • <strong>ERROR</strong>: "Table Prefix" must not be empty.
    source: wp-admin/setup-config.php:258

I can't find these strings on https://translate.wordpress.org either.

Sounds like a bug to me and a reason why this command here should be used instead :-)

@swissspidy swissspidy changed the title [WIP] Experiment with WordPress core support WordPress Core support Aug 6, 2018
@swissspidy
Copy link
Member Author

Updated and final numbers:

Type WP-CLI makepot.php
Frontend 2665 2665
Continents & Cities 524 524
Administration 2647 2645
Network Admin 311 311

I feel like now we only need to discuss the wording of the newly added arguments or, in the case of the copyright notice, the usefulness of these.

@swissspidy swissspidy requested a review from a team August 6, 2018 16:02
@schlessera
Copy link
Member

Regarding the --except flag, I think the name is slightly misleading. I feel like --subtract might be clearer, wouldn't it?

README.md Outdated
[--merge[=<file>]]
Existing POT file file whose content should be merged with the extracted strings.
[--merge[=<files>]]
One or more existing POT files whose contents should be merged with the extracted strings.
Copy link
Member

Choose a reason for hiding this comment

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

The syntax for multiple files should be documented.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean documented like <file1,file2> or as an example in the argument description?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure what the best way is, as you have multiple arguments with the same issue.

Maybe a small adjective like comma-separated and then a general note in the command description might be clearer?

	[--merge[=<file[,file2,...]>]]
		Comma-separated list of POT files whose contents should be merged with the extracted strings.

Something like that, maybe? Unsure about the notation...

Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out something like [--merge[=<file[,file2,...]>]] isn't valid.

I'll stick with the description though.

README.md Outdated
If left empty, defaults to the destination POT file.

[--except=<files>]
Copy link
Member

Choose a reason for hiding this comment

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

As already commented, I think --subtract would be a clearer name.
Also, syntax for multiples files should be documented.

README.md Outdated
[--except=<files>]
If set, only strings not already existing in one of the passed POT files will be extracted.

[--include=<paths>]
Copy link
Member

Choose a reason for hiding this comment

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

Syntax for multiple paths needs to be documented.

"""
<?php

__( 'Hello World' );
Copy link
Member

Choose a reason for hiding this comment

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

A regular project would normally use regular gettext with single underscore: _()

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 can add both, although I don‘t think we should enphasise support for any regular, non-WP gettext project.

Copy link
Member

Choose a reason for hiding this comment

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

It was more about having an example that is technically correct. Outside of WordPress, __() would normally not exist.

$this->slug = Utils\get_flag_value( $assoc_args, 'slug', Utils\basename( $this->source ) );
$this->skip_js = Utils\get_flag_value( $assoc_args, 'skip-js', $this->skip_js );
$this->headers = Utils\get_flag_value( $assoc_args, 'headers', $this->headers );
$this->package_name = Utils\get_flag_value( $assoc_args, 'package-name', 'Unknown' );
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the package and copyright holder be optional?
If I understand correctly, this will now always be added, but with values of 'Unknown'.

Copy link
Member Author

Choose a reason for hiding this comment

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

We could either make it optional, change the wording (just „Copyright 2018“ without any author or name) or remove it completely. I‘m not sure if it really serves any purpose.

Also, two command arguments just for this comment is a bit too much I think. Perhaps we could go with a singular argument to manually override that line?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, something more generic like --file-comment might be preferable.

Copy link
Member Author

Choose a reason for hiding this comment

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

See #47 (comment) and #55 for previous discussion about a --comment / --file-comment argument

Copy link
Member

Choose a reason for hiding this comment

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

I think --comment could be ambiguous, as it could just as well refer to a translator comment.

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 added --file-comment but left package-name as it's needed/useful for the Project-Id-Version POT header.

@swissspidy
Copy link
Member Author

It‘s certainly not ideal, yes. I have yet to come up with a better name. Subtract could work.

@schlessera schlessera added this to the 0.1.0 milestone Aug 6, 2018
@schlessera schlessera merged commit 066d581 into master Aug 6, 2018
@schlessera schlessera deleted the 36-core branch August 6, 2018 21:30
@schlessera
Copy link
Member

🎉

@johnbillion
Copy link

@swissspidy Regarding the two "Table prefix" strings that makepot missed, it might be because they were malformed. See https://core.trac.wordpress.org/changeset/43205.

schlessera added a commit that referenced this pull request Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants