Skip to content

Commit

Permalink
Merge pull request #264 from wp-cli/add/no-location
Browse files Browse the repository at this point in the history
Add new flag to omit source code references
  • Loading branch information
swissspidy authored Jul 19, 2021
2 parents a8ee181 + 02a3612 commit 1095a00
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 15 deletions.
64 changes: 64 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,70 @@ Feature: Generate a POT file of a WordPress project
I am not being ignored
"""

Scenario: Omit source code references
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
*/
__( 'Hello World', 'foo-plugin' );
"""
And a foo-plugin/file.php file:
"""
<?php
__( 'Foo', 'foo-plugin' );
"""
And a foo-plugin/file.js file:
"""
__( 'Bar', 'foo-plugin' );
"""
And a foo-plugin/block.json file:
"""
{
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
"parent": [ "core/group" ],
"icon": "star",
"description": "Shows warning, error or success notices ...",
"keywords": [ "alert", "message" ],
"textdomain": "foo-plugin",
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": ".message"
}
},
"styles": [
{ "name": "default", "label": "Default", "isDefault": true },
{ "name": "other", "label": "Other" }
],
"editorScript": "build/editor.js",
"script": "build/main.js",
"editorStyle": "build/editor.css",
"style": "build/style.css"
}
"""

When I run `wp i18n make-pot foo-plugin foo-plugin.pot --no-location`
Then the foo-plugin.pot file should not contain:
"""
#:
"""

Scenario: Merges translations with the ones from an existing POT file
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.pot file:
Expand Down
14 changes: 11 additions & 3 deletions src/BlockExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public static function fromString( $string, Translations $translations, array $o
return;
}

$add_reference = ! empty( $options['addReferences'] );

foreach ( $file_data as $key => $original ) {
switch ( $key ) {
case 'title':
case 'description':
$translation = $translations->insert( sprintf( 'block %s', $key ), $original );
$translation->addReference( $file );
if ( $add_reference ) {
$translation->addReference( $file );
}
break;
case 'keywords':
if ( ! is_array( $original ) ) {
Expand All @@ -53,7 +57,9 @@ public static function fromString( $string, Translations $translations, array $o

foreach ( $original as $msg ) {
$translation = $translations->insert( 'block keyword', $msg );
$translation->addReference( $file );
if ( $add_reference ) {
$translation->addReference( $file );
}
}

break;
Expand All @@ -64,7 +70,9 @@ public static function fromString( $string, Translations $translations, array $o

foreach ( $original as $msg ) {
$translation = $translations->insert( 'block style label', $msg['label'] );
$translation->addReference( $file );
if ( $add_reference ) {
$translation->addReference( $file );
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public function saveGettextFunctions( $translations, array $options ) {
*/
$traverser->addFunction(
function ( $node ) use ( &$translations, $options, &$all_comments ) {
$functions = $options['functions'];
$file = $options['file'];
$functions = $options['functions'];
$file = $options['file'];
$add_reference = ! empty( $options['addReferences'] );

/** @var Node\Node $node */
foreach ( $node->getLeadingComments() as $comment ) {
Expand Down Expand Up @@ -168,7 +169,9 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
}

$translation = $translations->insert( $context, $original, $plural );
$translation->addReference( $file, $line );
if ( $add_reference ) {
$translation->addReference( $file, $line );
}

/** @var Node\Comment $comment */
foreach ( $all_comments as $comment ) {
Expand Down
27 changes: 21 additions & 6 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class MakePotCommand extends WP_CLI_Command {
*/
protected $skip_audit = false;

/**
* @var bool
*/
protected $location = true;

/**
* @var array
*/
Expand Down Expand Up @@ -197,6 +202,11 @@ class MakePotCommand extends WP_CLI_Command {
* [--headers=<headers>]
* : Array in JSON format of custom headers which will be added to the POT file. Defaults to empty array.
*
* [--location]
* : Whether to write `#: filename:line` lines.
* Defaults to true, use `--no-location` to skip the removal.
* Note that disabling this option makes it harder for technically skilled translators to understand each message’s context.
*
* [--skip-js]
* : Skips JavaScript string extraction. Useful when this is done in another build step, e.g. through Babel.
*
Expand Down Expand Up @@ -289,6 +299,7 @@ public function handle_arguments( $args, $assoc_args ) {
$this->headers = Utils\get_flag_value( $assoc_args, 'headers', $this->headers );
$this->file_comment = Utils\get_flag_value( $assoc_args, 'file-comment' );
$this->package_name = Utils\get_flag_value( $assoc_args, 'package-name' );
$this->location = Utils\get_flag_value( $assoc_args, 'location', true );

$ignore_domain = Utils\get_flag_value( $assoc_args, 'ignore-domain', false );

Expand Down Expand Up @@ -585,6 +596,7 @@ protected function extract_strings() {
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'php' ],
'addReferences' => $this->location,
];
PhpCodeExtractor::fromDirectory( $this->source, $translations, $options );
}
Expand All @@ -594,19 +606,21 @@ protected function extract_strings() {
$this->source,
$translations,
[
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'js', 'jsx' ],
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'js', 'jsx' ],
'addReferences' => $this->location,
]
);

MapCodeExtractor::fromDirectory(
$this->source,
$translations,
[
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'map' ],
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'map' ],
'addReferences' => $this->location,
]
);
}
Expand All @@ -621,6 +635,7 @@ protected function extract_strings() {
'include' => $this->include,
'exclude' => $this->exclude,
'extensions' => [ 'json' ],
'addReferences' => $this->location,
]
);
}
Expand Down
9 changes: 6 additions & 3 deletions src/PhpFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public function saveGettextFunctions( $translations, array $options ) {
$translations = $translations[0];
}

$functions = $options['functions'];
$file = $options['file'];
$functions = $options['functions'];
$file = $options['file'];
$add_reference = ! empty( $options['addReferences'] );

foreach ( $this->getFunctions( $options['constants'] ) as $function ) {
list( $name, $line, $args ) = $function;
Expand Down Expand Up @@ -71,7 +72,9 @@ public function saveGettextFunctions( $translations, array $options ) {
}

$translation = $translations->insert( $context, $original, $plural );
$translation = $translation->addReference( $file, $line );
if ( $add_reference ) {
$translation = $translation->addReference( $file, $line );
}

if ( isset( $function[3] ) ) {
foreach ( $function[3] as $extracted_comment ) {
Expand Down

0 comments on commit 1095a00

Please sign in to comment.