Skip to content

Commit

Permalink
Merge pull request #428 from wp-cli/fix/427-format-flag
Browse files Browse the repository at this point in the history
Add php-format and js-format flags
  • Loading branch information
swissspidy authored Feb 28, 2025
2 parents c3f6f01 + 72105e1 commit 32744a4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
58 changes: 58 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,7 @@ Feature: Generate a POT file of a WordPress project
"""
#. translators: %s: test
#: foo-plugin.js:1
#, js-format
msgid "Hi %s"
msgstr ""
"""
Expand Down Expand Up @@ -4097,3 +4098,60 @@ Feature: Generate a POT file of a WordPress project
"""
msgid "Not extracted style variation description"
"""

Scenario: Add php-format and js-format flags for printf usage
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
* Plugin URI: https://example.com
* Description:
* Version: 0.1.0
* Author:
* Author URI:
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: foo-plugin
* Domain Path: /languages
*/
/* translators: %s: Name */
__( 'Hello %s', 'foo-plugin' );
/* translators: 1: Name */
__( 'Bonjour %1$s', 'foo-plugin' );
"""
And a foo-plugin/foo.js file:
"""
/* translators: %s: Name */
__( 'Hallo %s', 'foo-plugin' );
/* translators: 1: Name */
__( 'Buongiorno %1$s', 'foo-plugin' );
"""

When I run `wp i18n make-pot foo-plugin foo-plugin.pot`
Then the foo-plugin.pot file should contain:
"""
#: foo-plugin.php:16
#, php-format
msgid "Hello %s"
"""
And the foo-plugin.pot file should contain:
"""
#: foo-plugin.php:18
#, php-format
msgid "Bonjour %1$s"
"""
And the foo-plugin.pot file should contain:
"""
#: foo.js:2
#, js-format
msgid "Hallo %s"
"""
And the foo-plugin.pot file should contain:
"""
#: foo.js:4
#, js-format
msgid "Buongiorno %1$s"
"""
8 changes: 8 additions & 0 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,18 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
}

$translation = $translations->insert( $context, $original, $plural );

if ( $add_reference ) {
$translation->addReference( $file, $line );
}

if (
1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original ) ||
1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original )
) {
$translation->addFlag( 'js-format' );
}

/** @var Node\Comment $comment */
foreach ( $all_comments as $comment ) {
// Comments should be before the translation.
Expand Down
8 changes: 8 additions & 0 deletions src/PhpFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,18 @@ public function saveGettextFunctions( $translations, array $options ) {
}

$translation = $translations->insert( $context, $original, $plural );

if ( $add_reference ) {
$translation = $translation->addReference( $file, $line );
}

if (
1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original ) ||
1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original )
) {
$translation->addFlag( 'php-format' );
}

if ( isset( $function[3] ) ) {
foreach ( $function[3] as $extracted_comment ) {
$translation = $translation->addExtractedComment( $extracted_comment );
Expand Down

0 comments on commit 32744a4

Please sign in to comment.