Skip to content

Commit 3b5133d

Browse files
committed
Add php-format and js-format flags
1 parent a5b4752 commit 3b5133d

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

features/makepot.feature

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

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)