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

119 Filename before the warning and full path to it #361

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ Feature: Generate a POT file of a WordPress project
"""
And STDERR should contain:
"""
Warning: The string "Hello World" has 2 different translator comments. (foo-plugin.php:7)
Warning: foo-plugin.php:7: The string "Hello World" has 2 different translator comments.
translators: Translators 1!
Translators: Translators 2!
"""
Expand Down Expand Up @@ -812,7 +812,7 @@ Feature: Generate a POT file of a WordPress project
"""
And STDERR should contain:
"""
Warning: Found string without translatable content. (foo-plugin.php:6)
Warning: foo-plugin.php:6: Found string without translatable content.
"""

Scenario: Prints a warning for a string with missing translator comment
Expand All @@ -836,7 +836,7 @@ Feature: Generate a POT file of a WordPress project
"""
And STDERR should contain:
"""
Warning: The string "Hello, %s" contains placeholders but has no "translators:" comment to clarify their meaning. (foo-plugin.php:6)
Warning: foo-plugin.php:6: The string "Hello, %s" contains placeholders but has no "translators:" comment to clarify their meaning.
"""

Scenario: Prints a warning for missing singular placeholder
Expand All @@ -863,7 +863,7 @@ Feature: Generate a POT file of a WordPress project
"""
And STDERR should contain:
"""
Missing singular placeholder, needed for some languages. See https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals (foo-plugin.php:7)
foo-plugin.php:7: Missing singular placeholder, needed for some languages. See https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals
"""

Scenario: Prints a warning for mismatched placeholders
Expand Down Expand Up @@ -891,7 +891,7 @@ Feature: Generate a POT file of a WordPress project
"""
And STDERR should contain:
"""
Mismatched placeholders for singular and plural string. (foo-plugin.php:7)
foo-plugin.php:7: Mismatched placeholders for singular and plural string.
"""

Scenario: Prints a warning for multiple unordered placeholders
Expand Down Expand Up @@ -919,7 +919,7 @@ Feature: Generate a POT file of a WordPress project
"""
And STDERR should contain:
"""
Multiple placeholders should be ordered. (foo-plugin.php:7)
foo-plugin.php:7: Multiple placeholders should be ordered.
"""

Scenario: Prints no warnings when audit is being skipped.
Expand Down
30 changes: 16 additions & 14 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class MakePotCommand extends WP_CLI_Command {
public function __invoke( $args, $assoc_args ) {
$this->handle_arguments( $args, $assoc_args );

$translations = $this->extract_strings();
$translations = $this->extract_strings( $args );

if ( ! $translations ) {
WP_CLI::warning( 'No strings found' );
Expand Down Expand Up @@ -578,9 +578,10 @@ protected function get_file_headers( $type ) {
*
* @throws WP_CLI\ExitException
*
* @param array $args Command arguments.
* @return Translations A Translation set.
*/
protected function extract_strings() {
protected function extract_strings( $args ) {
$translations = new Translations();

// Add existing strings first but don't keep headers.
Expand Down Expand Up @@ -732,7 +733,7 @@ protected function extract_strings() {
}

if ( ! $this->skip_audit ) {
$this->audit_strings( $translations );
$this->audit_strings( $translations, $args );
}

return $translations;
Expand All @@ -744,25 +745,26 @@ protected function extract_strings() {
* Goes through all extracted strings to find possible mistakes.
*
* @param Translations $translations Translations object.
* @param array $args Command arguments.
*/
protected function audit_strings( $translations ) {
protected function audit_strings( $translations, $args ) {
foreach ( $translations as $translation ) {
/** @var Translation $translation */

$references = $translation->getReferences();

// File headers don't have any file references.
$location = $translation->hasReferences() ? '(' . implode( ':', $references[0] ) . ')' : '';
$location = $translation->hasReferences() ? realpath( $args[0] ) . '/' . implode( ':', $references[0] ) : '';

// Check 1: Flag strings with placeholders that should have translator comments.
if (
! $translation->hasExtractedComments() &&
preg_match( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $placeholders ) >= 1
) {
$message = sprintf(
'The string "%1$s" contains placeholders but has no "translators:" comment to clarify their meaning. %2$s',
$translation->getOriginal(),
$location
'%1$s: The string "%2$s" contains placeholders but has no "translators:" comment to clarify their meaning.',
$location,
$translation->getOriginal()
);
WP_CLI::warning( $message );
}
Expand Down Expand Up @@ -808,10 +810,10 @@ function ( $comment ) use ( &$unique_comments ) {

if ( $comments_count > 1 ) {
$message = sprintf(
"The string \"%1\$s\" has %2\$d different translator comments. %3\$s\n%4\$s",
"%1\$s: The string \"%2\$s\" has %3\$d different translator comments. %4\$s",
$location,
$translation->getOriginal(),
$comments_count,
$location,
implode( "\n", $unique_comments )
);
WP_CLI::warning( $message );
Expand All @@ -824,7 +826,7 @@ function ( $comment ) use ( &$unique_comments ) {
// Check 3: Flag empty strings without any translatable content.
if ( '' === $non_placeholder_content ) {
$message = sprintf(
'Found string without translatable content. %s',
'%s: Found string without translatable content.',
$location
);
WP_CLI::warning( $message );
Expand All @@ -836,7 +838,7 @@ function ( $comment ) use ( &$unique_comments ) {

if ( $unordered_matches_count >= 2 ) {
$message = sprintf(
'Multiple placeholders should be ordered. %s',
'%s: Multiple placeholders should be ordered.',
$location
);
WP_CLI::warning( $message );
Expand All @@ -853,7 +855,7 @@ function ( $comment ) use ( &$unique_comments ) {
if ( count( $single_placeholders ) < count( $plural_placeholders ) ) {
// Check 5: Flag things like _n( 'One comment', '%s Comments' )
$message = sprintf(
'Missing singular placeholder, needed for some languages. See https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals %s',
'%s: Missing singular placeholder, needed for some languages. See https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals.',
$location
);
WP_CLI::warning( $message );
Expand All @@ -865,7 +867,7 @@ function ( $comment ) use ( &$unique_comments ) {
// Check 6: Flag things like _n( '%s Comment (%d)', '%s Comments (%s)' )
if ( $single_placeholders !== $plural_placeholders ) {
$message = sprintf(
'Mismatched placeholders for singular and plural string. %s',
'%s: Mismatched placeholders for singular and plural string.',
$location
);
WP_CLI::warning( $message );
Expand Down