Skip to content
Closed
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
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
}
],
"require": {
"eftec/bladeone": "3.52",
"gettext/gettext": "^4.8",
"eftec/bladeone": "^4.11",
"gettext/gettext": "^5.7",
"gettext/js-scanner": "^1.1",
"gettext/php-scanner": "^v1.3.1",
"gettext/translator": "^1.2",
"mck89/peast": "^1.13.11",
"wp-cli/wp-cli": "^2.12"
},
Expand Down
2 changes: 0 additions & 2 deletions src/BladeCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ final class BladeCodeExtractor extends BladeGettextExtractor {
],
];

protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner';

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 1 addition & 5 deletions src/BladeGettextExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

use eftec\bladeone\BladeOne;

// Modified Gettext Blade extractor that
// uses the up-to-date BladeOne standalone Blade engine,
// correctly supports fromStringMultiple.

/**
* Class to get gettext strings from blade.php files returning arrays.
*/
class BladeGettextExtractor extends \Gettext\Extractors\PhpCode {
class BladeGettextExtractor extends PhpCodeExtractor {

/**
* Prepares a Blade compiler/engine and returns it.
Expand Down
51 changes: 42 additions & 9 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@
$headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ] );

if ( ! empty( $headers['Template Name'] ) ) {
$translation = new Translation( '', $headers['Template Name'] );
$translation->addExtractedComment( 'Template Name of the theme' );
$translation = Translation::create( '', $headers['Template Name'] );
$translation->getComments()->add( 'Template Name of the theme' );

Check warning on line 74 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L73-L74

Added lines #L73 - L74 were not covered by tests

$translations[] = $translation;
$translations->add( $translation );

Check warning on line 76 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L76

Added line #L76 was not covered by tests
}
}

Expand All @@ -88,17 +88,17 @@
);

if ( ! empty( $headers['Title'] ) ) {
$translation = new Translation( 'Pattern title', $headers['Title'] );
$translation->addReference( $options['file'] );
$translation = Translation::create( 'Pattern title', $headers['Title'] );
$translation->getReferences()->add( $options['file'] );

Check warning on line 92 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L91-L92

Added lines #L91 - L92 were not covered by tests

$translations[] = $translation;
$translations->add( $translation );

Check warning on line 94 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L94

Added line #L94 was not covered by tests
}

if ( ! empty( $headers['Description'] ) ) {
$translation = new Translation( 'Pattern description', $headers['Description'] );
$translation->addReference( $options['file'] );
$translation = Translation::create( 'Pattern description', $headers['Description'] );
$translation->getReferences()->add( $options['file'] );

Check warning on line 99 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L98-L99

Added lines #L98 - L99 were not covered by tests

$translations[] = $translation;
$translations->add( $translation );

Check warning on line 101 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L101

Added line #L101 was not covered by tests
}
}

Expand Down Expand Up @@ -339,4 +339,37 @@
protected static function trim_leading_slash( $path ) {
return ltrim( $path, '/' );
}

/**
* Formerly part php-gettext 4.x.
*/
protected static function getFiles( $file ) {
if ( empty( $file ) ) {
throw new InvalidArgumentException( 'There is not any file defined' );

Check warning on line 348 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L346-L348

Added lines #L346 - L348 were not covered by tests
}

if ( is_string( $file ) ) {
if ( ! is_file( $file ) ) {
throw new InvalidArgumentException( "'$file' is not a valid file" );

Check warning on line 353 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L351-L353

Added lines #L351 - L353 were not covered by tests
}

if ( ! is_readable( $file ) ) {
throw new InvalidArgumentException( "'$file' is not a readable file" );

Check warning on line 357 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L356-L357

Added lines #L356 - L357 were not covered by tests
}

return [ $file ];

Check warning on line 360 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L360

Added line #L360 was not covered by tests
}

if ( is_array( $file ) ) {
$files = [];

Check warning on line 364 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L363-L364

Added lines #L363 - L364 were not covered by tests

foreach ( $file as $f ) {
$files = array_merge( $files, self::getFiles( $f ) );

Check warning on line 367 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L366-L367

Added lines #L366 - L367 were not covered by tests
}

return $files;

Check warning on line 370 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L370

Added line #L370 was not covered by tests
}

throw new InvalidArgumentException( 'The first argument must be string or array' );

Check warning on line 373 in src/IterableCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/IterableCodeExtractor.php#L373

Added line #L373 was not covered by tests
}
}
109 changes: 60 additions & 49 deletions src/JedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WP_CLI\I18n;

use Gettext\Generators\Jed;
use Gettext\Generator\Generator;
use Gettext\Translation;
use Gettext\Translations;

Expand All @@ -11,72 +11,83 @@
*
* Adds some more meta data to JED translation files than the default generator.
*/
class JedGenerator extends Jed {
class JedGenerator extends Generator {
/**
* {@parentDoc}.
* Options passed to json_encode().
*
* @var int JSON options.
*/
public static function toString( Translations $translations, array $options = [] ) {
$options += static::$options;
$domain = $translations->getDomain() ?: 'messages';
$messages = static::buildMessages( $translations );

$configuration = [
'' => [
'domain' => $domain,
'lang' => $translations->getLanguage() ?: 'en',
'plural-forms' => $translations->getHeader( 'Plural-Forms' ) ?: 'nplurals=2; plural=(n != 1);',
],
];

$data = [
'translation-revision-date' => $translations->getHeader( 'PO-Revision-Date' ),
'generator' => 'WP-CLI/' . WP_CLI_VERSION,
'source' => $options['source'],
'domain' => $domain,
'locale_data' => [
$domain => $configuration + $messages,
],
];

return json_encode( $data, $options['json'] );
}
protected $json_options = 0;

/**
* Generates an array with all translations.
* Source file.
*
* @param Translations $translations
* @var string Source file.
*/
protected $source = '';

/**
* Constructor.
*
* @return array
* @param int $json_options Options passed to json_encode().
* @param string $source Source file.
*/
public static function buildMessages( Translations $translations ) {
$plural_forms = $translations->getPluralForms();
$number_of_plurals = is_array( $plural_forms ) ? ( $plural_forms[0] - 1 ) : null;
$messages = [];
$context_glue = chr( 4 );
public function __construct( int $json_options, string $source ) {
$this->json_options = $json_options;
$this->source = $source;

Check warning on line 37 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L35-L37

Added lines #L35 - L37 were not covered by tests
}

foreach ( $translations as $translation ) {
/** @var Translation $translation */
public function generateString( Translations $translations ): string {
$array = $this->generateArray( $translations );

Check warning on line 41 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L40-L41

Added lines #L40 - L41 were not covered by tests

return json_encode( $array, $this->json_options );

Check warning on line 43 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L43

Added line #L43 was not covered by tests
}

if ( $translation->isDisabled() ) {
public function generateArray( Translations $translations ): array {
$pluralForm = $translations->getHeaders()->getPluralForm();

Check failure on line 47 in src/JedGenerator.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Variable "$pluralForm" is not in valid snake_case format, try "$plural_form"
$pluralSize = is_array( $pluralForm ) ? ( $pluralForm[0] - 1 ) : null;

Check failure on line 48 in src/JedGenerator.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Variable "$pluralForm" is not in valid snake_case format, try "$plural_form"

Check failure on line 48 in src/JedGenerator.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Variable "$pluralForm" is not in valid snake_case format, try "$plural_form"

Check failure on line 48 in src/JedGenerator.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Variable "$pluralSize" is not in valid snake_case format, try "$plural_size"
$messages = [];

Check warning on line 49 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L46-L49

Added lines #L46 - L49 were not covered by tests

foreach ( $translations as $translation ) {
if ( ! $translation->getTranslation() || $translation->isDisabled() ) {

Check warning on line 52 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L51-L52

Added lines #L51 - L52 were not covered by tests
continue;
}

$key = $translation->getOriginal();
$context = $translation->getContext() ?: '';
$original = $translation->getOriginal();

Check warning on line 57 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L56-L57

Added lines #L56 - L57 were not covered by tests

if ( $translation->hasContext() ) {
$key = $translation->getContext() . $context_glue . $key;
if ( ! isset( $messages[ $context ] ) ) {
$messages[ $context ] = [];

Check warning on line 60 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L59-L60

Added lines #L59 - L60 were not covered by tests
}

if ( $translation->hasPluralTranslations( true ) ) {
$message = $translation->getPluralTranslations( $number_of_plurals );
array_unshift( $message, $translation->getTranslation() );
if ( self::hasPluralTranslations( $translation ) ) {
$messages[ $context ][ $original ] = $translation->getPluralTranslations( $pluralSize );

Check failure on line 64 in src/JedGenerator.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Variable "$pluralSize" is not in valid snake_case format, try "$plural_size"
array_unshift( $messages[ $context ][ $original ], $translation->getTranslation() );

Check warning on line 65 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L63-L65

Added lines #L63 - L65 were not covered by tests
} else {
$message = [ $translation->getTranslation() ];
$messages[ $context ][ $original ] = $translation->getTranslation();

Check warning on line 67 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L67

Added line #L67 was not covered by tests
}

$messages[ $key ] = $message;
}

return $messages;
$configuration = [
'' => [
'domain' => $translations->getDomain(),
'lang' => $translations->getLanguage() ?: 'en',
'plural-forms' => $translations->getHeaders()->getPluralForm() ?: 'nplurals=2; plural=(n != 1);',
],
];

Check warning on line 77 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L71-L77

Added lines #L71 - L77 were not covered by tests

return [
'translation-revision-date' => $translations->getHeaders()->get( 'PO-Revision-Date' ),
'generator' => 'WP-CLI/' . WP_CLI_VERSION,
'source' => $this->source,
'domain' => $translations->getDomain(),
'locale_data' => [
$translations->getDomain() => $configuration + $messages,
],
];

Check warning on line 87 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L79-L87

Added lines #L79 - L87 were not covered by tests
}

private static function hasPluralTranslations( Translation $translation ): bool {
return implode( '', $translation->getPluralTranslations() ) !== '';

Check warning on line 91 in src/JedGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/JedGenerator.php#L90-L91

Added lines #L90 - L91 were not covered by tests
}
}
39 changes: 16 additions & 23 deletions src/JsCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,40 @@
namespace WP_CLI\I18n;

use Exception;
use Gettext\Extractors\JsCode;
use Gettext\Scanner\JsScanner;
use Gettext\Translations;
use Peast\Syntax\Exception as PeastException;
use WP_CLI;

final class JsCodeExtractor extends JsCode {
final class JsCodeExtractor {
use IterableCodeExtractor;

protected $functions = [
'__' => 'text_domain',
'_x' => 'text_context_domain',
'_n' => 'single_plural_number_domain',
'_nx' => 'single_plural_number_context_domain',
];

public static $options = [
'extractComments' => [ 'translators', 'Translators' ],
'constants' => [],
'functions' => [
'__' => 'text_domain',
'_x' => 'text_context_domain',
'_n' => 'single_plural_number_domain',
'_nx' => 'single_plural_number_context_domain',
],
'functions' => [],
];

protected static $functionsScannerClass = 'WP_CLI\I18n\JsFunctionsScanner';

/**
* @inheritdoc
*/
public static function fromString( $text, Translations $translations, array $options = [] ) {

Check failure on line 30 in src/JsCodeExtractor.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Method name "fromString" in class JsCodeExtractor is not in snake case format, try "from_string"
WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' );

try {
self::fromStringMultiple( $text, [ $translations ], $options );
$options += self::$options;

Check warning on line 34 in src/JsCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/JsCodeExtractor.php#L34

Added line #L34 was not covered by tests

$scanner = new JsScanner( $translations );
$scanner->setFunctions( self::$options['functions'] );
$scanner->extractCommentsStartingWith( $options['extractComments'] );
$scanner->scanString( $text, $options['file'] );

Check warning on line 39 in src/JsCodeExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/JsCodeExtractor.php#L36-L39

Added lines #L36 - L39 were not covered by tests
} catch ( PeastException $exception ) {
WP_CLI::debug(
sprintf(
Expand All @@ -54,16 +59,4 @@
);
}
}

/**
* @inheritDoc
*/
public static function fromStringMultiple( $text, array $translations, array $options = [] ) {
$options += self::$options;

/** @var JsFunctionsScanner $functions */
$functions = new self::$functionsScannerClass( $text );
$functions->enableCommentsExtraction( $options['extractComments'] );
$functions->saveGettextFunctions( $translations, $options );
}
}
4 changes: 2 additions & 2 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
$translation = $translations->insert( $context, $original, $plural );

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

Check warning on line 181 in src/JsFunctionsScanner.php

View check run for this annotation

Codecov / codecov/patch

src/JsFunctionsScanner.php#L181

Added line #L181 was not covered by tests
}

if (
Expand All @@ -203,7 +203,7 @@
$prefixes = array_filter( (array) $this->extract_comments );

if ( $parsed_comment->checkPrefixes( $prefixes ) ) {
$translation->addExtractedComment( $parsed_comment->getComment() );
$translation->getComments()->add( $parsed_comment->getComment() );

Check warning on line 206 in src/JsFunctionsScanner.php

View check run for this annotation

Codecov / codecov/patch

src/JsFunctionsScanner.php#L206

Added line #L206 was not covered by tests

$this->comments_cache[] = $comment;
}
Expand Down
10 changes: 6 additions & 4 deletions src/JsonSchemaExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace WP_CLI\I18n;

use Gettext\Extractors\Extractor;
use Gettext\Translation;
use Gettext\Translations;
use WP_CLI;
use WP_CLI\Utils;

class JsonSchemaExtractor extends Extractor {
class JsonSchemaExtractor {
use IterableCodeExtractor;

/**
Expand Down Expand Up @@ -82,7 +82,7 @@
/**
* @inheritdoc
*/
public static function fromString( $text, Translations $translations, array $options = [] ) {

Check failure on line 85 in src/JsonSchemaExtractor.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Method name "fromString" in class JsonSchemaExtractor is not in snake case format, try "from_string"
$file = $options['file'];
WP_CLI::debug( "Parsing file {$file}", 'make-pot' );

Expand Down Expand Up @@ -127,12 +127,14 @@
}

if ( is_string( $i18n_schema ) && is_string( $settings ) ) {
$translation = $translations->insert( $i18n_schema, $settings );
$translation = Translation::create( $i18n_schema, $settings );

Check warning on line 130 in src/JsonSchemaExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/JsonSchemaExtractor.php#L130

Added line #L130 was not covered by tests

if ( $file ) {
$translation->addReference( $file );
$translation->getReferences()->add( $file );

Check warning on line 133 in src/JsonSchemaExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/JsonSchemaExtractor.php#L133

Added line #L133 was not covered by tests
}

$translations->add( $translation );

Check warning on line 136 in src/JsonSchemaExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/JsonSchemaExtractor.php#L136

Added line #L136 was not covered by tests

return;
}

Expand Down
Loading
Loading