Skip to content

Commit 32744a4

Browse files
authored
Merge pull request #428 from wp-cli/fix/427-format-flag
Add php-format and js-format flags
2 parents c3f6f01 + 72105e1 commit 32744a4

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

features/makepot.feature

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,7 @@ Feature: Generate a POT file of a WordPress project
26082608
"""
26092609
#. translators: %s: test
26102610
#: foo-plugin.js:1
2611+
#, js-format
26112612
msgid "Hi %s"
26122613
msgstr ""
26132614
"""
@@ -4097,3 +4098,60 @@ Feature: Generate a POT file of a WordPress project
40974098
"""
40984099
msgid "Not extracted style variation description"
40994100
"""
4101+
4102+
Scenario: Add php-format and js-format flags for printf usage
4103+
Given an empty foo-plugin directory
4104+
And a foo-plugin/foo-plugin.php file:
4105+
"""
4106+
<?php
4107+
/**
4108+
* Plugin Name: Foo Plugin
4109+
* Plugin URI: https://example.com
4110+
* Description:
4111+
* Version: 0.1.0
4112+
* Author:
4113+
* Author URI:
4114+
* License: GPL-2.0+
4115+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
4116+
* Text Domain: foo-plugin
4117+
* Domain Path: /languages
4118+
*/
4119+
4120+
/* translators: %s: Name */
4121+
__( 'Hello %s', 'foo-plugin' );
4122+
/* translators: 1: Name */
4123+
__( 'Bonjour %1$s', 'foo-plugin' );
4124+
"""
4125+
And a foo-plugin/foo.js file:
4126+
"""
4127+
/* translators: %s: Name */
4128+
__( 'Hallo %s', 'foo-plugin' );
4129+
/* translators: 1: Name */
4130+
__( 'Buongiorno %1$s', 'foo-plugin' );
4131+
"""
4132+
4133+
When I run `wp i18n make-pot foo-plugin foo-plugin.pot`
4134+
Then the foo-plugin.pot file should contain:
4135+
"""
4136+
#: foo-plugin.php:16
4137+
#, php-format
4138+
msgid "Hello %s"
4139+
"""
4140+
And the foo-plugin.pot file should contain:
4141+
"""
4142+
#: foo-plugin.php:18
4143+
#, php-format
4144+
msgid "Bonjour %1$s"
4145+
"""
4146+
And the foo-plugin.pot file should contain:
4147+
"""
4148+
#: foo.js:2
4149+
#, js-format
4150+
msgid "Hallo %s"
4151+
"""
4152+
And the foo-plugin.pot file should contain:
4153+
"""
4154+
#: foo.js:4
4155+
#, js-format
4156+
msgid "Buongiorno %1$s"
4157+
"""

src/JsFunctionsScanner.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,18 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
176176
}
177177

178178
$translation = $translations->insert( $context, $original, $plural );
179+
179180
if ( $add_reference ) {
180181
$translation->addReference( $file, $line );
181182
}
182183

184+
if (
185+
1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original ) ||
186+
1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original )
187+
) {
188+
$translation->addFlag( 'js-format' );
189+
}
190+
183191
/** @var Node\Comment $comment */
184192
foreach ( $all_comments as $comment ) {
185193
// Comments should be before the translation.

src/PhpFunctionsScanner.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,18 @@ public function saveGettextFunctions( $translations, array $options ) {
7272
}
7373

7474
$translation = $translations->insert( $context, $original, $plural );
75+
7576
if ( $add_reference ) {
7677
$translation = $translation->addReference( $file, $line );
7778
}
7879

80+
if (
81+
1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original ) ||
82+
1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original )
83+
) {
84+
$translation->addFlag( 'php-format' );
85+
}
86+
7987
if ( isset( $function[3] ) ) {
8088
foreach ( $function[3] as $extracted_comment ) {
8189
$translation = $translation->addExtractedComment( $extracted_comment );

0 commit comments

Comments
 (0)