From 7995f6bcd6015e7df6da10cc0046870f039a7ba5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 17 Nov 2026 10:40:53 +0000 Subject: [PATCH 01/14] Migrate to gettext/gettext v5 gettext/gettext v5 no longer ships extractors, the Jed and PHP array generators or the PHP functions scanner, and changes the data model (immutable merges, nullable getters, sorted headers and flags, references grouped by file). To keep the output of all commands unchanged: * Add `Translation` and `Translations` classes on top of the v5 ones that keep headers, flags and references in the order they were added and provide the default headers that v4 used to set. * Add a PO loader and PO/MO generators. The ones in v5 treat "0" as an empty string, which drops `msgid "0"` entries and `msgstr "0"` translations when reading, merging and writing files. * Port the PHP functions scanner from v4, as it is no longer part of the library. * Make the extractors and the Jed and PHP array generators standalone classes. * Implement the merge strategies used by `update-po` and `make-pot --merge/--subtract-and-merge` on top of the v5 API. Two intentional fixes on the way: headers of a PO file that only consists of a header entry without a trailing newline are no longer ignored, and strings with `\u{...}` escapes no longer trigger a `hexdec()` deprecation. --- .gitattributes | 1 - composer.json | 2 +- features/makemo.feature | 28 +++ features/makephp.feature | 36 ++++ features/makepot.feature | 111 ++++++++++ features/updatepo.feature | 124 +++++++++++ phpcs.xml.dist | 6 +- phpstan.neon.dist | 2 - src/AuditCommand.php | 37 +--- src/BladeCodeExtractor.php | 10 +- src/BladeGettextExtractor.php | 26 +-- src/BlockExtractor.php | 1 - src/Generator.php | 29 +++ src/IterableCodeExtractor.php | 66 ++++-- src/JedGenerator.php | 31 +-- src/JsCodeExtractor.php | 47 ++--- src/JsFunctionsScanner.php | 49 +++-- src/JsStringFilterTrait.php | 16 +- src/JsonSchemaExtractor.php | 8 +- src/MakeJsonCommand.php | 27 +-- src/MakeMoCommand.php | 5 +- src/MakePhpCommand.php | 3 +- src/MakePotCommand.php | 95 ++++----- src/MapCodeExtractor.php | 12 +- src/MoGenerator.php | 152 ++++++++++++++ src/OrderedFlags.php | 21 ++ src/OrderedHeaders.php | 36 ++++ src/OrderedReferences.php | 79 +++++++ src/ParsedComment.php | 120 +++++++++++ src/ParsedFunction.php | 141 +++++++++++++ src/PhpArrayGenerator.php | 11 +- src/PhpCodeExtractor.php | 39 ++-- src/PhpFunctionsScanner.php | 306 ++++++++++++++++++++++++++-- src/PoGenerator.php | 164 +++++++++++++++ src/PoLoader.php | 265 ++++++++++++++++++++++++ src/PotGenerator.php | 132 ++---------- src/Translation.php | 82 ++++++++ src/Translations.php | 56 +++++ src/UpdatePoCommand.php | 92 +++++---- stubs/Gettext.stub | 36 ---- tests/BladeGettextExtractorTest.php | 40 ++-- tests/JsFunctionsScannerTest.php | 20 +- tests/PhpFunctionsScannerTest.php | 106 ++++++++++ tests/PoLoaderTest.php | 191 +++++++++++++++++ tests/PotGeneratorTest.php | 9 +- tests/TranslationTest.php | 131 ++++++++++++ 46 files changed, 2496 insertions(+), 505 deletions(-) create mode 100644 src/Generator.php create mode 100644 src/MoGenerator.php create mode 100644 src/OrderedFlags.php create mode 100644 src/OrderedHeaders.php create mode 100644 src/OrderedReferences.php create mode 100644 src/ParsedComment.php create mode 100644 src/ParsedFunction.php create mode 100644 src/PoGenerator.php create mode 100644 src/PoLoader.php create mode 100644 src/Translation.php create mode 100644 src/Translations.php delete mode 100644 stubs/Gettext.stub create mode 100644 tests/PhpFunctionsScannerTest.php create mode 100644 tests/PoLoaderTest.php create mode 100644 tests/TranslationTest.php diff --git a/.gitattributes b/.gitattributes index 0708b6bd..d84f4ade 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11,5 +11,4 @@ /phpstan.neon.dist export-ignore /phpunit.xml.dist export-ignore /tests export-ignore -/stubs export-ignore /wp-cli.yml export-ignore diff --git a/composer.json b/composer.json index bcf758d8..d2d880d6 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "eftec/bladeone": "3.52", - "gettext/gettext": "^4.8", + "gettext/gettext": "^5.7", "mck89/peast": "^1.13.11", "wp-cli/wp-cli": "^3.0" }, diff --git a/features/makemo.feature b/features/makemo.feature index ae0e2585..0814ff9d 100644 --- a/features/makemo.feature +++ b/features/makemo.feature @@ -254,3 +254,31 @@ Feature: Generate MO files from PO files JS Only Translation """ + + Scenario: Includes translations that are "0" + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin-de_DE.po file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "Language: de_DE\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:10 + msgctxt "start of week" + msgid "1" + msgstr "0" + """ + + When I run `wp i18n make-mo foo-plugin` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And the foo-plugin/foo-plugin-de_DE.mo file should contain: + """ + start of week + """ diff --git a/features/makephp.feature b/features/makephp.feature index eda279cc..431d204d 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -400,3 +400,39 @@ Feature: Generate PHP files from PO files [, """ + + Scenario: Includes translations that are "0" + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin-de_DE.po file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "Language: de_DE\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:10 + msgctxt "start of week" + msgid "1" + msgstr "0" + + #: foo-plugin.php:20 + msgid "0" + msgstr "Null" + """ + + When I run `wp i18n make-php foo-plugin --pretty-print` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: + """ + 1' => '0', + """ + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: + """ + 0 => 'Null', + """ diff --git a/features/makepot.feature b/features/makepot.feature index 1abb35d7..ecbef8d4 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -4401,3 +4401,114 @@ Feature: Generate a POT file of a WordPress project #, js-format msgid "Buongiorno %1$s" """ + + Scenario: Subtracts and merges strings that are "0" + Given an empty foo-plugin directory + And a foo-plugin/exception.pot file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + + #: other.php:1 + msgid "0" + msgstr "" + """ + And a foo-plugin/existing.pot file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + + #: existing.php:1 + msgctxt "number" + msgid "0" + msgstr "" + """ + And a foo-plugin/foo-plugin.php file: + """ + - + - */src/IterableCodeExtractor\.php$ + */src/*(Extractor|Generator|Scanner)\.php$ + */src/Parsed(Comment|Function)\.php$ @@ -65,7 +66,6 @@ - diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9ec6ffe8..6d6c2689 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -11,5 +11,3 @@ parameters: treatPhpDocTypesAsCertain: false - stubFiles: - - stubs/Gettext.stub diff --git a/src/AuditCommand.php b/src/AuditCommand.php index dcdf1c37..b53f92aa 100644 --- a/src/AuditCommand.php +++ b/src/AuditCommand.php @@ -2,9 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Translation; -use Gettext\Translations; -use Gettext\Utils\ParsedComment; use WP_CLI; use WP_CLI\Path; use WP_CLI\Utils; @@ -268,7 +265,7 @@ protected function get_main_file_data() { * @return Translations A Translation set. */ protected function extract_strings_for_audit() { - $translations = new Translations(); + $translations = Translations::create(); if ( $this->domain ) { $translations->setDomain( $this->domain ); @@ -383,16 +380,6 @@ protected function extract_strings_for_audit() { return $translations; } - /** - * Extracts comment text from a ParsedComment or string. - * - * @param ParsedComment|string $comment Comment object or string. - * @return string Comment text. - */ - protected function get_comment_text( $comment ) { - return $comment instanceof ParsedComment ? $comment->getComment() : $comment; - } - /** * Collects audit issues from translations. * @@ -407,19 +394,21 @@ protected function collect_audit_issues( $translations ) { foreach ( $translations as $translation ) { /** @var Translation $translation */ - $references = $translation->getReferences(); + $references = $translation->getReferenceEntries(); // File headers don't have any file references. - if ( ! $translation->hasReferences() ) { + if ( empty( $references ) ) { continue; } $file = $references[0][0]; - $line = $references[0][1] ?? null; + $line = $references[0][1]; + + $comments = $translation->getExtractedComments()->toArray(); // Check 1: Flag strings with placeholders that should have translator comments. if ( - ! $translation->hasExtractedComments() && + empty( $comments ) && preg_match( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal() ) >= 1 ) { $issues[] = [ @@ -434,15 +423,13 @@ protected function collect_audit_issues( $translations ) { } // Check 2: Flag strings with different translator comments. - if ( $translation->hasExtractedComments() ) { - $comments = $translation->getExtractedComments(); - + if ( ! empty( $comments ) ) { // Remove plugin header information from comments. $comments = array_filter( $comments, function ( $comment ) { foreach ( $this->get_file_headers( $this->project_type ) as $file_header ) { - if ( 0 === strpos( $this->get_comment_text( $comment ), $file_header ) ) { + if ( 0 === strpos( $comment, $file_header ) ) { return false; } } @@ -451,9 +438,7 @@ function ( $comment ) { } ); - $unique_comment_texts = array_unique( - array_map( [ $this, 'get_comment_text' ], $comments ) - ); + $unique_comment_texts = array_unique( $comments ); $comments_count = count( $unique_comment_texts ); @@ -501,7 +486,7 @@ function ( $comment ) { preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $single_placeholders ); $single_placeholders = $single_placeholders[0]; - preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getPlural(), $plural_placeholders ); + preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, (string) $translation->getPlural(), $plural_placeholders ); $plural_placeholders = $plural_placeholders[0]; // see https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals diff --git a/src/BladeCodeExtractor.php b/src/BladeCodeExtractor.php index c289f51f..8d2a4f5c 100644 --- a/src/BladeCodeExtractor.php +++ b/src/BladeCodeExtractor.php @@ -3,7 +3,6 @@ namespace WP_CLI\I18n; use Exception; -use Gettext\Translations; use WP_CLI; final class BladeCodeExtractor extends BladeGettextExtractor { @@ -46,12 +45,7 @@ final class BladeCodeExtractor extends BladeGettextExtractor { ]; /** - * @var string - */ - protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner'; - - /** - * {@inheritdoc} + * Extracts translations from a Blade template. * * @param string $text The text to extract strings from. * @param Translations $translations Translations instance. @@ -63,7 +57,7 @@ public static function fromString( $text, Translations $translations, array $opt WP_CLI::debug( "Parsing file {$file}", 'make-pot' ); try { - self::fromStringMultiple( $text, [ $translations ], $options ); + PhpCodeExtractor::scan( self::toPhp( $text, $options ), $translations, $options + self::$options ); } catch ( Exception $exception ) { WP_CLI::debug( sprintf( diff --git a/src/BladeGettextExtractor.php b/src/BladeGettextExtractor.php index 66fa844e..7b1b1b54 100644 --- a/src/BladeGettextExtractor.php +++ b/src/BladeGettextExtractor.php @@ -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. + * Turns Blade templates into PHP code using the BladeOne standalone Blade engine. */ -class BladeGettextExtractor extends \Gettext\Extractors\PhpCode { +class BladeGettextExtractor { /** * Prepares a Blade compiler/engine and returns it. @@ -73,19 +69,13 @@ protected static function extractComponentPropExpressions( $text ) { } /** - * {@inheritdoc} - * - * @param string $text The text to extract strings from. - * @param array<\Gettext\Translations> $translations Translations instances. - * @param array $options Options. - * @return void + * Returns the PHP code to scan for a Blade template. * - * Note: In the parent PhpCode class fromString() uses fromStringMultiple() (overridden here) + * @param string $text Blade template string. + * @param array $options Options array. + * @return string */ - public static function fromStringMultiple( $text, array $translations, array $options = [] ) { - $php_string = static::compileBladeToPhp( $text, $options ); - - $php_string .= static::extractComponentPropExpressions( $text ); - parent::fromStringMultiple( $php_string, $translations, $options ); + protected static function toPhp( $text, array $options = [] ) { + return static::compileBladeToPhp( $text, $options ) . static::extractComponentPropExpressions( $text ); } } diff --git a/src/BlockExtractor.php b/src/BlockExtractor.php index 3d7d0e12..c4edd69b 100644 --- a/src/BlockExtractor.php +++ b/src/BlockExtractor.php @@ -2,7 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Translations; use WP_CLI; final class BlockExtractor extends JsonSchemaExtractor { diff --git a/src/Generator.php b/src/Generator.php new file mode 100644 index 00000000..0c910069 --- /dev/null +++ b/src/Generator.php @@ -0,0 +1,29 @@ + $options Generator options. + * @return bool Whether the file was written. + */ + public static function toFile( Translations $translations, $file, array $options = [] ) { + return false !== file_put_contents( $file, static::toString( $translations, $options ) ); + } + + /** + * Generates a string with the translations ready to save in a file. + * + * @param Translations $translations Translations instance. + * @param array $options Generator options. + * @return string + */ + abstract public static function toString( Translations $translations, array $options = [] ); +} diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index a98faf94..060eac62 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -2,8 +2,7 @@ namespace WP_CLI\I18n; -use Gettext\Translation; -use Gettext\Translations; +use InvalidArgumentException; use RecursiveCallbackFilterIterator; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; @@ -75,15 +74,13 @@ public static function fromFile( $file_or_files, Translations $translations, arr $headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ] ); if ( ! empty( $headers['Template Name']['value'] ) ) { - $translation = new Translation( '', $headers['Template Name']['value'] ); - $translation->addExtractedComment( 'Template Name of the theme' ); + $translation = $translations->insert( null, $headers['Template Name']['value'] ); + $translation->getExtractedComments()->add( 'Template Name of the theme' ); // Add file reference with line number if available if ( ! empty( $headers['Template Name']['line'] ) ) { - $translation->addReference( $options['file'], $headers['Template Name']['line'] ); + $translation->getReferences()->add( $options['file'], $headers['Template Name']['line'] ); } - - $translations[] = $translation; } } @@ -98,29 +95,25 @@ public static function fromFile( $file_or_files, Translations $translations, arr ); if ( ! empty( $headers['Title']['value'] ) ) { - $translation = new Translation( 'Pattern title', $headers['Title']['value'] ); + $translation = $translations->insert( 'Pattern title', $headers['Title']['value'] ); // Add file reference with line number if available if ( ! empty( $headers['Title']['line'] ) ) { - $translation->addReference( $options['file'], $headers['Title']['line'] ); + $translation->getReferences()->add( $options['file'], $headers['Title']['line'] ); } else { - $translation->addReference( $options['file'] ); + $translation->getReferences()->add( $options['file'] ); } - - $translations[] = $translation; } if ( ! empty( $headers['Description']['value'] ) ) { - $translation = new Translation( 'Pattern description', $headers['Description']['value'] ); + $translation = $translations->insert( 'Pattern description', $headers['Description']['value'] ); // Add file reference with line number if available if ( ! empty( $headers['Description']['line'] ) ) { - $translation->addReference( $options['file'], $headers['Description']['line'] ); + $translation->getReferences()->add( $options['file'], $headers['Description']['line'] ); } else { - $translation->addReference( $options['file'] ); + $translation->getReferences()->add( $options['file'] ); } - - $translations[] = $translation; } } @@ -130,6 +123,45 @@ public static function fromFile( $file_or_files, Translations $translations, arr return null; } + /** + * Checks and returns all files. + * + * @param array|string $file_or_files A path of a file or files. + * @return array The file paths. + * + * @throws InvalidArgumentException If a file does not exist or is not readable. + */ + protected static function getFiles( $file_or_files ) { + if ( empty( $file_or_files ) ) { + throw new InvalidArgumentException( 'There is not any file defined' ); + } + + if ( is_string( $file_or_files ) ) { + if ( ! is_file( $file_or_files ) ) { + throw new InvalidArgumentException( "'$file_or_files' is not a valid file" ); + } + + if ( ! is_readable( $file_or_files ) ) { + throw new InvalidArgumentException( "'$file_or_files' is not a readable file" ); + } + + return [ $file_or_files ]; + } + + if ( is_array( $file_or_files ) ) { + $files = []; + + foreach ( $file_or_files as $file ) { + /** @var array|string $file */ + $files = array_merge( $files, static::getFiles( $file ) ); + } + + return $files; + } + + throw new InvalidArgumentException( 'The first argument must be string or array' ); + } + /** * Extract the translations from a file. * diff --git a/src/JedGenerator.php b/src/JedGenerator.php index a618d2f3..d21c7c5a 100644 --- a/src/JedGenerator.php +++ b/src/JedGenerator.php @@ -2,7 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Generators\Jed; use Gettext\Translation; use Gettext\Translations; @@ -11,9 +10,17 @@ * * Adds some more meta data to JED translation files than the default generator. */ -class JedGenerator extends Jed { +class JedGenerator extends Generator { /** - * {@parentDoc}. + * @var array + */ + public static $options = [ + 'json' => 0, + 'source' => '', + ]; + + /** + * {@inheritdoc} * * @param Translations $translations Translations instance. * @param array $options Options. @@ -28,12 +35,12 @@ public static function toString( Translations $translations, array $options = [] '' => [ 'domain' => $domain, 'lang' => $translations->getLanguage() ?: 'en', - 'plural-forms' => $translations->getHeader( 'Plural-Forms' ) ?: 'nplurals=2; plural=(n != 1);', + 'plural-forms' => $translations->getHeaders()->get( 'Plural-Forms' ) ?: 'nplurals=2; plural=(n != 1);', ], ]; $data = [ - 'translation-revision-date' => $translations->getHeader( 'PO-Revision-Date' ), + 'translation-revision-date' => $translations->getHeaders()->get( 'PO-Revision-Date' ), 'generator' => 'WP-CLI/' . WP_CLI_VERSION, 'source' => $options['source'], 'domain' => $domain, @@ -42,7 +49,7 @@ public static function toString( Translations $translations, array $options = [] ], ]; - $result = json_encode( $data, $options['json'] ); + $result = json_encode( $data, is_int( $options['json'] ) ? $options['json'] : 0 ); return false === $result ? '' : $result; } @@ -54,8 +61,8 @@ public static function toString( Translations $translations, array $options = [] * @return array> */ public static function buildMessages( Translations $translations ) { - $plural_forms = $translations->getPluralForms(); - $number_of_plurals = is_array( $plural_forms ) && is_numeric( $plural_forms[0] ) ? ( (int) $plural_forms[0] ) - 1 : null; + $plural_forms = $translations->getHeaders()->getPluralForm(); + $number_of_plurals = is_array( $plural_forms ) ? $plural_forms[0] - 1 : null; $messages = []; $context_glue = chr( 4 ); @@ -68,15 +75,15 @@ public static function buildMessages( Translations $translations ) { $key = $translation->getOriginal(); - if ( $translation->hasContext() ) { + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { $key = $translation->getContext() . $context_glue . $key; } - if ( $translation->hasPluralTranslations( true ) ) { + if ( '' !== implode( '', $translation->getPluralTranslations() ) ) { $message = $translation->getPluralTranslations( $number_of_plurals ); - array_unshift( $message, $translation->getTranslation() ); + array_unshift( $message, (string) $translation->getTranslation() ); } else { - $message = [ $translation->getTranslation() ]; + $message = [ (string) $translation->getTranslation() ]; } $messages[ $key ] = $message; diff --git a/src/JsCodeExtractor.php b/src/JsCodeExtractor.php index 07027a4c..f902b82e 100644 --- a/src/JsCodeExtractor.php +++ b/src/JsCodeExtractor.php @@ -3,12 +3,10 @@ namespace WP_CLI\I18n; use Exception; -use Gettext\Extractors\JsCode; -use Gettext\Translations; use Peast\Syntax\Exception as PeastException; use WP_CLI; -final class JsCodeExtractor extends JsCode { +final class JsCodeExtractor { use IterableCodeExtractor; /** @@ -26,12 +24,7 @@ final class JsCodeExtractor extends JsCode { ]; /** - * @var string - */ - protected static $functionsScannerClass = 'WP_CLI\I18n\JsFunctionsScanner'; - - /** - * {@inheritdoc} + * Extracts translations from JavaScript code. * * @param string $text The text to extract strings from. * @param Translations $translations Translations instance. @@ -43,7 +36,17 @@ public static function fromString( $text, Translations $translations, array $opt WP_CLI::debug( "Parsing file {$file}", 'make-pot' ); try { - self::fromStringMultiple( $text, [ $translations ], $options ); + $options += self::$options; + + $scanner = new JsFunctionsScanner( $text ); + + if ( isset( $options['extractComments'] ) && ( is_string( $options['extractComments'] ) || is_array( $options['extractComments'] ) ) ) { + /** @var array|string $extract_comments */ + $extract_comments = $options['extractComments']; + $scanner->enableCommentsExtraction( $extract_comments ); + } + + $scanner->saveGettextFunctions( $translations, $options ); } catch ( PeastException $exception ) { WP_CLI::debug( sprintf( @@ -66,28 +69,4 @@ public static function fromString( $text, Translations $translations, array $opt ); } } - - /** - * {@inheritdoc} - * - * @param string $text The text to extract strings from. - * @param array<\Gettext\Translations> $translations Translations instances. - * @param array $options Extraction options. - * @return void - */ - public static function fromStringMultiple( $text, array $translations, array $options = [] ) { - $options += self::$options; - - /** @var JsFunctionsScanner $functions */ - $functions = new self::$functionsScannerClass( $text ); - if ( isset( $options['extractComments'] ) && ( is_string( $options['extractComments'] ) || is_array( $options['extractComments'] ) ) ) { - /** @var array|string $extract_comments */ - $extract_comments = $options['extractComments']; - $functions->enableCommentsExtraction( $extract_comments ); - } - - if ( ! empty( $translations ) ) { - $functions->saveGettextFunctions( $translations[0], $options ); - } - } } diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index 3f1d74dd..605ccab9 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -2,14 +2,19 @@ namespace WP_CLI\I18n; -use Gettext\Utils\JsFunctionsScanner as GettextJsFunctionsScanner; -use Gettext\Utils\ParsedComment; use WP_CLI; use Peast\Peast; use Peast\Syntax\Node; use Peast\Traverser; -final class JsFunctionsScanner extends GettextJsFunctionsScanner { +final class JsFunctionsScanner { + + /** + * The JavaScript code to scan. + * + * @var string + */ + private $code; /** * If not false, comments will be extracted. @@ -27,6 +32,14 @@ final class JsFunctionsScanner extends GettextJsFunctionsScanner { */ private $comments_cache = []; + /** + * @param string $code The JavaScript code to scan. + */ + public function __construct( $code ) { + // Normalize newline characters. + $this->code = str_replace( [ "\r\n", "\n\r", "\r" ], "\n", $code ); + } + /** * Enable extracting comments that start with a tag (if $tag is empty all the comments will be extracted). * @@ -47,19 +60,13 @@ public function disableCommentsExtraction() { } /** - * {@inheritdoc} + * Searches for specific functions and creates translations. * - * @param \Gettext\Translations $translations Translations instance. - * @param array $options Options. + * @param Translations $translations Translations instance. + * @param array $options Options. * @return void */ - public function saveGettextFunctions( $translations, array $options ) { - // Ignore multiple translations for now. - // @todo Add proper support for multiple translations. - if ( is_array( $translations ) ) { - $translations = $translations[0]; - } - + public function saveGettextFunctions( Translations $translations, array $options ) { $functions = isset( $options['functions'] ) && is_array( $options['functions'] ) ? array_keys( $options['functions'] ) : []; // Parsing is by far the most expensive part, so skip it for code that @@ -97,7 +104,7 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) { if ( ! is_array( $functions ) ) { return; } - $file = $options['file']; + $file = isset( $options['file'] ) && is_scalar( $options['file'] ) ? (string) $options['file'] : ''; $add_reference = ! empty( $options['addReferences'] ); foreach ( $node->getLeadingComments() as $comment ) { @@ -200,22 +207,26 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) { } if ( isset( $options['line'] ) ) { - $line = $options['line']; + $line = is_numeric( $options['line'] ) ? (int) $options['line'] : null; } else { $line = $node->getLocation()->getStart()->getLine(); } - $translation = $translations->insert( $context, $original, $plural ); + $translation = $translations->insert( + is_scalar( $context ) ? (string) $context : null, + $original, + is_scalar( $plural ) ? (string) $plural : null + ); if ( $add_reference ) { - $translation->addReference( $file, $line ); + $translation->getReferences()->add( $file, $line ); } if ( 1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original ) || 1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original ) ) { - $translation->addFlag( 'js-format' ); + $translation->getFlags()->add( 'js-format' ); } /** @var Node\Comment $comment */ @@ -233,7 +244,7 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) { $prefixes = array_filter( (array) $this->extract_comments ); if ( $parsed_comment->checkPrefixes( $prefixes ) ) { - $translation->addExtractedComment( $parsed_comment->getComment() ); + $translation->getExtractedComments()->add( $parsed_comment->getComment() ); $this->comments_cache[] = $comment; } diff --git a/src/JsStringFilterTrait.php b/src/JsStringFilterTrait.php index c1ace362..8880bc92 100644 --- a/src/JsStringFilterTrait.php +++ b/src/JsStringFilterTrait.php @@ -2,9 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Translation; -use Gettext\Translations; - trait JsStringFilterTrait { /** * JavaScript file extensions to check for. @@ -19,17 +16,16 @@ trait JsStringFilterTrait { * @param Translations $translations The translations instance to filter. */ protected function remove_js_only_strings( Translations $translations ): void { - foreach ( $translations->getArrayCopy() as $translation ) { - /** @var Translation $translation */ + foreach ( $translations->getTranslations() as $translation ) { + $files = array_keys( $translation->getReferences()->toArray() ); - if ( ! $translation->hasReferences() ) { + if ( empty( $files ) ) { continue; } $has_non_js_reference = false; - foreach ( $translation->getReferences() as $reference ) { - $file = $reference[0]; - $extension = '.' . strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ); + foreach ( $files as $file ) { + $extension = '.' . strtolower( pathinfo( (string) $file, PATHINFO_EXTENSION ) ); if ( ! in_array( $extension, static::$js_extensions, true ) ) { $has_non_js_reference = true; @@ -39,7 +35,7 @@ protected function remove_js_only_strings( Translations $translations ): void { // If all references are JS files, remove this translation. if ( ! $has_non_js_reference ) { - unset( $translations[ $translation->getId() ] ); + $translations->remove( $translation ); } } } diff --git a/src/JsonSchemaExtractor.php b/src/JsonSchemaExtractor.php index 2ea41f55..c9656a94 100644 --- a/src/JsonSchemaExtractor.php +++ b/src/JsonSchemaExtractor.php @@ -2,13 +2,11 @@ namespace WP_CLI\I18n; -use Gettext\Extractors\Extractor; -use Gettext\Translations; use RuntimeException; use WP_CLI; use WP_CLI\Utils; -class JsonSchemaExtractor extends Extractor { +class JsonSchemaExtractor { use IterableCodeExtractor; /** @@ -86,7 +84,7 @@ protected static function load_schema( $schema, $fallback ) { } /** - * {@inheritdoc} + * Extracts translations from a JSON file using its i18n schema. * * @param string $text The text to extract strings from. * @param Translations $translations Translations instance. @@ -155,7 +153,7 @@ private static function extract_strings_using_i18n_schema( Translations $transla $translation = $translations->insert( $i18n_schema, $settings ); if ( $file ) { - $translation->addReference( $file ); + $translation->getReferences()->add( $file ); } return; diff --git a/src/MakeJsonCommand.php b/src/MakeJsonCommand.php index 54e375a8..6d170790 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -2,9 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Extractors\Po as PoExtractor; -use Gettext\Translation; -use Gettext\Translations; use WP_CLI; use WP_CLI_Command; use WP_CLI\Utils; @@ -239,12 +236,10 @@ static function ( $value ) { protected function make_json( $source_file, $destination, $map, $domain, $extensions ) { /** @var Translations[] $mapping */ $mapping = []; - $translations = new Translations(); + $translations = PoLoader::fromFiles( $source_file ); $result = []; $extensions = array_merge( [ 'js' ], $extensions ); - PoExtractor::fromFile( $source_file, $translations ); - $base_file_name = basename( $source_file, '.po' ); $domain = ( ! empty( $domain ) ) ? $domain : $translations->getDomain(); @@ -276,31 +271,31 @@ static function ( $reference ) use ( $extensions ) { ? (string) preg_replace( "/\.min\.{$extension}$/", ".{$extension}", $file ) : null; }, - $this->reference_map( $translation->getReferences(), $map ) + $this->reference_map( $translation->getReferenceEntries(), $map ) ); $sources = array_unique( array_filter( $sources ) ); foreach ( $sources as $source ) { if ( ! isset( $mapping[ $source ] ) ) { - $mapping[ $source ] = new Translations(); + $mapping[ $source ] = Translations::create(); // phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- Provide code that is meant to be used once the bug is fixed. // See https://core.trac.wordpress.org/ticket/45441 // $mapping[ $source ]->setDomain( $translations->getDomain() ); - $mapping[ $source ]->setHeader( 'Language', $translations->getLanguage() ); - $mapping[ $source ]->setHeader( 'PO-Revision-Date', $translations->getHeader( 'PO-Revision-Date' ) ?? '' ); + $mapping[ $source ]->getHeaders()->set( 'Language', (string) $translations->getLanguage() ); + $mapping[ $source ]->getHeaders()->set( 'PO-Revision-Date', (string) $translations->getHeaders()->get( 'PO-Revision-Date' ) ); - $plural_forms = $translations->getPluralForms(); + $plural_forms = $translations->getHeaders()->getPluralForm(); if ( $plural_forms ) { list( $count, $rule ) = $plural_forms; - $mapping[ $source ]->setPluralForms( $count, $rule ); + $mapping[ $source ]->getHeaders()->setPluralForm( $count, $rule ); } } - $mapping[ $source ][] = $translation; + $mapping[ $source ]->addOrMerge( $translation ); } } @@ -359,9 +354,9 @@ static function ( $value ) { * * Exports translations for each JS file to a separate translation file. * - * @param array $mapping A mapping of files to translation entries. - * @param string $base_file_name Base file name for JSON files. - * @param string $destination Path to the destination directory. + * @param array $mapping A mapping of files to translation entries. + * @param string $base_file_name Base file name for JSON files. + * @param string $destination Path to the destination directory. * * @return array List of created JSON files. */ diff --git a/src/MakeMoCommand.php b/src/MakeMoCommand.php index 2b45cd6a..466ba1f2 100644 --- a/src/MakeMoCommand.php +++ b/src/MakeMoCommand.php @@ -3,7 +3,6 @@ namespace WP_CLI\I18n; use DirectoryIterator; -use Gettext\Translations; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -93,12 +92,12 @@ public function __invoke( $args, $assoc_args ) { } $destination_file = "{$destination}/{$file_name}"; - $translations = Translations::fromPoFile( $file->getPathname() ); + $translations = PoLoader::fromFiles( $file->getPathname() ); // Remove JS-only strings from MO files to keep them small. $this->remove_js_only_strings( $translations ); - if ( ! $translations->toMoFile( $destination_file ) ) { + if ( ! MoGenerator::toFile( $translations, $destination_file ) ) { WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) ); continue; } diff --git a/src/MakePhpCommand.php b/src/MakePhpCommand.php index 0ef09486..ab810874 100644 --- a/src/MakePhpCommand.php +++ b/src/MakePhpCommand.php @@ -3,7 +3,6 @@ namespace WP_CLI\I18n; use DirectoryIterator; -use Gettext\Translations; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -90,7 +89,7 @@ public function __invoke( $args, $assoc_args ) { $file_basename = basename( $file->getFilename(), '.po' ); $destination_file = "{$destination}/{$file_basename}.l10n.php"; - $translations = Translations::fromPoFile( $file->getPathname() ); + $translations = PoLoader::fromFiles( $file->getPathname() ); // Remove JS-only strings from PHP files to keep them small. $this->remove_js_only_strings( $translations ); diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index eb9986ed..416c7500 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -2,11 +2,7 @@ namespace WP_CLI\I18n; -use Gettext\Extractors\Po; -use Gettext\Merge; -use Gettext\Translation; -use Gettext\Translations; -use Gettext\Utils\ParsedComment; +use Gettext\Headers; use WP_CLI; use WP_CLI_Command; use WP_CLI\Path; @@ -466,8 +462,7 @@ function ( $file ) { WP_CLI::debug( sprintf( 'Ignoring any string already existing in: %s', $file ), 'make-pot' ); - $this->exceptions[ $file ] = new Translations(); - Po::fromFile( $file, $this->exceptions[ $file ] ); + $this->exceptions[ $file ] = PoLoader::fromFiles( $file ); } } @@ -626,13 +621,14 @@ protected function get_file_headers( $type ) { * @return Translations A Translation set. */ protected function extract_strings() { - $translations = new Translations(); + $translations = Translations::create(); // Add existing strings first but don't keep headers. if ( ! empty( $this->merge ) ) { - $existing_translations = new Translations(); - Po::fromFile( $this->merge, $existing_translations ); - $translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE ); + foreach ( PoLoader::fromFiles( $this->merge ) as $existing_translation ) { + /** @var Translation $existing_translation */ + $translations->add( $existing_translation ); + } } PotGenerator::setCommentBeforeHeaders( $this->get_file_comment() ); @@ -640,10 +636,10 @@ protected function extract_strings() { $this->set_default_headers( $translations ); // POT files have no Language header. - $translations->deleteHeader( Translations::HEADER_LANGUAGE ); + $translations->getHeaders()->delete( Headers::HEADER_LANGUAGE ); // Only relevant for PO files, not POT files. - $translations->setHeader( 'PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE' ); + $translations->getHeaders()->set( 'PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE' ); if ( $this->domain ) { $translations->setDomain( $this->domain ); @@ -660,12 +656,12 @@ protected function extract_strings() { } $val = $data['value']; - $translation = new Translation( '', is_scalar( $val ) ? (string) $val : '' ); + $translation = $translations->insert( null, is_scalar( $val ) ? (string) $val : '' ); if ( $is_theme ) { - $translation->addExtractedComment( sprintf( '%s of the theme', $header ) ); + $translation->getExtractedComments()->add( sprintf( '%s of the theme', $header ) ); } else { - $translation->addExtractedComment( sprintf( '%s of the plugin', $header ) ); + $translation->getExtractedComments()->add( sprintf( '%s of the plugin', $header ) ); } if ( $this->main_file_path && $this->location ) { @@ -673,13 +669,11 @@ protected function extract_strings() { // Add line number if available if ( ! empty( $data['line'] ) ) { $line_val = $data['line']; - $translation->addReference( $file_reference, is_numeric( $line_val ) ? (int) $line_val : null ); + $translation->getReferences()->add( $file_reference, is_numeric( $line_val ) ? (int) $line_val : null ); } else { - $translation->addReference( $file_reference ); + $translation->getReferences()->add( $file_reference ); } } - - $translations[] = $translation; } try { @@ -790,18 +784,17 @@ protected function extract_strings() { foreach ( $this->exceptions as $file => $exception_translations ) { /** @var Translation $exception_translation */ foreach ( $exception_translations as $exception_translation ) { - if ( ! $translations->find( $exception_translation ) ) { + $translation = $translations->find( $exception_translation->getContext(), $exception_translation->getOriginal() ); + + if ( ! $translation ) { continue; } if ( $this->subtract_and_merge ) { - $translation = $translations[ $exception_translation->getId() ]; - if ( $translation instanceof Translation ) { - $exception_translation->mergeWith( $translation ); - } + $exception_translations->add( $exception_translation->mergeWith( $translation ) ); } - unset( $translations[ $exception_translation->getId() ] ); + $translations->remove( $translation ); } if ( $this->subtract_and_merge ) { @@ -821,21 +814,23 @@ protected function extract_strings() { * * Goes through all extracted strings to find possible mistakes. * - * @param \Gettext\Translations $translations Translations object. + * @param Translations $translations Translations object. * @return void */ protected function audit_strings( $translations ) { foreach ( $translations as $translation ) { /** @var Translation $translation */ - $references = $translation->getReferences(); + $references = $translation->getReferenceEntries(); // File headers don't have any file references. - $location = $translation->hasReferences() ? '(' . implode( ':', $references[0] ) . ')' : ''; + $location = ! empty( $references ) ? '(' . implode( ':', $references[0] ) . ')' : ''; + + $comments = $translation->getExtractedComments()->toArray(); // Check 1: Flag strings with placeholders that should have translator comments. if ( - ! $translation->hasExtractedComments() && + empty( $comments ) && preg_match( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $placeholders ) >= 1 ) { $message = sprintf( @@ -847,15 +842,13 @@ protected function audit_strings( $translations ) { } // Check 2: Flag strings with different translator comments. - if ( $translation->hasExtractedComments() ) { - $comments = $translation->getExtractedComments(); - + if ( ! empty( $comments ) ) { // Remove plugin header information from comments. $comments = array_filter( $comments, function ( $comment ) { foreach ( $this->get_file_headers( $this->project_type ) as $file_header ) { - if ( 0 === strpos( ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ), $file_header ) ) { + if ( 0 === strpos( $comment, $file_header ) ) { return null; } } @@ -864,22 +857,8 @@ function ( $comment ) { } ); - $unique_comments = array(); - - // Remove duplicate comments. - $comments = array_filter( - $comments, - function ( $comment ) use ( &$unique_comments ) { - /** @var ParsedComment|string $comment */ - if ( in_array( ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ), $unique_comments, true ) ) { - return false; - } - - $unique_comments[] = ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ); - - return true; - } - ); + $unique_comments = array_values( array_unique( $comments ) ); + $comments = $unique_comments; $comments_count = count( $comments ); @@ -924,7 +903,7 @@ function ( $comment ) use ( &$unique_comments ) { preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $single_placeholders ); $single_placeholders = $single_placeholders[0]; - preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getPlural(), $plural_placeholders ); + preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, (string) $translation->getPlural(), $plural_placeholders ); $plural_placeholders = $plural_placeholders[0]; // see https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals @@ -1033,7 +1012,7 @@ protected function get_file_comment() { /** * Sets default POT file headers for the project. * - * @param \Gettext\Translations $translations Translations object. + * @param Translations $translations Translations object. * @return void */ protected function set_default_headers( $translations ) { @@ -1067,20 +1046,20 @@ protected function set_default_headers( $translations ) { } if ( null !== $name ) { - $translations->setHeader( 'Project-Id-Version', $name . ( $version ? ' ' . $version : '' ) ); + $translations->getHeaders()->set( 'Project-Id-Version', $name . ( $version ? ' ' . $version : '' ) ); } if ( null !== $bugs_address ) { - $translations->setHeader( 'Report-Msgid-Bugs-To', $bugs_address ); + $translations->getHeaders()->set( 'Report-Msgid-Bugs-To', $bugs_address ); } - $translations->setHeader( 'Last-Translator', 'FULL NAME ' ); - $translations->setHeader( 'Language-Team', 'LANGUAGE ' ); - $translations->setHeader( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION ); + $translations->getHeaders()->set( 'Last-Translator', 'FULL NAME ' ); + $translations->getHeaders()->set( 'Language-Team', 'LANGUAGE ' ); + $translations->getHeaders()->set( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION ); foreach ( $this->headers as $key => $value ) { if ( is_scalar( $value ) ) { - $translations->setHeader( $key, (string) $value ); + $translations->getHeaders()->set( $key, (string) $value ); } } } diff --git a/src/MapCodeExtractor.php b/src/MapCodeExtractor.php index e1d352f5..a3935d66 100644 --- a/src/MapCodeExtractor.php +++ b/src/MapCodeExtractor.php @@ -2,12 +2,10 @@ namespace WP_CLI\I18n; -use Gettext\Extractors\JsCode; -use Gettext\Translations; use Peast\Syntax\Exception as PeastException; use WP_CLI; -final class MapCodeExtractor extends JsCode { +final class MapCodeExtractor { use IterableCodeExtractor; /** @@ -25,11 +23,11 @@ final class MapCodeExtractor extends JsCode { ]; /** - * {@inheritdoc} + * Extracts translations from the sources embedded in a JavaScript source map. * - * @param string $text - * @param Translations $translations - * @param array $options + * @param string $text The source map contents. + * @param Translations $translations Translations instance. + * @param array $options Extraction options. * @return void */ public static function fromString( $text, Translations $translations, array $options = [] ) { diff --git a/src/MoGenerator.php b/src/MoGenerator.php new file mode 100644 index 00000000..461af62b --- /dev/null +++ b/src/MoGenerator.php @@ -0,0 +1,152 @@ + $options + * @return string + */ + public static function toString( Translations $translations, array $options = [] ) { + $headers = ''; + + foreach ( $translations->getHeaders()->toArray() as $name => $value ) { + $headers .= sprintf( "%s: %s\n", $name, $value ); + } + + /** @var array $messages */ + $messages = [ '' => $headers ]; + + foreach ( $translations as $translation ) { + /** @var Translation $translation */ + + if ( null === $translation->getTranslation() || '' === $translation->getTranslation() || $translation->isDisabled() ) { + continue; + } + + $original_string = $translation->getOriginal(); + + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { + $original_string = $translation->getContext() . "\x04" . $original_string; + } + + $messages[ $original_string ] = $translation; + } + + ksort( $messages ); + + $num_entries = count( $messages ); + $originals_table = ''; + $translations_table = ''; + $originals_index = []; + $translations_index = []; + + $plural_form = $translations->getHeaders()->getPluralForm(); + $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : null; + + foreach ( $messages as $original_string => $translation ) { + $original_string = (string) $original_string; + + if ( is_string( $translation ) ) { + // Headers. + $translation_string = $translation; + } else { + $translation_string = (string) $translation->getTranslation(); + + if ( + null !== $translation->getPlural() && + '' !== $translation->getPlural() && + '' !== implode( '', $translation->getPluralTranslations() ) + ) { + $original_string .= "\x00" . $translation->getPlural(); + $translation_string .= "\x00" . implode( "\x00", $translation->getPluralTranslations( $plural_size ) ); + } + } + + $originals_index[] = [ + 'relativeOffset' => strlen( $originals_table ), + 'length' => strlen( $original_string ), + ]; + + $originals_table .= $original_string . "\x00"; + + $translations_index[] = [ + 'relativeOffset' => strlen( $translations_table ), + 'length' => strlen( $translation_string ), + ]; + + $translations_table .= $translation_string . "\x00"; + } + + // Offset of table with the original strings index: right after the header (which is 7 words). + $originals_index_offset = 7 * 4; + + // Size of table with the original strings index. + $originals_index_size = $num_entries * ( 4 + 4 ); + + // Offset of table with the translation strings index: right after the original strings index table. + $translations_index_offset = $originals_index_offset + $originals_index_size; + + // Size of table with the translation strings index. + $translations_index_size = $num_entries * ( 4 + 4 ); + + // Hashing table starts after the header and after the index table. + $originals_strings_offset = $translations_index_offset + $translations_index_size; + + // Translations start after the keys. + $translations_strings_offset = $originals_strings_offset + strlen( $originals_table ); + + // Magic number. + $mo = pack( 'L', 0x950412de ); + + // File format revision. + $mo .= pack( 'L', 0 ); + + // Number of strings. + $mo .= pack( 'L', $num_entries ); + + // Offset of table with original strings. + $mo .= pack( 'L', $originals_index_offset ); + + // Offset of table with translation strings. + $mo .= pack( 'L', $translations_index_offset ); + + // Size of hashing table: we don't use it. + $mo .= pack( 'L', 0 ); + + // Offset of hashing table: it would start right after the translations index table. + $mo .= pack( 'L', $translations_index_offset + $translations_index_size ); + + // Write the lengths & offsets of the original strings. + foreach ( $originals_index as $info ) { + $mo .= pack( 'L', $info['length'] ); + $mo .= pack( 'L', $originals_strings_offset + $info['relativeOffset'] ); + } + + // Write the lengths & offsets of the translated strings. + foreach ( $translations_index as $info ) { + $mo .= pack( 'L', $info['length'] ); + $mo .= pack( 'L', $translations_strings_offset + $info['relativeOffset'] ); + } + + // Write original strings. + $mo .= $originals_table; + + // Write translation strings. + $mo .= $translations_table; + + return $mo; + } +} diff --git a/src/OrderedFlags.php b/src/OrderedFlags.php new file mode 100644 index 00000000..185ce8eb --- /dev/null +++ b/src/OrderedFlags.php @@ -0,0 +1,21 @@ +has( $flag ) ) { + $this->flags[] = $flag; + } + } + + return $this; + } +} diff --git a/src/OrderedHeaders.php b/src/OrderedHeaders.php new file mode 100644 index 00000000..5a41c11c --- /dev/null +++ b/src/OrderedHeaders.php @@ -0,0 +1,36 @@ + $headers + */ + public function __construct( array $headers = [] ) { + parent::__construct(); + + $this->headers = $headers; + } + + public function set( string $name, string $value ): Headers { + $this->headers[ trim( $name ) ] = trim( $value ); + + return $this; + } + + public function mergeWith( Headers $headers ): Headers { + $merged = clone $this; + $merged->headers = $headers->toArray() + $merged->headers; + + return $merged; + } +} diff --git a/src/OrderedReferences.php b/src/OrderedReferences.php new file mode 100644 index 00000000..d8615922 --- /dev/null +++ b/src/OrderedReferences.php @@ -0,0 +1,79 @@ + + */ + protected $entries = []; + + public function add( string $filename, ?int $line = null ): References { + $this->entries[ "{$filename}:{$line}" ] = [ $filename, $line ]; + + return parent::add( $filename, $line ); + } + + /** + * Returns all references in the order they were added. + * + * @return array List of file name and line number pairs. + */ + public function getEntries(): array { + return array_values( $this->entries ); + } + + public function count(): int { + return count( $this->entries ); + } + + public function mergeWith( References $references ): References { + if ( ! $references instanceof self ) { + return parent::mergeWith( $references ); + } + + $merged = clone $this; + + foreach ( $references->getEntries() as $entry ) { + $merged->add( $entry[0], $entry[1] ); + } + + return $merged; + } + + /** + * Returns the references of a translation in the order they were added. + * + * @param References $references References instance. + * @return array List of file name and line number pairs. + */ + public static function entries( References $references ): array { + if ( $references instanceof self ) { + return $references->getEntries(); + } + + $entries = []; + + foreach ( $references->toArray() as $filename => $lines ) { + if ( empty( $lines ) ) { + $entries[] = [ (string) $filename, null ]; + continue; + } + + foreach ( $lines as $line ) { + $entries[] = [ (string) $filename, $line ]; + } + } + + return $entries; + } +} diff --git a/src/ParsedComment.php b/src/ParsedComment.php new file mode 100644 index 00000000..2df44a31 --- /dev/null +++ b/src/ParsedComment.php @@ -0,0 +1,120 @@ +comment = $comment; + $this->first_line = $first_line; + $this->last_line = $last_line; + } + + /** + * Creates a new instance from a raw comment. + * + * @param string $value The comment as found in the source code. + * @param int $line The line where the comment starts. + * @return static + */ + public static function create( $value, $line ) { + $last_line = $line + substr_count( $value, "\n" ); + + $lines = array_map( + static function ( $line ) { + if ( '' === trim( $line ) ) { + return null; + } + + $line = ltrim( $line, "#*/ \t" ); + $line = rtrim( $line, "#*/ \t" ); + + return trim( $line ); + }, + explode( "\n", $value ) + ); + + // Remove empty lines. + $lines = array_filter( $lines ); + + return new static( implode( ' ', $lines ), $line, $last_line ); + } + + /** + * @return int + */ + public function getFirstLine() { + return $this->first_line; + } + + /** + * @return int + */ + public function getLastLine() { + return $this->last_line; + } + + /** + * @return string + */ + public function getComment() { + return $this->comment; + } + + /** + * Whether this comment is on the same line as the given function call or on the one before. + * + * @param ParsedFunction $parsed_function The function to check. + * @return bool + */ + public function isRelatedWith( ParsedFunction $parsed_function ) { + return $this->getLastLine() === $parsed_function->getLine() || $this->getLastLine() === $parsed_function->getLine() - 1; + } + + /** + * Whether the comment starts with one of the given prefixes. + * + * @param array $prefixes Prefixes to check. Every non-empty comment matches if there are none. + * @return bool + */ + public function checkPrefixes( array $prefixes ) { + if ( '' === $this->comment ) { + return false; + } + + if ( empty( $prefixes ) ) { + return true; + } + + foreach ( $prefixes as $prefix ) { + if ( 0 === strpos( $this->comment, $prefix ) ) { + return true; + } + } + + return false; + } +} diff --git a/src/ParsedFunction.php b/src/ParsedFunction.php new file mode 100644 index 00000000..22140f6a --- /dev/null +++ b/src/ParsedFunction.php @@ -0,0 +1,141 @@ + + */ + protected $arguments = []; + + /** + * @var int + */ + protected $argument_index = -1; + + /** + * Whether the current argument can no longer be a plain string. + * + * @var bool + */ + protected $argument_stopped = false; + + /** + * @var ParsedComment[]|null + */ + protected $comments; + + /** + * @param string $name The function name. + * @param int $line The line where the function call starts. + */ + public function __construct( $name, $line ) { + $this->name = $name; + $this->line = $line; + } + + /** + * Stops extracting strings for the current argument. + * + * @return void + */ + public function stopArgument() { + if ( -1 === $this->argument_index ) { + $this->argument_index = 0; + } + + $this->argument_stopped = true; + } + + /** + * Moves on to the next argument. + * + * @return void + */ + public function nextArgument() { + if ( -1 === $this->argument_index ) { + $this->argument_index = 1; + } else { + ++$this->argument_index; + } + + $this->argument_stopped = false; + } + + /** + * Adds a string to the current argument. + * + * @param string|null $chunk Part of the argument. + * @return void + */ + public function addArgumentChunk( $chunk ) { + if ( $this->argument_stopped ) { + return; + } + + if ( -1 === $this->argument_index ) { + $this->argument_index = 0; + } + + if ( isset( $this->arguments[ $this->argument_index ] ) ) { + $this->arguments[ $this->argument_index ] .= $chunk; + } else { + $this->arguments[ $this->argument_index ] = $chunk; + } + } + + /** + * Adds a comment related to this function call. + * + * @param ParsedComment $comment The comment. + * @return void + */ + public function addComment( ParsedComment $comment ) { + if ( null === $this->comments ) { + $this->comments = []; + } + + $this->comments[] = $comment; + } + + /** + * @return int + */ + public function getLine() { + return $this->line; + } + + /** + * Returns the function call once all its arguments have been read. + * + * @return array{0: string, 1: int, 2: array, 3: ParsedComment[]|null} Name, line, arguments and comments. + */ + public function close() { + $arguments = []; + + for ( $i = 0; $i <= $this->argument_index; ++$i ) { + $arguments[ $i ] = isset( $this->arguments[ $i ] ) ? $this->arguments[ $i ] : null; + } + + return [ + $this->name, + $this->line, + $arguments, + $this->comments, + ]; + } +} diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 2c8a3204..66e7da19 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -2,7 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Generators\PhpArray; use Gettext\Translation; use Gettext\Translations; @@ -11,7 +10,7 @@ * * Returns output in the form WordPress uses. */ -class PhpArrayGenerator extends PhpArray { +class PhpArrayGenerator extends Generator { /** * @var array */ @@ -64,7 +63,7 @@ protected static function toArray( Translations $translations, $include_headers, $result = [ 'domain' => $translations->getDomain(), - 'plural-forms' => $translations->getHeader( 'Plural-Forms' ), + 'plural-forms' => $translations->getHeaders()->get( 'Plural-Forms' ), ]; $language = $translations->getLanguage(); @@ -79,7 +78,7 @@ protected static function toArray( Translations $translations, $include_headers, 'X-Generator' => 'x-generator', ]; - foreach ( $translations->getHeaders() as $name => $value ) { + foreach ( $translations->getHeaders()->toArray() as $name => $value ) { if ( isset( $headers_allowlist[ $name ] ) ) { $result[ $headers_allowlist[ $name ] ] = $value; } @@ -89,7 +88,7 @@ protected static function toArray( Translations $translations, $include_headers, * @var Translation $translation */ foreach ( $translations as $translation ) { - if ( $translation->isDisabled() || ! $translation->hasTranslation() ) { + if ( $translation->isDisabled() || null === $translation->getTranslation() || '' === $translation->getTranslation() ) { continue; } @@ -98,7 +97,7 @@ protected static function toArray( Translations $translations, $include_headers, $key = $context ? $context . "\4" . $original : $original; - if ( $translation->hasPluralTranslations() ) { + if ( ! empty( $translation->getPluralTranslations() ) ) { $msg_translations = $translation->getPluralTranslations(); array_unshift( $msg_translations, $translation->getTranslation() ); $messages[ $key ] = implode( "\0", $msg_translations ); diff --git a/src/PhpCodeExtractor.php b/src/PhpCodeExtractor.php index af799748..4434201b 100644 --- a/src/PhpCodeExtractor.php +++ b/src/PhpCodeExtractor.php @@ -3,11 +3,9 @@ namespace WP_CLI\I18n; use Exception; -use Gettext\Extractors\PhpCode; -use Gettext\Translations; use WP_CLI; -final class PhpCodeExtractor extends PhpCode { +final class PhpCodeExtractor { use IterableCodeExtractor; /** @@ -47,16 +45,11 @@ final class PhpCodeExtractor extends PhpCode { ]; /** - * @var string - */ - protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner'; - - /** - * {@inheritdoc} + * Extracts translations from PHP code. * - * @param string $text - * @param Translations $translations - * @param array $options + * @param string $text The code to extract strings from. + * @param Translations $translations Translations instance. + * @param array $options Extraction options. * @return void */ public static function fromString( $text, Translations $translations, array $options = [] ) { @@ -67,7 +60,7 @@ public static function fromString( $text, Translations $translations, array $opt WP_CLI::debug( "Parsing file {$file}", 'make-pot' ); try { - self::fromStringMultiple( $text, [ $translations ], $options ); + self::scan( $text, $translations, $options + self::$options ); } catch ( Exception $exception ) { WP_CLI::debug( sprintf( @@ -79,4 +72,24 @@ public static function fromString( $text, Translations $translations, array $opt ); } } + + /** + * Scans PHP code for the configured functions and adds the strings found. + * + * @param string $text The code to extract strings from. + * @param Translations $translations Translations instance. + * @param array $options Extraction options. + * @return void + */ + public static function scan( $text, Translations $translations, array $options ) { + $scanner = new PhpFunctionsScanner( $text ); + + $extract_comments = $options['extractComments'] ?? false; + if ( is_string( $extract_comments ) || is_array( $extract_comments ) ) { + /** @var string|array $extract_comments */ + $scanner->enableCommentsExtraction( $extract_comments ); + } + + $scanner->saveGettextFunctions( $translations, $options ); + } } diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index e1bf9e6d..50fdb7a2 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -2,24 +2,296 @@ namespace WP_CLI\I18n; -use Gettext\Utils\PhpFunctionsScanner as GettextPhpFunctionsScanner; +/** + * Finds function calls and their string arguments in PHP code. + * + * Based on the PHP functions scanner of gettext/gettext 4.x. + */ +class PhpFunctionsScanner { -class PhpFunctionsScanner extends GettextPhpFunctionsScanner { + /** + * PHP tokens of the code to be parsed. + * + * @var array + */ + protected $tokens; /** - * {@inheritdoc} + * If not false, comments will be extracted. * - * @param \Gettext\Translations $translations Translations instance. - * @param array $options Options. + * @var string|false|array + */ + protected $extract_comments = false; + + /** + * @param string $code The PHP code to scan. + */ + public function __construct( $code ) { + $this->tokens = array_values( + array_filter( + token_get_all( $code ), + static function ( $token ) { + return ! is_array( $token ) || T_WHITESPACE !== $token[0]; + } + ) + ); + } + + /** + * Enable extracting comments that start with a tag (if $tag is empty all the comments will be extracted). + * + * @param string|array $tag Tag to extract. * @return void */ - public function saveGettextFunctions( $translations, array $options ) { - // Ignore multiple translations for now. - // @todo Add proper support for multiple translations. - if ( is_array( $translations ) ) { - $translations = $translations[0]; + public function enableCommentsExtraction( $tag = '' ) { + $this->extract_comments = $tag; + } + + /** + * Disable comments extraction. + * + * @return void + */ + public function disableCommentsExtraction() { + $this->extract_comments = false; + } + + /** + * Scans the code and returns the function calls and their arguments. + * + * @param array $constants Constants used in the code to replace. + * @return array, 3: ParsedComment[]|null}> List of name, line, arguments and comments. + */ + public function getFunctions( array $constants = [] ) { + $count = count( $this->tokens ); + + /** @var ParsedFunction[] $buffer_functions */ + $buffer_functions = []; + + /** @var ParsedComment[] $buffer_comments */ + $buffer_comments = []; + + $functions = []; + + for ( $k = 0; $k < $count; ++$k ) { + $value = $this->tokens[ $k ]; + + if ( is_string( $value ) ) { + if ( isset( $buffer_functions[0] ) ) { + switch ( $value ) { + case ',': + $buffer_functions[0]->nextArgument(); + break; + + case ')': + $functions[] = $buffer_functions[0]->close(); + array_shift( $buffer_functions ); + break; + + case '.': + break; + + default: + $buffer_functions[0]->stopArgument(); + break; + } + } + + continue; + } + + // phpcs:ignore PHPCompatibility.Constants.NewConstants.t_name_fully_qualifiedFound + if ( defined( 'T_NAME_FULLY_QUALIFIED' ) && T_NAME_FULLY_QUALIFIED === $value[0] ) { + $value[0] = T_STRING; + $value[1] = ltrim( $value[1], '\\' ); + } + + switch ( $value[0] ) { + case T_CONSTANT_ENCAPSED_STRING: + // Add an argument to the current function. + if ( isset( $buffer_functions[0] ) ) { + $buffer_functions[0]->addArgumentChunk( self::convertString( $value[1] ) ); + } + break; + + case T_STRING: + if ( isset( $buffer_functions[0] ) ) { + if ( isset( $constants[ $value[1] ] ) ) { + $buffer_functions[0]->addArgumentChunk( $constants[ $value[1] ] ); + break; + } + + if ( 'null' === strtolower( $value[1] ) ) { + $buffer_functions[0]->addArgumentChunk( null ); + break; + } + + $buffer_functions[0]->stopArgument(); + } + + // New function found. + for ( $j = $k + 1; $j < $count; ++$j ) { + $next_token = $this->tokens[ $j ]; + + if ( is_array( $next_token ) && T_COMMENT === $next_token[0] ) { + continue; + } + + if ( '(' === $next_token ) { + $new_function = new ParsedFunction( $value[1], $value[2] ); + + // Add comment that was on the line before. + if ( isset( $buffer_comments[0] ) && $buffer_comments[0]->isRelatedWith( $new_function ) ) { + $new_function->addComment( $buffer_comments[0] ); + } + + array_unshift( $buffer_functions, $new_function ); + $k = $j; + } + + break; + } + break; + + case T_COMMENT: + $comment = $this->parsePhpComment( $value[1], $value[2] ); + + if ( $comment ) { + array_unshift( $buffer_comments, $comment ); + + // The comment is inside the function call. + if ( isset( $buffer_functions[0] ) ) { + $buffer_functions[0]->addComment( $comment ); + } + } + break; + + default: + if ( isset( $buffer_functions[0] ) ) { + $buffer_functions[0]->stopArgument(); + } + break; + } + } + + return $functions; + } + + /** + * Extracts the actual text from a PHP comment. + * + * @param string $value The PHP comment. + * @param int $line Line number. + * @return ParsedComment|null Comment or null if comment extraction is disabled or if there is a prefix mismatch. + */ + protected function parsePhpComment( $value, $line ) { + if ( false === $this->extract_comments ) { + return null; + } + + $comment = ParsedComment::create( $value, $line ); + $prefixes = array_filter( (array) $this->extract_comments ); + + return $comment->checkPrefixes( $prefixes ) ? $comment : null; + } + + /** + * Decodes a T_CONSTANT_ENCAPSED_STRING string. + * + * @param string $value String literal including its quotes. + * @return string + */ + public static function convertString( $value ) { + if ( false === strpos( $value, '\\' ) ) { + return substr( $value, 1, -1 ); } + if ( "'" === $value[0] ) { + return strtr( + substr( $value, 1, -1 ), + [ + '\\\\' => '\\', + '\\\'' => '\'', + ] + ); + } + + $value = substr( $value, 1, -1 ); + + return (string) preg_replace_callback( + '/\\\(n|r|t|v|e|f|\$|"|\\\|x[0-9A-Fa-f]{1,2}|u{[0-9a-f]{1,6}}|[0-7]{1,3})/', + static function ( $matches ) { + switch ( $matches[1][0] ) { + case 'n': + return "\n"; + case 'r': + return "\r"; + case 't': + return "\t"; + case 'v': + return "\v"; + case 'e': + return "\e"; + case 'f': + return "\f"; + case '$': + return '$'; + case '"': + return '"'; + case '\\': + return '\\'; + case 'x': + return chr( (int) hexdec( substr( $matches[1], 1 ) ) ); + case 'u': + return (string) self::unicodeChar( (int) hexdec( trim( substr( $matches[1], 1 ), '{}' ) ) ); + default: + return chr( (int) octdec( $matches[1] ) ); + } + }, + $value + ); + } + + /** + * Returns the UTF-8 encoded character for a Unicode code point. + * + * @param int $dec Code point. + * @return string|null + */ + protected static function unicodeChar( $dec ) { + if ( $dec < 0x80 ) { + return chr( $dec ); + } + + if ( $dec < 0x0800 ) { + return chr( 0xC0 + ( $dec >> 6 ) ) + . chr( 0x80 + ( $dec & 0x3f ) ); + } + + if ( $dec < 0x010000 ) { + return chr( 0xE0 + ( $dec >> 12 ) ) + . chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) ) + . chr( 0x80 + ( $dec & 0x3f ) ); + } + + if ( $dec < 0x200000 ) { + return chr( 0xF0 + ( $dec >> 18 ) ) + . chr( 0x80 + ( ( $dec >> 12 ) & 0x3f ) ) + . chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) ) + . chr( 0x80 + ( $dec & 0x3f ) ); + } + + return null; + } + + /** + * Searches for specific functions and creates translations. + * + * @param Translations $translations Translations instance. + * @param array $options Options. + * @return void + */ + public function saveGettextFunctions( Translations $translations, array $options ) { $functions = isset( $options['functions'] ) ? $options['functions'] : []; if ( ! is_array( $functions ) ) { $functions = []; @@ -100,24 +372,18 @@ public function saveGettextFunctions( $translations, array $options ) { $translation = $translations->insert( $context, $original_str, $plural ); if ( $add_reference ) { - $translation = $translation->addReference( $file, $line ); + $translation->getReferences()->add( $file, $line ); } if ( 1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original_str ) || 1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original_str ) ) { - $translation->addFlag( 'php-format' ); + $translation->getFlags()->add( 'php-format' ); } - if ( isset( $function[3] ) && is_array( $function[3] ) ) { - foreach ( $function[3] as $extracted_comment ) { - if ( is_scalar( $extracted_comment ) ) { - $translation = $translation->addExtractedComment( (string) $extracted_comment ); - } elseif ( is_object( $extracted_comment ) && method_exists( $extracted_comment, 'getComment' ) ) { - $translation = $translation->addExtractedComment( $extracted_comment->getComment() ); - } - } + foreach ( $function[3] ?? [] as $extracted_comment ) { + $translation->getExtractedComments()->add( $extracted_comment->getComment() ); } } } diff --git a/src/PoGenerator.php b/src/PoGenerator.php new file mode 100644 index 00000000..af2eadc0 --- /dev/null +++ b/src/PoGenerator.php @@ -0,0 +1,164 @@ + $options + * @return string + */ + public static function toString( Translations $translations, array $options = [] ) { + $plural_form = $translations->getHeaders()->getPluralForm(); + $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : null; + + $lines = static::getHeaderLines( $translations ); + + foreach ( $translations as $translation ) { + /** @var Translation $translation */ + + $prefix = static::addMetaLines( $lines, $translation ); + + if ( null !== $translation->getPlural() && '' !== $translation->getPlural() ) { + static::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); + static::addLines( $lines, $prefix . 'msgstr[0]', (string) $translation->getTranslation() ); + + foreach ( $translation->getPluralTranslations( $plural_size ) as $k => $v ) { + static::addLines( $lines, $prefix . 'msgstr[' . ( $k + 1 ) . ']', $v ); + } + } else { + static::addLines( $lines, $prefix . 'msgstr', (string) $translation->getTranslation() ); + } + + $lines[] = ''; + } + + return implode( "\n", $lines ); + } + + /** + * Returns the lines for the header entry. + * + * @param Translations $translations Translations instance. + * @return array + */ + protected static function getHeaderLines( Translations $translations ) { + $lines = [ 'msgid ""', 'msgstr ""' ]; + + foreach ( $translations->getHeaders()->toArray() as $name => $value ) { + $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); + } + + $lines[] = ''; + + return $lines; + } + + /** + * Adds the comments, references, flags, context and original string of an entry. + * + * @param array &$lines Array lines should be added to. + * @param Translation $translation Translation entry. + * @return string Prefix to use for the remaining lines of the entry. + */ + protected static function addMetaLines( array &$lines, Translation $translation ) { + foreach ( $translation->getComments() as $comment ) { + $lines[] = '# ' . $comment; + } + + foreach ( $translation->getExtractedComments() as $comment ) { + $lines[] = '#. ' . $comment; + } + + foreach ( OrderedReferences::entries( $translation->getReferences() ) as $reference ) { + $lines[] = '#: ' . $reference[0] . ( null !== $reference[1] ? ':' . $reference[1] : '' ); + } + + if ( count( $translation->getFlags() ) > 0 ) { + $lines[] = '#, ' . implode( ',', $translation->getFlags()->toArray() ); + } + + $prefix = $translation->isDisabled() ? '#~ ' : ''; + + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { + $lines[] = $prefix . 'msgctxt ' . static::convertString( $translation->getContext() ); + } + + static::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); + + return $prefix; + } + + /** + * Escapes and adds double quotes to a string. + * + * @param string $text Multiline string. + * + * @return string[] + */ + protected static function multilineQuote( $text ) { + $lines = explode( "\n", $text ); + $last = count( $lines ) - 1; + + foreach ( $lines as $k => $line ) { + if ( $k === $last ) { + $lines[ $k ] = static::convertString( $line ); + } else { + $lines[ $k ] = static::convertString( $line . "\n" ); + } + } + + return $lines; + } + + /** + * Add one or more lines depending whether the string is multiline or not. + * + * @param array &$lines Array lines should be added to. + * @param string $name Name of the line, e.g. msgstr or msgid_plural. + * @param string $value The line to add. + * @return void + */ + protected static function addLines( array &$lines, $name, $value ) { + $newlines = static::multilineQuote( $value ); + + if ( count( $newlines ) === 1 ) { + $lines[] = $name . ' ' . $newlines[0]; + } else { + $lines[] = $name . ' ""'; + + foreach ( $newlines as $line ) { + $lines[] = $line; + } + } + } + + /** + * Converts a string to its PO representation. + * + * @param string $value String to convert. + * @return string + */ + public static function convertString( $value ) { + return '"' . strtr( + $value, + [ + "\x00" => '', + '\\' => '\\\\', + "\t" => '\t', + "\r" => '\r', + "\n" => '\n', + '"' => '\\"', + ] + ) . '"'; + } +} diff --git a/src/PoLoader.php b/src/PoLoader.php new file mode 100644 index 00000000..77b6deb2 --- /dev/null +++ b/src/PoLoader.php @@ -0,0 +1,265 @@ + $files A file path or a list of file paths. + * @return Translations + */ + public static function fromFiles( $files ): Translations { + $loader = new self(); + $translations = Translations::create(); + + foreach ( (array) $files as $file ) { + $loader->loadFile( $file, $translations ); + } + + return $translations; + } + + public function loadString( string $text, ?GettextTranslations $translations = null ): GettextTranslations { + $translations = parent::loadString( $text, $translations ); + + $lines = explode( "\n", $text ); + $count = count( $lines ); + $translation = $this->createTranslation( null, '' ); + + for ( $i = 0; $i < $count; ++$i ) { + $line = trim( $lines[ $i ] ); + $line = self::fixMultiLines( $line, $lines, $i ); + + // Treat empty comments as empty lines. + if ( '#' === $line ) { + $line = ''; + } + + if ( '' === $line ) { + self::addTranslation( $translation, $translations ); + $translation = $this->createTranslation( null, '' ); + continue; + } + + $split_line = preg_split( '/\s+/', $line, 2 ) ?: []; + $key = $split_line[0] ?? ''; + $data = $split_line[1] ?? ''; + + if ( '#~' === $key ) { + $translation->disable(); + + $split_line = preg_split( '/\s+/', $data, 2 ) ?: []; + $key = $split_line[0] ?? ''; + $data = $split_line[1] ?? ''; + } + + switch ( $key ) { + case '#': + $translation->getComments()->add( $data ); + break; + + case '#.': + $translation->getExtractedComments()->add( $data ); + break; + + case '#,': + foreach ( array_map( 'trim', explode( ',', trim( $data ) ) ) as $flag ) { + $translation->getFlags()->add( $flag ); + } + break; + + case '#:': + foreach ( preg_split( '/\s+/', trim( $data ) ) ?: [] as $reference ) { + if ( preg_match( '/^(.+)(:(\d*))?$/U', $reference, $matches ) ) { + $translation->getReferences()->add( + $matches[1], + isset( $matches[3] ) && '' !== $matches[3] ? (int) $matches[3] : null + ); + } + } + break; + + case 'msgctxt': + $translation = $translation->withContext( self::decode( $data ) ); + break; + + case 'msgid': + $translation = $translation->withOriginal( self::decode( $data ) ); + break; + + case 'msgid_plural': + $translation->setPlural( self::decode( $data ) ); + break; + + case 'msgstr': + case 'msgstr[0]': + $translation->translate( self::decode( $data ) ); + break; + + case 'msgstr[1]': + $translation->translatePlural( self::decode( $data ) ); + break; + + default: + if ( 0 === strpos( $key, 'msgstr[' ) ) { + $plural_translations = $translation->getPluralTranslations(); + $plural_translations[] = self::decode( $data ); + $translation->translatePlural( ...$plural_translations ); + } + break; + } + } + + self::addTranslation( $translation, $translations ); + + return $translations; + } + + protected function createTranslations(): GettextTranslations { + return Translations::create(); + } + + /** + * {@inheritdoc} + * + * @return GettextTranslation + */ + protected function createTranslation( ?string $context, string $original, ?string $plural = null ): ?GettextTranslation { + $translation = Translation::create( $context, $original ); + + if ( null !== $plural ) { + $translation->setPlural( $plural ); + } + + return $translation; + } + + /** + * Adds a parsed entry to the translations. + * + * The entry with an empty original string and no context holds the headers. + * + * @param GettextTranslation $translation The parsed entry. + * @param GettextTranslations $translations Translations instance to add the entry to. + * @return void + */ + private static function addTranslation( GettextTranslation $translation, GettextTranslations $translations ) { + if ( '' !== $translation->getOriginal() ) { + $translations->addOrMerge( $translation ); + return; + } + + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { + return; + } + + self::extractHeaders( (string) $translation->getTranslation(), $translations ); + } + + /** + * Adds the headers found in the header entry to the translations. + * + * @param string $headers Translation of the header entry. + * @param GettextTranslations $translations Translations instance. + * @return void + */ + private static function extractHeaders( $headers, GettextTranslations $translations ) { + $current_header = null; + + foreach ( explode( "\n", $headers ) as $line ) { + $line = self::decode( $line ); + + if ( '' === $line ) { + continue; + } + + // A header definition, as opposed to the continuation of the previous header. + if ( preg_match( '/^[\w-]+:/', $line ) ) { + list( $current_header, $value ) = array_map( 'trim', explode( ':', $line, 2 ) ); + $translations->getHeaders()->set( $current_header, $value ); + continue; + } + + if ( null !== $current_header ) { + $translations->getHeaders()->set( + $current_header, + $translations->getHeaders()->get( $current_header ) . $line + ); + } + } + } + + /** + * Gets one string from multiline strings. + * + * @param string $line Current line. + * @param array $lines All lines. + * @param int $i Index of the current line, moved forward to the last line that was consumed. + * @return string + */ + private static function fixMultiLines( $line, array $lines, &$i ) { + for ( $j = $i, $t = count( $lines ); $j < $t; ++$j ) { + if ( '"' === substr( $line, -1, 1 ) && isset( $lines[ $j + 1 ] ) ) { + $next_line = trim( $lines[ $j + 1 ] ); + + if ( '"' === substr( $next_line, 0, 1 ) ) { + $line = substr( $line, 0, -1 ) . substr( $next_line, 1 ); + continue; + } + + if ( '#~ "' === substr( $next_line, 0, 4 ) ) { + $line = substr( $line, 0, -1 ) . substr( $next_line, 4 ); + continue; + } + } + + $i = $j; + break; + } + + return $line; + } + + /** + * Converts a string from its PO representation. + * + * @param string $value String as found in a PO file. + * @return string + */ + public static function decode( $value ) { + if ( ! $value ) { + return ''; + } + + if ( '"' === $value[0] ) { + $value = substr( $value, 1, -1 ); + } + + return strtr( + $value, + [ + '\\\\' => '\\', + '\\a' => "\x07", + '\\b' => "\x08", + '\\t' => "\t", + '\\n' => "\n", + '\\v' => "\x0b", + '\\f' => "\x0c", + '\\r' => "\r", + '\\"' => '"', + ] + ); + } +} diff --git a/src/PotGenerator.php b/src/PotGenerator.php index e2a1dfc5..8ad297cd 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -2,15 +2,14 @@ namespace WP_CLI\I18n; -use Gettext\Generators\Po as PoGenerator; +use Gettext\Translation; use Gettext\Translations; -use Gettext\Utils\ParsedComment; /** * POT file generator. * - * The only difference to the existing PO file generator is that this - * adds some comments at the very beginning of the file. + * The only difference to the PO file generator is that this adds some + * comments at the very beginning of the file and leaves plural forms untranslated. */ class PotGenerator extends PoGenerator { /** @@ -39,135 +38,34 @@ public static function setCommentBeforeHeaders( $comment ) { /** * {@inheritdoc} * - * @param \Gettext\Translations $translations - * @param array $options + * @param Translations $translations + * @param array $options * @return string */ public static function toString( Translations $translations, array $options = [] ) { - /** @var array $lines */ - $lines = static::$comments_before_headers; - $lines[] = 'msgid ""'; - $lines[] = 'msgstr ""'; + $plural_form = $translations->getHeaders()->getPluralForm(); + $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : 1; - $plural_form = $translations->getPluralForms(); - $plural_size = 1; - if ( is_array( $plural_form ) && isset( $plural_form[0] ) && is_numeric( $plural_form[0] ) ) { - $plural_size = (int) $plural_form[0] - 1; - } - - foreach ( $translations->getHeaders() as $name => $value ) { - $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); - } - - $lines[] = ''; + $lines = array_merge( static::$comments_before_headers, static::getHeaderLines( $translations ) ); foreach ( $translations as $translation ) { - /** @var \Gettext\Translation $translation */ - if ( $translation->hasComments() ) { - foreach ( $translation->getComments() as $comment ) { - if ( is_scalar( $comment ) ) { - $lines[] = '# ' . $comment; - } - } - } - - if ( $translation->hasExtractedComments() ) { - $unique_comments = array(); - - /** @var ParsedComment|string $comment */ - foreach ( $translation->getExtractedComments() as $comment ) { - $comment = ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ); - if ( is_scalar( $comment ) ) { - $comment_str = (string) $comment; - if ( ! in_array( $comment_str, $unique_comments, true ) ) { - $lines[] = '#. ' . $comment_str; - $unique_comments[] = $comment_str; - } - } - } - } + /** @var Translation $translation */ - foreach ( $translation->getReferences() as $reference ) { - if ( is_array( $reference ) && isset( $reference[0] ) ) { - $ref_str = is_scalar( $reference[0] ) ? (string) $reference[0] : ''; - $line_str = ''; - if ( isset( $reference[1] ) && is_scalar( $reference[1] ) ) { - $line_str = ':' . $reference[1]; - } - $lines[] = '#: ' . $ref_str . $line_str; - } - } - - if ( $translation->hasFlags() ) { - $lines[] = '#, ' . implode( ',', $translation->getFlags() ); - } - - $prefix = $translation->isDisabled() ? '#~ ' : ''; + $prefix = static::addMetaLines( $lines, $translation ); - if ( $translation->hasContext() ) { - $lines[] = $prefix . 'msgctxt ' . self::convertString( $translation->getContext() ); - } - - self::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); - - if ( $translation->hasPlural() ) { - self::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); + if ( null !== $translation->getPlural() && '' !== $translation->getPlural() ) { + static::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); for ( $i = 0; $i <= $plural_size; $i++ ) { - self::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' ); + static::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' ); } } else { - self::addLines( $lines, $prefix . 'msgstr', $translation->getTranslation() ); + static::addLines( $lines, $prefix . 'msgstr', (string) $translation->getTranslation() ); } $lines[] = ''; } - $string_lines = array_filter( $lines, 'is_string' ); - return implode( "\n", $string_lines ); - } - - /** - * Escapes and adds double quotes to a string. - * - * @param string $text Multiline string. - * - * @return string[] - */ - protected static function wpMultilineQuote( $text ) { - $lines = explode( "\n", $text ); - $last = count( $lines ) - 1; - - foreach ( $lines as $k => $line ) { - if ( $k === $last ) { - $lines[ $k ] = self::convertString( $line ); - } else { - $lines[ $k ] = self::convertString( $line . "\n" ); - } - } - - return $lines; - } - - /** - * Add one or more lines depending whether the string is multiline or not. - * - * @param array &$lines Array lines should be added to. - * @param string $name Name of the line, e.g. msgstr or msgid_plural. - * @param string $value The line to add. - * @return void - */ - protected static function addLines( array &$lines, $name, $value ) { - $newlines = self::wpMultilineQuote( $value ); - - if ( count( $newlines ) === 1 ) { - $lines[] = $name . ' ' . $newlines[0]; - } else { - $lines[] = $name . ' ""'; - - foreach ( $newlines as $line ) { - $lines[] = $line; - } - } + return implode( "\n", $lines ); } } diff --git a/src/Translation.php b/src/Translation.php new file mode 100644 index 00000000..aea556c9 --- /dev/null +++ b/src/Translation.php @@ -0,0 +1,82 @@ +references = new OrderedReferences(); + $this->flags = new OrderedFlags(); + } + + public function hasContext(): bool { + return null !== $this->context && '' !== $this->context; + } + + public function hasPlural(): bool { + return null !== $this->plural && '' !== $this->plural; + } + + public function hasTranslation(): bool { + return null !== $this->translation && '' !== $this->translation; + } + + /** + * @param bool $check_content Whether to check that at least one plural translation is not empty. + */ + public function hasPluralTranslations( bool $check_content = false ): bool { + if ( $check_content ) { + return '' !== implode( '', $this->getPluralTranslations() ); + } + + return ! empty( $this->getPluralTranslations() ); + } + + /** + * Returns the references in the order they were added. + * + * @return array List of file name and line number pairs. + */ + public function getReferenceEntries(): array { + return OrderedReferences::entries( $this->references ); + } + + /** + * {@inheritdoc} + * + * Unlike the parent method, this keeps translations and plurals that are "0", + * and does not change whether the translation is disabled. + */ + public function mergeWith( GettextTranslation $translation, int $strategy = 0 ): GettextTranslation { + /** @var Translation $merged */ + $merged = parent::mergeWith( $translation, $strategy ); + $override = (bool) ( $strategy & Merge::TRANSLATIONS_OVERRIDE ); + + $their_translation = $translation->getTranslation(); + $their_plural = $translation->getPlural(); + + $merged->translation = $this->translation; + if ( ! $this->hasTranslation() || ( $override && null !== $their_translation && '' !== $their_translation ) ) { + $merged->translation = $their_translation; + } + + $merged->plural = $this->plural; + if ( ! $this->hasPlural() || ( $override && null !== $their_plural && '' !== $their_plural ) ) { + $merged->plural = $their_plural; + } + + $merged->disabled = $this->disabled; + + return $merged; + } +} diff --git a/src/Translations.php b/src/Translations.php new file mode 100644 index 00000000..350b2b9e --- /dev/null +++ b/src/Translations.php @@ -0,0 +1,56 @@ +headers = new OrderedHeaders( + [ + 'Project-Id-Version' => '', + 'Report-Msgid-Bugs-To' => '', + 'Last-Translator' => '', + 'Language-Team' => '', + 'MIME-Version' => '1.0', + 'Content-Type' => 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding' => '8bit', + 'POT-Creation-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date + 'PO-Revision-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date + 'Language' => '', + ] + ); + } + + /** + * Returns the translation with the given context and original string, creating it if needed. + * + * @param string|null $context Context, if any. + * @param string $original Original string. + * @param string|null $plural Plural form of the original string, if any. + * @return Translation The existing or newly added translation. + */ + public function insert( ?string $context, string $original, ?string $plural = null ): Translation { + $translation = $this->find( $context, $original ); + + if ( ! $translation instanceof Translation ) { + /** @var Translation $translation */ + $translation = Translation::create( $context, $original ); + $this->add( $translation ); + } + + if ( null !== $plural && '' !== $plural && ! $translation->hasPlural() ) { + $translation->setPlural( $plural ); + } + + return $translation; + } +} diff --git a/src/UpdatePoCommand.php b/src/UpdatePoCommand.php index c9e0a223..8a178960 100644 --- a/src/UpdatePoCommand.php +++ b/src/UpdatePoCommand.php @@ -3,8 +3,8 @@ namespace WP_CLI\I18n; use DirectoryIterator; +use Gettext\Headers; use Gettext\Merge; -use Gettext\Translations; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -82,18 +82,10 @@ public function __invoke( $args, $assoc_args ) { $files = new IteratorIterator( new DirectoryIterator( $destination ) ); } - $pot_translations = Translations::fromPoFile( $source ); - - // Build merge flags based on options - $merge_flags = Merge::ADD | Merge::EXTRACTED_COMMENTS_THEIRS | Merge::REFERENCES_THEIRS | Merge::DOMAIN_OVERRIDE; + $pot_translations = PoLoader::fromFiles( $source ); /** @var array $assoc_args */ - $purge = Utils\get_flag_value( $assoc_args, 'purge', true ); - - if ( $purge ) { - // By default, remove obsolete entries and replace translator comments - $merge_flags |= Merge::REMOVE | Merge::COMMENTS_THEIRS; - } + $purge = (bool) Utils\get_flag_value( $assoc_args, 'purge', true ); $updated_count = 0; $unchanged_count = 0; @@ -113,13 +105,8 @@ public function __invoke( $args, $assoc_args ) { if ( ! $purge ) { $file_comments = $this->extract_file_comments( $file->getPathname() ); } - $po_translations = Translations::fromPoFile( $file->getPathname() ); - $original_translations = clone $po_translations; - - $po_translations->mergeWith( - $pot_translations, - $merge_flags - ); + $original_translations = PoLoader::fromFiles( $file->getPathname() ); + $po_translations = $this->merge_translations( $original_translations, $pot_translations, $purge ); // Check if the translations actually changed by comparing the objects. $has_changes = $this->translations_differ( $original_translations, $po_translations ); @@ -131,12 +118,12 @@ public function __invoke( $args, $assoc_args ) { // Update PO-Revision-Date to current date and time in UTC. // Uses gmdate() for consistency across different server timezones. - $po_translations->setHeader( 'PO-Revision-Date', gmdate( 'Y-m-d\TH:i:sP' ) ); + $po_translations->getHeaders()->set( 'PO-Revision-Date', gmdate( 'Y-m-d\TH:i:sP' ) ); // Reorder translations to match POT file order. $ordered_translations = $this->reorder_translations( $po_translations, $pot_translations ); - if ( ! $ordered_translations->toPoFile( $file->getPathname() ) ) { + if ( ! PoGenerator::toFile( $ordered_translations, $file->getPathname() ) ) { WP_CLI::warning( sprintf( 'Could not update file %s', $file->getPathname() ) ); continue; } @@ -233,6 +220,43 @@ private function restore_file_comments( $file_path, $comments ) { return true; } + /** + * Merges the entries of a POT file into the translations of a PO file. + * + * @param Translations $po_translations Translations of the PO file. + * @param Translations $pot_translations Translations of the POT file. + * @param bool $purge Whether to remove obsolete entries and replace translator comments. + * @return Translations The merged translations. + */ + private function merge_translations( Translations $po_translations, Translations $pot_translations, $purge ) { + $merged = clone $po_translations; + + // The text domain is taken from the POT file if both files have one. + $pot_domain = $pot_translations->getDomain(); + if ( ! empty( $merged->getDomain() ) && ! empty( $pot_domain ) ) { + $merged->getHeaders()->set( Headers::HEADER_DOMAIN, $pot_domain ); + } + + $strategy = Merge::EXTRACTED_COMMENTS_THEIRS | Merge::REFERENCES_THEIRS; + + if ( $purge ) { + $strategy |= Merge::COMMENTS_THEIRS; + + foreach ( $merged->getTranslations() as $po_entry ) { + if ( ! $pot_translations->has( $po_entry ) ) { + $merged->remove( $po_entry ); + } + } + } + + foreach ( $pot_translations as $pot_entry ) { + /** @var Translation $pot_entry */ + $merged->addOrMerge( clone $pot_entry, $strategy ); + } + + return $merged; + } + /** * Check if two Translations objects differ. * @@ -248,7 +272,7 @@ private function translations_differ( Translations $original, Translations $upda // Compare each translation entry. foreach ( $original as $translation ) { - /** @var \Gettext\Translation $translation */ + /** @var Translation $translation */ $context = $translation->getContext(); $original_str = $translation->getOriginal(); @@ -274,8 +298,8 @@ private function translations_differ( Translations $original, Translations $upda } // Compare references (source code locations). - $original_refs = $translation->getReferences(); - $updated_refs = $updated_translation->getReferences(); + $original_refs = OrderedReferences::entries( $translation->getReferences() ); + $updated_refs = OrderedReferences::entries( $updated_translation->getReferences() ); $original_refs_sorted = $original_refs; $updated_refs_sorted = $updated_refs; @@ -288,11 +312,11 @@ private function translations_differ( Translations $original, Translations $upda } // Compare comments. - if ( $translation->getExtractedComments() !== $updated_translation->getExtractedComments() ) { + if ( $translation->getExtractedComments()->toArray() !== $updated_translation->getExtractedComments()->toArray() ) { return true; } - if ( $translation->getComments() !== $updated_translation->getComments() ) { + if ( $translation->getComments()->toArray() !== $updated_translation->getComments()->toArray() ) { return true; } } @@ -309,28 +333,28 @@ private function translations_differ( Translations $original, Translations $upda * @return Translations Translations object with entries in POT file order. */ private function reorder_translations( Translations $po_translations, Translations $pot_translations ) { - $ordered = new Translations(); + $ordered = Translations::create(); // Copy headers from the merged PO translations. - foreach ( $po_translations->getHeaders() as $name => $value ) { - $ordered->setHeader( $name, $value ); + foreach ( $po_translations->getHeaders()->toArray() as $name => $value ) { + $ordered->getHeaders()->set( $name, $value ); } // Add translations in POT file order. foreach ( $pot_translations as $pot_entry ) { - /** @var \Gettext\Translation $pot_entry */ - $po_entry = $po_translations->find( $pot_entry ); + /** @var Translation $pot_entry */ + $po_entry = $po_translations->find( $pot_entry->getContext(), $pot_entry->getOriginal() ); if ( $po_entry ) { - $ordered[] = $po_entry->getClone(); + $ordered->add( clone $po_entry ); } } // Add any remaining translations from PO that aren't in POT (e.g., obsolete/disabled translations). foreach ( $po_translations as $po_entry ) { - /** @var \Gettext\Translation $po_entry */ + /** @var Translation $po_entry */ // Check if this entry is already in the ordered set. - if ( ! $ordered->find( $po_entry ) ) { - $ordered[] = $po_entry->getClone(); + if ( ! $ordered->has( $po_entry ) ) { + $ordered->add( clone $po_entry ); } } diff --git a/stubs/Gettext.stub b/stubs/Gettext.stub deleted file mode 100644 index e25e86aa..00000000 --- a/stubs/Gettext.stub +++ /dev/null @@ -1,36 +0,0 @@ - - */ - public static $options; - - /** - * @var string - */ - protected static $functionsScannerClass; - } - - abstract class JsCode { - /** - * @var array - */ - public static $options; - - /** - * @var string - */ - protected static $functionsScannerClass; - } -} - -namespace Gettext\Generators { - class PhpArray { - /** - * @var array - */ - public static $options; - } -} diff --git a/tests/BladeGettextExtractorTest.php b/tests/BladeGettextExtractorTest.php index 3b44fa5a..e39b896b 100644 --- a/tests/BladeGettextExtractorTest.php +++ b/tests/BladeGettextExtractorTest.php @@ -2,8 +2,8 @@ namespace WP_CLI\I18n\Tests; -use Gettext\Translations; use WP_CLI\I18n\BladeCodeExtractor; +use WP_CLI\I18n\Translations; use WP_CLI\Tests\TestCase; class BladeGettextExtractorTest extends TestCase { @@ -16,7 +16,7 @@ class BladeGettextExtractorTest extends TestCase { * @return Translations */ private function extract( $blade, $domain = 'foo-theme' ) { - $translations = new Translations(); + $translations = Translations::create(); $translations->setDomain( $domain ); $options = array_merge( @@ -34,7 +34,7 @@ public function test_extracts_bound_prop_with_translation_function() { '' ); - $this->assertNotFalse( $translations->find( null, 'Hello' ) ); + $this->assertNotNull( $translations->find( null, 'Hello' ) ); } public function test_extracts_multiple_bound_props() { @@ -42,8 +42,8 @@ public function test_extracts_multiple_bound_props() { '' ); - $this->assertNotFalse( $translations->find( null, 'Not found' ) ); - $this->assertNotFalse( $translations->find( null, 'Try again' ) ); + $this->assertNotNull( $translations->find( null, 'Not found' ) ); + $this->assertNotNull( $translations->find( null, 'Try again' ) ); } public function test_extracts_bound_props_from_multiline_component_tag() { @@ -56,8 +56,8 @@ public function test_extracts_bound_props_from_multiline_component_tag() { $translations = $this->extract( $blade ); - $this->assertNotFalse( $translations->find( null, 'Page not found' ) ); - $this->assertNotFalse( $translations->find( null, 'Please try again' ) ); + $this->assertNotNull( $translations->find( null, 'Page not found' ) ); + $this->assertNotNull( $translations->find( null, 'Please try again' ) ); } public function test_extracts_bound_props_from_open_component_tag() { @@ -69,8 +69,8 @@ public function test_extracts_bound_props_from_open_component_tag() { $translations = $this->extract( $blade ); - $this->assertNotFalse( $translations->find( null, 'Warning message' ) ); - $this->assertNotFalse( $translations->find( null, 'Content inside' ) ); + $this->assertNotNull( $translations->find( null, 'Warning message' ) ); + $this->assertNotNull( $translations->find( null, 'Content inside' ) ); } public function test_ignores_static_props() { @@ -78,8 +78,8 @@ public function test_ignores_static_props() { '' ); - $this->assertNotFalse( $translations->find( null, 'Hello' ) ); - $this->assertFalse( $translations->find( null, 'warning' ) ); + $this->assertNotNull( $translations->find( null, 'Hello' ) ); + $this->assertNull( $translations->find( null, 'warning' ) ); } public function test_does_not_match_non_component_html() { @@ -87,7 +87,7 @@ public function test_does_not_match_non_component_html() { '{{ __(\'Link text\', \'foo-theme\') }}' ); - $this->assertNotFalse( $translations->find( null, 'Link text' ) ); + $this->assertNotNull( $translations->find( null, 'Link text' ) ); // Only 1 translation should exist. $this->assertCount( 1, $translations ); } @@ -98,7 +98,7 @@ public function test_extracts_context_function_in_prop() { ); $translation = $translations->find( 'verb', 'Read' ); - $this->assertNotFalse( $translation ); + $this->assertNotNull( $translation ); } public function test_extracts_esc_functions_in_props() { @@ -111,8 +111,8 @@ public function test_extracts_esc_functions_in_props() { $translations = $this->extract( $blade ); - $this->assertNotFalse( $translations->find( null, 'Username' ) ); - $this->assertNotFalse( $translations->find( null, 'Enter username' ) ); + $this->assertNotNull( $translations->find( null, 'Username' ) ); + $this->assertNotNull( $translations->find( null, 'Enter username' ) ); } public function test_extracts_single_quoted_bound_props() { @@ -120,7 +120,7 @@ public function test_extracts_single_quoted_bound_props() { "" ); - $this->assertNotFalse( $translations->find( null, 'Single quoted' ) ); + $this->assertNotNull( $translations->find( null, 'Single quoted' ) ); } public function test_existing_blade_extraction_still_works() { @@ -135,9 +135,9 @@ public function test_existing_blade_extraction_still_works() { $translations = $this->extract( $blade ); - $this->assertNotFalse( $translations->find( null, 'PHP block string' ) ); - $this->assertNotFalse( $translations->find( null, 'Echo string' ) ); - $this->assertNotFalse( $translations->find( null, 'Raw string' ) ); - $this->assertNotFalse( $translations->find( null, 'Directive string' ) ); + $this->assertNotNull( $translations->find( null, 'PHP block string' ) ); + $this->assertNotNull( $translations->find( null, 'Echo string' ) ); + $this->assertNotNull( $translations->find( null, 'Raw string' ) ); + $this->assertNotNull( $translations->find( null, 'Directive string' ) ); } } diff --git a/tests/JsFunctionsScannerTest.php b/tests/JsFunctionsScannerTest.php index afc9d51a..2d0b1e31 100644 --- a/tests/JsFunctionsScannerTest.php +++ b/tests/JsFunctionsScannerTest.php @@ -2,8 +2,8 @@ namespace WP_CLI\I18n\Tests; -use Gettext\Translations; use WP_CLI\I18n\JsCodeExtractor; +use WP_CLI\I18n\Translations; use WP_CLI\Tests\TestCase; class JsFunctionsScannerTest extends TestCase { @@ -16,7 +16,7 @@ class JsFunctionsScannerTest extends TestCase { * @return Translations */ private function extract( $code, array $options = [] ) { - $translations = new Translations(); + $translations = Translations::create(); JsCodeExtractor::fromString( $code, $translations, $options + [ 'file' => 'test.js' ] ); @@ -39,8 +39,8 @@ public function test_ignores_comments_if_comment_extraction_is_disabled() { ); $translation = $translations->find( null, 'Hello' ); - $this->assertNotFalse( $translation ); - $this->assertSame( [], $translation->getExtractedComments() ); + $this->assertNotNull( $translation ); + $this->assertSame( [], $translation->getExtractedComments()->toArray() ); } public function test_extracts_all_comments_with_empty_prefix() { @@ -50,8 +50,8 @@ public function test_extracts_all_comments_with_empty_prefix() { ); $translation = $translations->find( null, 'Hello' ); - $this->assertNotFalse( $translation ); - $this->assertSame( [ 'Any comment.' ], $translation->getExtractedComments() ); + $this->assertNotNull( $translation ); + $this->assertSame( [ 'Any comment.' ], $translation->getExtractedComments()->toArray() ); } public function test_extracts_translator_comments_with_prefix() { @@ -61,8 +61,8 @@ public function test_extracts_translator_comments_with_prefix() { ); $translation = $translations->find( null, 'Hello' ); - $this->assertNotFalse( $translation ); - $this->assertSame( [ 'translators: A comment.' ], $translation->getExtractedComments() ); + $this->assertNotNull( $translation ); + $this->assertSame( [ 'translators: A comment.' ], $translation->getExtractedComments()->toArray() ); } public function test_extracts_calls_with_unicode_escaped_names() { @@ -70,8 +70,8 @@ public function test_extracts_calls_with_unicode_escaped_names() { "\\u005f\\u005f( 'Escaped', 'foo-plugin' );\n_\\u{78}( 'Braced', 'context', 'foo-plugin' );" ); - $this->assertNotFalse( $translations->find( null, 'Escaped' ) ); - $this->assertNotFalse( $translations->find( 'context', 'Braced' ) ); + $this->assertNotNull( $translations->find( null, 'Escaped' ) ); + $this->assertNotNull( $translations->find( 'context', 'Braced' ) ); } public function test_skips_code_with_unrelated_unicode_escapes() { diff --git a/tests/PhpFunctionsScannerTest.php b/tests/PhpFunctionsScannerTest.php new file mode 100644 index 00000000..a0d51a5e --- /dev/null +++ b/tests/PhpFunctionsScannerTest.php @@ -0,0 +1,106 @@ +assertSame( $expected, PhpFunctionsScanner::convertString( $literal ) ); + } + + /** + * @return array + */ + public static function data_convert_string() { + return [ + 'single quoted' => [ "'Hello'", 'Hello' ], + 'double quoted' => [ '"Hello"', 'Hello' ], + 'escaped single quote' => [ "'It\\'s'", "It's" ], + 'single quoted backslashes' => [ "'a\\\\b\\nc'", 'a\\b\\nc' ], + 'double quoted escapes' => [ '"a\\nb\\tc\\\\d\\"e\\$f"', "a\nb\tc\\d\"e\$f" ], + 'hexadecimal and octal' => [ '"\\x41\\101"', 'AA' ], + 'unicode code point' => [ '"\\u{e9}\\u{1f600}"', "\u{e9}\u{1f600}" ], + 'unknown escape is kept' => [ '"100\\%"', '100\\%' ], + ]; + } + + public function test_finds_functions_with_arguments_and_comments() { + $code = <<<'PHP' +enableCommentsExtraction( [ 'translators' ] ); + + $functions = []; + foreach ( $scanner->getFunctions() as $function ) { + $comments = []; + foreach ( $function[3] ?? [] as $comment ) { + $comments[] = $comment->getComment(); + } + + $functions[] = [ $function[0], $function[1], $function[2], $comments ]; + } + + $this->assertSame( + [ + [ '__', 3, [ 'Hello %s', 'foo' ], [ 'translators: %s: Name' ] ], + [ 'printf', 3, [ null, null ], [ 'translators: %s: Name' ] ], + [ '_x', 4, [ 'Concat', 'context', 'foo' ], [] ], + [ '__', 5, [ 'With ', 'foo' ], [] ], + [ 'esc_html__', 6, [ 'inner', 'foo' ], [] ], + [ '__', 6, [ 'Nested ', null ], [] ], + ], + $functions + ); + } + + public function test_extracts_strings_for_text_domain() { + $code = <<<'PHP' + 'foo-plugin.php', + 'addReferences' => true, + ] + ); + + $this->assertCount( 2, $translations ); + $this->assertNotNull( $translations->find( null, '0' ) ); + + $translation = $translations->find( null, '%d item' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( '%d items', $translation->getPlural() ); + $this->assertSame( [ 'php-format' ], $translation->getFlags()->toArray() ); + $this->assertSame( [ 'translators: %d: Number of items.' ], $translation->getExtractedComments()->toArray() ); + $this->assertSame( [ [ 'foo-plugin.php', 3 ] ], $translation->getReferenceEntries() ); + } +} diff --git a/tests/PoLoaderTest.php b/tests/PoLoaderTest.php new file mode 100644 index 00000000..1196489b --- /dev/null +++ b/tests/PoLoaderTest.php @@ -0,0 +1,191 @@ +loadString( $po ); + + $this->assertInstanceOf( Translations::class, $translations ); + + return $translations; + } + + public function test_keeps_strings_and_translations_that_are_zero() { + $po = <<<'PO' +msgid "" +msgstr "" +"Language: de\n" + +msgid "0" +msgstr "" + +msgctxt "start of week" +msgid "1" +msgstr "0" + +msgid "%d item" +msgid_plural "%d items" +msgstr[0] "0" +msgstr[1] "0" + +PO; + + $translations = $this->load( $po ); + + $this->assertCount( 3, $translations ); + $this->assertNotNull( $translations->find( null, '0' ) ); + + $translation = $translations->find( 'start of week', '1' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( '0', $translation->getTranslation() ); + + $result = PoGenerator::toString( $translations ); + + $this->assertStringContainsString( "msgid \"0\"\nmsgstr \"\"\n", $result ); + $this->assertStringContainsString( "msgid \"1\"\nmsgstr \"0\"\n", $result ); + $this->assertStringContainsString( "msgstr[0] \"0\"\nmsgstr[1] \"0\"\n", $result ); + } + + public function test_keeps_order_of_headers_flags_and_references() { + $po = <<<'PO' +msgid "" +msgstr "" +"X-Generator: Poedit\n" +"Language: de\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"A-Custom-Header: multi " +"line\n" + +#: b.php:2 a.php:1 +#: b.php:1 +#: c.php +#, php-format, fuzzy,js-format +msgid "Hello %s" +msgstr "Hello %s (DE)" + +PO; + + $translations = $this->load( $po ); + + $this->assertSame( + [ 'POT-Creation-Date', 'PO-Revision-Date', 'Language', 'X-Generator', 'Plural-Forms', 'A-Custom-Header' ], + array_slice( array_keys( $translations->getHeaders()->toArray() ), 7 ) + ); + $this->assertSame( 'de', $translations->getLanguage() ); + $this->assertSame( 'multi line', $translations->getHeaders()->get( 'A-Custom-Header' ) ); + + $translation = $translations->find( null, 'Hello %s' ); + $this->assertInstanceOf( Translation::class, $translation ); + + $this->assertSame( [ 'php-format', 'fuzzy', 'js-format' ], $translation->getFlags()->toArray() ); + $this->assertSame( + [ + [ 'b.php', 2 ], + [ 'a.php', 1 ], + [ 'b.php', 1 ], + [ 'c.php', null ], + ], + $translation->getReferenceEntries() + ); + + $result = PoGenerator::toString( $translations ); + + $this->assertStringContainsString( "#: b.php:2\n#: a.php:1\n#: b.php:1\n#: c.php\n#, php-format,fuzzy,js-format\n", $result ); + } + + public function test_merges_duplicate_entries() { + $po = <<<'PO' +#: a.php:1 +msgid "Hello" +msgstr "" + +#. A comment +#: b.php:2 +msgid "Hello" +msgstr "Hello (DE)" + +PO; + + $translations = $this->load( $po ); + + $this->assertCount( 1, $translations ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertInstanceOf( Translation::class, $translation ); + + $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); + $this->assertSame( [ 'A comment' ], $translation->getExtractedComments()->toArray() ); + $this->assertCount( 2, $translation->getReferenceEntries() ); + } + + public function test_loads_multiline_and_disabled_entries() { + $po = <<<'PO' +msgctxt "context" +msgid "" +"First\n" +"second \"quoted\"" +msgstr "" +"First (DE)\n" +"second (DE)" + +#~ msgid "Obsolete " +#~ "string" +#~ msgstr "Obsolete (DE)" +PO; + + $translations = $this->load( $po ); + + $translation = $translations->find( 'context', "First\nsecond \"quoted\"" ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( "First (DE)\nsecond (DE)", $translation->getTranslation() ); + $this->assertFalse( $translation->isDisabled() ); + + $translation = $translations->find( null, 'Obsolete string' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( 'Obsolete (DE)', $translation->getTranslation() ); + $this->assertTrue( $translation->isDisabled() ); + + $result = PoGenerator::toString( $translations ); + + $this->assertStringContainsString( "msgctxt \"context\"\nmsgid \"\"\n\"First\\n\"\n\"second \\\"quoted\\\"\"\n", $result ); + $this->assertStringContainsString( "#~ msgid \"Obsolete string\"\n#~ msgstr \"Obsolete (DE)\"\n", $result ); + } + + public function test_loads_multiple_files_into_one_set() { + $dir = sys_get_temp_dir() . '/' . uniqid( 'wp-cli-i18n-test-', true ); + mkdir( $dir ); + + file_put_contents( "$dir/a.pot", "#: a.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n" ); + file_put_contents( "$dir/b.pot", "#: b.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n\nmsgid \"World\"\nmsgstr \"\"\n" ); + + $translations = PoLoader::fromFiles( [ "$dir/a.pot", "$dir/b.pot" ] ); + + unlink( "$dir/a.pot" ); + unlink( "$dir/b.pot" ); + rmdir( $dir ); + + $this->assertCount( 2, $translations ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( + [ + [ 'a.php', 1 ], + [ 'b.php', 1 ], + ], + $translation->getReferenceEntries() + ); + } +} diff --git a/tests/PotGeneratorTest.php b/tests/PotGeneratorTest.php index 2e7ff389..226c3099 100644 --- a/tests/PotGeneratorTest.php +++ b/tests/PotGeneratorTest.php @@ -2,18 +2,15 @@ namespace WP_CLI\I18n\Tests; -use Gettext\Translation; use WP_CLI\I18n\PotGenerator; -use Gettext\Translations; +use WP_CLI\I18n\Translations; use WP_CLI\Tests\TestCase; class PotGeneratorTest extends TestCase { public function test_adds_correct_amount_of_plural_strings() { - $translations = new Translations(); + $translations = Translations::create(); - $translation = new Translation( '', '%d cat', '%d cats' ); - - $translations[] = $translation; + $translations->insert( null, '%d cat', '%d cats' ); $result = PotGenerator::toString( $translations ); diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php new file mode 100644 index 00000000..56804d13 --- /dev/null +++ b/tests/TranslationTest.php @@ -0,0 +1,131 @@ +translate( '0' ); + $theirs = Translation::create( null, '1' ); + + $this->assertSame( '0', $ours->mergeWith( $theirs )->getTranslation() ); + $this->assertSame( '0', $theirs->mergeWith( $ours )->getTranslation() ); + + $theirs->translate( '7' ); + + $this->assertSame( '0', $ours->mergeWith( $theirs )->getTranslation() ); + $this->assertSame( '7', $ours->mergeWith( $theirs, Merge::TRANSLATIONS_OVERRIDE )->getTranslation() ); + $this->assertSame( '0', $theirs->mergeWith( $ours, Merge::TRANSLATIONS_OVERRIDE )->getTranslation() ); + } + + public function test_merge_keeps_disabled_state() { + $ours = Translation::create( null, 'Hello' )->disable(); + $theirs = Translation::create( null, 'Hello' ); + + $this->assertTrue( $ours->mergeWith( $theirs )->isDisabled() ); + $this->assertFalse( $theirs->mergeWith( $ours )->isDisabled() ); + } + + public function test_merge_keeps_order_of_flags_and_references() { + $ours = Translation::create( null, 'Hello %s' ); + $ours->getFlags()->add( 'php-format' ); + $ours->getReferences()->add( 'b.php', 2 )->add( 'a.php', 1 ); + + $theirs = Translation::create( null, 'Hello %s' ); + $theirs->getFlags()->add( 'js-format', 'fuzzy' ); + $theirs->getReferences()->add( 'a.js', 3 )->add( 'b.php', 1 )->add( 'a.php', 1 ); + + $merged = $ours->mergeWith( $theirs ); + + $this->assertInstanceOf( Translation::class, $merged ); + $this->assertSame( [ 'php-format', 'js-format', 'fuzzy' ], $merged->getFlags()->toArray() ); + $this->assertSame( + [ + [ 'b.php', 2 ], + [ 'a.php', 1 ], + [ 'a.js', 3 ], + [ 'b.php', 1 ], + ], + $merged->getReferenceEntries() + ); + + $merged = $ours->mergeWith( $theirs, Merge::REFERENCES_THEIRS | Merge::FLAGS_THEIRS ); + + $this->assertInstanceOf( Translation::class, $merged ); + $this->assertSame( [ 'js-format', 'fuzzy' ], $merged->getFlags()->toArray() ); + $this->assertSame( + [ + [ 'a.js', 3 ], + [ 'b.php', 1 ], + [ 'a.php', 1 ], + ], + $merged->getReferenceEntries() + ); + } + + public function test_insert_returns_existing_translation() { + $translations = Translations::create(); + + $first = $translations->insert( 'context', 'Hello' ); + $first->getReferences()->add( 'a.php', 1 ); + + $second = $translations->insert( 'context', 'Hello', 'Hellos' ); + + $this->assertSame( $first, $second ); + $this->assertCount( 1, $translations ); + $this->assertSame( 'Hellos', $first->getPlural() ); + $this->assertTrue( $first->hasPlural() ); + $this->assertTrue( $first->hasContext() ); + $this->assertFalse( $first->hasTranslation() ); + } + + public function test_translations_have_default_headers_in_fixed_order() { + $translations = Translations::create( 'foo-plugin' ); + $translations->getHeaders()->set( 'A-Header', 'value' ); + + $this->assertSame( + [ + 'Project-Id-Version', + 'Report-Msgid-Bugs-To', + 'Last-Translator', + 'Language-Team', + 'MIME-Version', + 'Content-Type', + 'Content-Transfer-Encoding', + 'POT-Creation-Date', + 'PO-Revision-Date', + 'Language', + 'X-Domain', + 'A-Header', + ], + array_keys( $translations->getHeaders()->toArray() ) + ); + } + + public function test_mo_file_includes_headers_and_translations_that_are_zero() { + $translations = Translations::create(); + $translations->getHeaders()->set( 'Plural-Forms', 'nplurals=2; plural=(n != 1);' ); + + $translations->insert( 'start of week', '1' )->translate( '0' ); + $translations->insert( null, 'Untranslated' ); + $translations->insert( null, 'Disabled' )->translate( 'Disabled (DE)' )->disable(); + $translations->insert( null, '%d item', '%d items' )->translate( '%d item (DE)' )->translatePlural( '%d items (DE)' ); + + $mo = MoGenerator::toString( $translations ); + + $this->assertSame( 0x950412de, current( (array) unpack( 'L', $mo ) ) ); + $this->assertSame( 3, current( (array) unpack( 'L', substr( $mo, 8 ) ) ) ); + $this->assertStringContainsString( "Plural-Forms: nplurals=2; plural=(n != 1);\n", $mo ); + $this->assertStringContainsString( "start of week\x041\x00", $mo ); + $this->assertStringContainsString( "%d item\x00%d items\x00", $mo ); + $this->assertStringContainsString( "%d item (DE)\x00%d items (DE)\x00", $mo ); + $this->assertStringNotContainsString( 'Untranslated', $mo ); + $this->assertStringNotContainsString( 'Disabled', $mo ); + } +} From fa44c45c33b5680dc5d65e424bb42fdd4710a130 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 17 Nov 2026 10:45:18 +0000 Subject: [PATCH 02/14] Fix PHPStan errors Narrow the return types of the `create()` factories, read comments through `toArray()`, and make numeric ranges explicit. --- src/JedGenerator.php | 2 +- src/MoGenerator.php | 2 +- src/PhpFunctionsScanner.php | 8 +++++--- src/PoGenerator.php | 6 +++--- src/PoLoader.php | 19 ++----------------- src/PotGenerator.php | 2 +- src/Translation.php | 12 ++++++++++++ src/Translations.php | 13 ++++++++++++- 8 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/JedGenerator.php b/src/JedGenerator.php index d21c7c5a..8431ceb1 100644 --- a/src/JedGenerator.php +++ b/src/JedGenerator.php @@ -62,7 +62,7 @@ public static function toString( Translations $translations, array $options = [] */ public static function buildMessages( Translations $translations ) { $plural_forms = $translations->getHeaders()->getPluralForm(); - $number_of_plurals = is_array( $plural_forms ) ? $plural_forms[0] - 1 : null; + $number_of_plurals = is_array( $plural_forms ) ? (int) $plural_forms[0] - 1 : null; $messages = []; $context_glue = chr( 4 ); diff --git a/src/MoGenerator.php b/src/MoGenerator.php index 461af62b..534a361e 100644 --- a/src/MoGenerator.php +++ b/src/MoGenerator.php @@ -54,7 +54,7 @@ public static function toString( Translations $translations, array $options = [] $translations_index = []; $plural_form = $translations->getHeaders()->getPluralForm(); - $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : null; + $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : null; foreach ( $messages as $original_string => $translation ) { $original_string = (string) $original_string; diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index 50fdb7a2..b3fc2939 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -241,11 +241,11 @@ static function ( $matches ) { case '\\': return '\\'; case 'x': - return chr( (int) hexdec( substr( $matches[1], 1 ) ) ); + return chr( (int) hexdec( substr( $matches[1], 1 ) ) & 0xFF ); case 'u': return (string) self::unicodeChar( (int) hexdec( trim( substr( $matches[1], 1 ), '{}' ) ) ); default: - return chr( (int) octdec( $matches[1] ) ); + return chr( (int) octdec( $matches[1] ) & 0xFF ); } }, $value @@ -260,7 +260,7 @@ static function ( $matches ) { */ protected static function unicodeChar( $dec ) { if ( $dec < 0x80 ) { - return chr( $dec ); + return chr( $dec & 0x7F ); } if ( $dec < 0x0800 ) { @@ -309,6 +309,8 @@ public function saveGettextFunctions( Translations $translations, array $options $constants = []; } + /** @var array $constants */ + foreach ( $this->getFunctions( $constants ) as $function ) { list( $name, $line, $args ) = $function; diff --git a/src/PoGenerator.php b/src/PoGenerator.php index af2eadc0..7fbe6c02 100644 --- a/src/PoGenerator.php +++ b/src/PoGenerator.php @@ -19,7 +19,7 @@ class PoGenerator extends Generator { */ public static function toString( Translations $translations, array $options = [] ) { $plural_form = $translations->getHeaders()->getPluralForm(); - $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : null; + $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : null; $lines = static::getHeaderLines( $translations ); @@ -71,11 +71,11 @@ protected static function getHeaderLines( Translations $translations ) { * @return string Prefix to use for the remaining lines of the entry. */ protected static function addMetaLines( array &$lines, Translation $translation ) { - foreach ( $translation->getComments() as $comment ) { + foreach ( $translation->getComments()->toArray() as $comment ) { $lines[] = '# ' . $comment; } - foreach ( $translation->getExtractedComments() as $comment ) { + foreach ( $translation->getExtractedComments()->toArray() as $comment ) { $lines[] = '#. ' . $comment; } diff --git a/src/PoLoader.php b/src/PoLoader.php index 77b6deb2..4b5a38d4 100644 --- a/src/PoLoader.php +++ b/src/PoLoader.php @@ -36,7 +36,7 @@ public function loadString( string $text, ?GettextTranslations $translations = n $lines = explode( "\n", $text ); $count = count( $lines ); - $translation = $this->createTranslation( null, '' ); + $translation = Translation::create( null, '' ); for ( $i = 0; $i < $count; ++$i ) { $line = trim( $lines[ $i ] ); @@ -49,7 +49,7 @@ public function loadString( string $text, ?GettextTranslations $translations = n if ( '' === $line ) { self::addTranslation( $translation, $translations ); - $translation = $this->createTranslation( null, '' ); + $translation = Translation::create( null, '' ); continue; } @@ -131,21 +131,6 @@ protected function createTranslations(): GettextTranslations { return Translations::create(); } - /** - * {@inheritdoc} - * - * @return GettextTranslation - */ - protected function createTranslation( ?string $context, string $original, ?string $plural = null ): ?GettextTranslation { - $translation = Translation::create( $context, $original ); - - if ( null !== $plural ) { - $translation->setPlural( $plural ); - } - - return $translation; - } - /** * Adds a parsed entry to the translations. * diff --git a/src/PotGenerator.php b/src/PotGenerator.php index 8ad297cd..0a4f2fb0 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -44,7 +44,7 @@ public static function setCommentBeforeHeaders( $comment ) { */ public static function toString( Translations $translations, array $options = [] ) { $plural_form = $translations->getHeaders()->getPluralForm(); - $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : 1; + $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : 1; $lines = array_merge( static::$comments_before_headers, static::getHeaderLines( $translations ) ); diff --git a/src/Translation.php b/src/Translation.php index aea556c9..a093e3e3 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -19,6 +19,18 @@ protected function __construct( string $id ) { $this->flags = new OrderedFlags(); } + /** + * {@inheritdoc} + * + * @return static + */ + public static function create( ?string $context, string $original ): GettextTranslation { + /** @var static $translation */ + $translation = parent::create( $context, $original ); + + return $translation; + } + public function hasContext(): bool { return null !== $this->context && '' !== $this->context; } diff --git a/src/Translations.php b/src/Translations.php index 350b2b9e..b147da3d 100644 --- a/src/Translations.php +++ b/src/Translations.php @@ -30,6 +30,18 @@ protected function __construct() { ); } + /** + * {@inheritdoc} + * + * @return static + */ + public static function create( ?string $domain = null, ?string $language = null ): GettextTranslations { + /** @var static $translations */ + $translations = parent::create( $domain, $language ); + + return $translations; + } + /** * Returns the translation with the given context and original string, creating it if needed. * @@ -42,7 +54,6 @@ public function insert( ?string $context, string $original, ?string $plural = nu $translation = $this->find( $context, $original ); if ( ! $translation instanceof Translation ) { - /** @var Translation $translation */ $translation = Translation::create( $context, $original ); $this->add( $translation ); } From 3bb98262baf577fbd5f095526a7f13622ce4178d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 17 Nov 2026 10:47:03 +0000 Subject: [PATCH 03/14] Fix type annotation for scanner constants --- src/PhpFunctionsScanner.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index b3fc2939..9fee9627 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -304,12 +304,8 @@ public function saveGettextFunctions( Translations $translations, array $options $add_reference = ! empty( $options['addReferences'] ); - $constants = isset( $options['constants'] ) ? $options['constants'] : []; - if ( ! is_array( $constants ) ) { - $constants = []; - } - /** @var array $constants */ + $constants = isset( $options['constants'] ) && is_array( $options['constants'] ) ? $options['constants'] : []; foreach ( $this->getFunctions( $constants ) as $function ) { list( $name, $line, $args ) = $function; From 53da0b0e583d897ba043296c20a184311c5ae1c5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 17 Nov 2026 11:13:42 +0000 Subject: [PATCH 04/14] Keep data of plain gettext translations in insert() --- src/Translations.php | 14 ++++++++++++-- tests/TranslationTest.php | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Translations.php b/src/Translations.php index b147da3d..db32b804 100644 --- a/src/Translations.php +++ b/src/Translations.php @@ -51,10 +51,20 @@ public static function create( ?string $domain = null, ?string $language = null * @return Translation The existing or newly added translation. */ public function insert( ?string $context, string $original, ?string $plural = null ): Translation { - $translation = $this->find( $context, $original ); + $existing = $this->find( $context, $original ); - if ( ! $translation instanceof Translation ) { + if ( $existing instanceof Translation ) { + $translation = $existing; + } else { $translation = Translation::create( $context, $original ); + + // Keep the data of an entry that was added as a plain gettext translation. + if ( null !== $existing ) { + /** @var Translation $translation */ + $translation = $translation->mergeWith( $existing ); + $translation->disable( $existing->isDisabled() ); + } + $this->add( $translation ); } diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 56804d13..d0726081 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -85,6 +85,25 @@ public function test_insert_returns_existing_translation() { $this->assertFalse( $first->hasTranslation() ); } + public function test_insert_keeps_data_of_plain_gettext_translation() { + $translations = Translations::create(); + + $plain = \Gettext\Translation::create( null, 'Hello' )->translate( 'Hello (DE)' )->disable(); + $plain->getReferences()->add( 'a.php', 1 ); + $plain->getFlags()->add( 'fuzzy' ); + $translations->add( $plain ); + + $translation = $translations->insert( null, 'Hello', 'Hellos' ); + + $this->assertCount( 1, $translations ); + $this->assertSame( $translation, $translations->find( null, 'Hello' ) ); + $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); + $this->assertSame( 'Hellos', $translation->getPlural() ); + $this->assertTrue( $translation->isDisabled() ); + $this->assertSame( [ 'fuzzy' ], $translation->getFlags()->toArray() ); + $this->assertSame( [ [ 'a.php', 1 ] ], $translation->getReferenceEntries() ); + } + public function test_translations_have_default_headers_in_fixed_order() { $translations = Translations::create( 'foo-plugin' ); $translations->getHeaders()->set( 'A-Header', 'value' ); From 18737b38571cf2a266237f3b5e7ac5adbb44f42e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 17 Nov 2026 11:14:04 +0000 Subject: [PATCH 05/14] Assert the translated value in the MO generator test --- tests/TranslationTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index d0726081..0c95514d 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -144,6 +144,11 @@ public function test_mo_file_includes_headers_and_translations_that_are_zero() { $this->assertStringContainsString( "start of week\x041\x00", $mo ); $this->assertStringContainsString( "%d item\x00%d items\x00", $mo ); $this->assertStringContainsString( "%d item (DE)\x00%d items (DE)\x00", $mo ); + + $loaded = ( new \Gettext\Loader\MoLoader() )->loadString( $mo )->find( 'start of week', '1' ); + $this->assertNotNull( $loaded ); + $this->assertSame( '0', $loaded->getTranslation() ); + $this->assertStringNotContainsString( 'Untranslated', $mo ); $this->assertStringNotContainsString( 'Disabled', $mo ); } From 4cdfebc1e292f54517855c7cbe57fd68cf86ec03 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 17 Nov 2026 16:23:25 +0000 Subject: [PATCH 06/14] Use the header, flag and reference order of gettext/gettext v5 Drop the classes that kept headers, flags and references in the order they were added. Headers and flags are now sorted alphabetically and references are grouped by file, as the library does by default. The keys in generated PHP files keep their order. --- features/makepot.feature | 24 +++++++----- features/updatepo.feature | 13 ++++--- src/OrderedFlags.php | 21 ----------- src/OrderedHeaders.php | 36 ------------------ src/OrderedReferences.php | 79 --------------------------------------- src/PhpArrayGenerator.php | 10 +++-- src/PoGenerator.php | 2 +- src/Translation.php | 37 ++++++++++++------ src/Translations.php | 32 ++++++++-------- src/UpdatePoCommand.php | 4 +- tests/PoLoaderTest.php | 16 ++++---- tests/TranslationTest.php | 55 ++++----------------------- 12 files changed, 90 insertions(+), 239 deletions(-) delete mode 100644 src/OrderedFlags.php delete mode 100644 src/OrderedHeaders.php delete mode 100644 src/OrderedReferences.php diff --git a/features/makepot.feature b/features/makepot.feature index ecbef8d4..7adcffce 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -189,8 +189,8 @@ Feature: Generate a POT file of a WordPress project And I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot` Then the wp-content/plugins/hello-world/languages/hello-world.pot file should contain: """ - "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" + "Last-Translator: FULL NAME \n" """ Scenario: Sets the generator header @@ -1765,17 +1765,23 @@ Feature: Generate a POT file of a WordPress project # This file is distributed under the same license as the Hello World plugin. msgid "" msgstr "" - "Project-Id-Version: Hello World 0.1.0\n" - "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n" - "Last-Translator: FULL NAME \n" + "Content-Transfer-Encoding: 8bit\n" + "Content-Type: text/plain; charset=UTF-8\n" "Language-Team: LANGUAGE \n" + "Last-Translator: FULL NAME \n" "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=UTF-8\n" - "Content-Transfer-Encoding: 8bit\n" + """ + And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain: + """ + "Project-Id-Version: Hello World 0.1.0\n" + "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n" """ And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain: """ "POT-Creation-Date: 2018-05-02T22:06:24+00:00\n" + """ + And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain: + """ "PO-Revision-Date: 2018-05-02T22:06:24+00:00\n" """ And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain: @@ -4460,7 +4466,7 @@ Feature: Generate a POT file of a WordPress project #: foo-plugin.php:6 """ - Scenario: Keeps flags and references in the order they were found + Scenario: Groups references by file and sorts flags Given an empty foo-plugin directory And a foo-plugin/foo-plugin.php file: """ @@ -4506,9 +4512,9 @@ Feature: Generate a POT file of a WordPress project #: admin.php:3 #: foo-plugin.php:7 #: build/index.js:2 - #: build/view.js:2 #: build/index.js:4 - #, php-format,js-format + #: build/view.js:2 + #, js-format,php-format msgid "Hello %s" msgstr "" """ diff --git a/features/updatepo.feature b/features/updatepo.feature index 8fbaba1b..11e47c96 100644 --- a/features/updatepo.feature +++ b/features/updatepo.feature @@ -934,7 +934,7 @@ Feature: Update existing PO files from a POT file msgstr[1] "0" """ - Scenario: Keeps the order of headers, flags and references + Scenario: Sorts headers and flags and groups references by file Given an empty foo-plugin directory And a foo-plugin/foo-plugin.pot file: """ @@ -975,16 +975,17 @@ Feature: Update existing PO files from a POT file And STDERR should be empty And the foo-plugin/foo-plugin-de_DE.po file should contain: """ - "Language: de_DE\n" - "X-Poedit-Basepath: ..\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" - "X-Generator: Poedit 3.0\n" + "Project-Id-Version: Foo Plugin\n" + "Report-Msgid-Bugs-To: \n" "X-Domain: foo-plugin\n" + "X-Generator: Poedit 3.0\n" + "X-Poedit-Basepath: ..\n" #: foo-plugin.php:20 - #: build/index.js:1 #: foo-plugin.php:10 - #, php-format,fuzzy,js-format + #: build/index.js:1 + #, fuzzy,js-format,php-format msgid "Hello %s" msgstr "Hallo %s" """ diff --git a/src/OrderedFlags.php b/src/OrderedFlags.php deleted file mode 100644 index 185ce8eb..00000000 --- a/src/OrderedFlags.php +++ /dev/null @@ -1,21 +0,0 @@ -has( $flag ) ) { - $this->flags[] = $flag; - } - } - - return $this; - } -} diff --git a/src/OrderedHeaders.php b/src/OrderedHeaders.php deleted file mode 100644 index 5a41c11c..00000000 --- a/src/OrderedHeaders.php +++ /dev/null @@ -1,36 +0,0 @@ - $headers - */ - public function __construct( array $headers = [] ) { - parent::__construct(); - - $this->headers = $headers; - } - - public function set( string $name, string $value ): Headers { - $this->headers[ trim( $name ) ] = trim( $value ); - - return $this; - } - - public function mergeWith( Headers $headers ): Headers { - $merged = clone $this; - $merged->headers = $headers->toArray() + $merged->headers; - - return $merged; - } -} diff --git a/src/OrderedReferences.php b/src/OrderedReferences.php deleted file mode 100644 index d8615922..00000000 --- a/src/OrderedReferences.php +++ /dev/null @@ -1,79 +0,0 @@ - - */ - protected $entries = []; - - public function add( string $filename, ?int $line = null ): References { - $this->entries[ "{$filename}:{$line}" ] = [ $filename, $line ]; - - return parent::add( $filename, $line ); - } - - /** - * Returns all references in the order they were added. - * - * @return array List of file name and line number pairs. - */ - public function getEntries(): array { - return array_values( $this->entries ); - } - - public function count(): int { - return count( $this->entries ); - } - - public function mergeWith( References $references ): References { - if ( ! $references instanceof self ) { - return parent::mergeWith( $references ); - } - - $merged = clone $this; - - foreach ( $references->getEntries() as $entry ) { - $merged->add( $entry[0], $entry[1] ); - } - - return $merged; - } - - /** - * Returns the references of a translation in the order they were added. - * - * @param References $references References instance. - * @return array List of file name and line number pairs. - */ - public static function entries( References $references ): array { - if ( $references instanceof self ) { - return $references->getEntries(); - } - - $entries = []; - - foreach ( $references->toArray() as $filename => $lines ) { - if ( empty( $lines ) ) { - $entries[] = [ (string) $filename, null ]; - continue; - } - - foreach ( $lines as $line ) { - $entries[] = [ (string) $filename, $line ]; - } - } - - return $entries; - } -} diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 66e7da19..c312cc85 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -72,15 +72,17 @@ protected static function toArray( Translations $translations, $include_headers, } $headers_allowlist = [ + 'Project-Id-Version' => 'project-id-version', 'POT-Creation-Date' => 'pot-creation-date', 'PO-Revision-Date' => 'po-revision-date', - 'Project-Id-Version' => 'project-id-version', 'X-Generator' => 'x-generator', ]; - foreach ( $translations->getHeaders()->toArray() as $name => $value ) { - if ( isset( $headers_allowlist[ $name ] ) ) { - $result[ $headers_allowlist[ $name ] ] = $value; + foreach ( $headers_allowlist as $name => $key ) { + $value = $translations->getHeaders()->get( $name ); + + if ( null !== $value ) { + $result[ $key ] = $value; } } diff --git a/src/PoGenerator.php b/src/PoGenerator.php index 7fbe6c02..488f922c 100644 --- a/src/PoGenerator.php +++ b/src/PoGenerator.php @@ -79,7 +79,7 @@ protected static function addMetaLines( array &$lines, Translation $translation $lines[] = '#. ' . $comment; } - foreach ( OrderedReferences::entries( $translation->getReferences() ) as $reference ) { + foreach ( \WP_CLI\I18n\Translation::flattenReferences( $translation->getReferences() ) as $reference ) { $lines[] = '#: ' . $reference[0] . ( null !== $reference[1] ? ':' . $reference[1] : '' ); } diff --git a/src/Translation.php b/src/Translation.php index a093e3e3..6efb4055 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -3,22 +3,14 @@ namespace WP_CLI\I18n; use Gettext\Merge; +use Gettext\References; use Gettext\Translation as GettextTranslation; /** * A single translation entry. - * - * Keeps flags and references in the order they were added. */ class Translation extends GettextTranslation { - protected function __construct( string $id ) { - parent::__construct( $id ); - - $this->references = new OrderedReferences(); - $this->flags = new OrderedFlags(); - } - /** * {@inheritdoc} * @@ -55,12 +47,35 @@ public function hasPluralTranslations( bool $check_content = false ): bool { } /** - * Returns the references in the order they were added. + * Returns the references as a flat list. * * @return array List of file name and line number pairs. */ public function getReferenceEntries(): array { - return OrderedReferences::entries( $this->references ); + return self::flattenReferences( $this->getReferences() ); + } + + /** + * Turns references grouped by file name into a flat list. + * + * @param References $references References instance. + * @return array List of file name and line number pairs. + */ + public static function flattenReferences( References $references ): array { + $entries = []; + + foreach ( $references->toArray() as $filename => $lines ) { + if ( empty( $lines ) ) { + $entries[] = [ (string) $filename, null ]; + continue; + } + + foreach ( $lines as $line ) { + $entries[] = [ (string) $filename, $line ]; + } + } + + return $entries; } /** diff --git a/src/Translations.php b/src/Translations.php index db32b804..43fb6c99 100644 --- a/src/Translations.php +++ b/src/Translations.php @@ -7,27 +7,29 @@ /** * A set of translations. * - * Comes with a set of default headers and keeps headers in the order they were set. + * Comes with a set of default headers. */ class Translations extends GettextTranslations { protected function __construct() { parent::__construct(); - $this->headers = new OrderedHeaders( - [ - 'Project-Id-Version' => '', - 'Report-Msgid-Bugs-To' => '', - 'Last-Translator' => '', - 'Language-Team' => '', - 'MIME-Version' => '1.0', - 'Content-Type' => 'text/plain; charset=UTF-8', - 'Content-Transfer-Encoding' => '8bit', - 'POT-Creation-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - 'PO-Revision-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - 'Language' => '', - ] - ); + $defaults = [ + 'Project-Id-Version' => '', + 'Report-Msgid-Bugs-To' => '', + 'Last-Translator' => '', + 'Language-Team' => '', + 'MIME-Version' => '1.0', + 'Content-Type' => 'text/plain; charset=UTF-8', + 'Content-Transfer-Encoding' => '8bit', + 'POT-Creation-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date + 'PO-Revision-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date + 'Language' => '', + ]; + + foreach ( $defaults as $name => $value ) { + $this->headers->set( $name, $value ); + } } /** diff --git a/src/UpdatePoCommand.php b/src/UpdatePoCommand.php index 8a178960..e982b84c 100644 --- a/src/UpdatePoCommand.php +++ b/src/UpdatePoCommand.php @@ -298,8 +298,8 @@ private function translations_differ( Translations $original, Translations $upda } // Compare references (source code locations). - $original_refs = OrderedReferences::entries( $translation->getReferences() ); - $updated_refs = OrderedReferences::entries( $updated_translation->getReferences() ); + $original_refs = Translation::flattenReferences( $translation->getReferences() ); + $updated_refs = Translation::flattenReferences( $updated_translation->getReferences() ); $original_refs_sorted = $original_refs; $updated_refs_sorted = $updated_refs; diff --git a/tests/PoLoaderTest.php b/tests/PoLoaderTest.php index 1196489b..66f020ba 100644 --- a/tests/PoLoaderTest.php +++ b/tests/PoLoaderTest.php @@ -58,7 +58,7 @@ public function test_keeps_strings_and_translations_that_are_zero() { $this->assertStringContainsString( "msgstr[0] \"0\"\nmsgstr[1] \"0\"\n", $result ); } - public function test_keeps_order_of_headers_flags_and_references() { + public function test_sorts_headers_and_flags_and_groups_references() { $po = <<<'PO' msgid "" msgstr "" @@ -79,22 +79,22 @@ public function test_keeps_order_of_headers_flags_and_references() { $translations = $this->load( $po ); - $this->assertSame( - [ 'POT-Creation-Date', 'PO-Revision-Date', 'Language', 'X-Generator', 'Plural-Forms', 'A-Custom-Header' ], - array_slice( array_keys( $translations->getHeaders()->toArray() ), 7 ) - ); + $headers = array_keys( $translations->getHeaders()->toArray() ); + + $this->assertSame( 'A-Custom-Header', $headers[0] ); + $this->assertSame( 'X-Generator', end( $headers ) ); $this->assertSame( 'de', $translations->getLanguage() ); $this->assertSame( 'multi line', $translations->getHeaders()->get( 'A-Custom-Header' ) ); $translation = $translations->find( null, 'Hello %s' ); $this->assertInstanceOf( Translation::class, $translation ); - $this->assertSame( [ 'php-format', 'fuzzy', 'js-format' ], $translation->getFlags()->toArray() ); + $this->assertSame( [ 'fuzzy', 'js-format', 'php-format' ], $translation->getFlags()->toArray() ); $this->assertSame( [ [ 'b.php', 2 ], - [ 'a.php', 1 ], [ 'b.php', 1 ], + [ 'a.php', 1 ], [ 'c.php', null ], ], $translation->getReferenceEntries() @@ -102,7 +102,7 @@ public function test_keeps_order_of_headers_flags_and_references() { $result = PoGenerator::toString( $translations ); - $this->assertStringContainsString( "#: b.php:2\n#: a.php:1\n#: b.php:1\n#: c.php\n#, php-format,fuzzy,js-format\n", $result ); + $this->assertStringContainsString( "#: b.php:2\n#: b.php:1\n#: a.php:1\n#: c.php\n#, fuzzy,js-format,php-format\n", $result ); } public function test_merges_duplicate_entries() { diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 0c95514d..763f0018 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -32,43 +32,6 @@ public function test_merge_keeps_disabled_state() { $this->assertFalse( $theirs->mergeWith( $ours )->isDisabled() ); } - public function test_merge_keeps_order_of_flags_and_references() { - $ours = Translation::create( null, 'Hello %s' ); - $ours->getFlags()->add( 'php-format' ); - $ours->getReferences()->add( 'b.php', 2 )->add( 'a.php', 1 ); - - $theirs = Translation::create( null, 'Hello %s' ); - $theirs->getFlags()->add( 'js-format', 'fuzzy' ); - $theirs->getReferences()->add( 'a.js', 3 )->add( 'b.php', 1 )->add( 'a.php', 1 ); - - $merged = $ours->mergeWith( $theirs ); - - $this->assertInstanceOf( Translation::class, $merged ); - $this->assertSame( [ 'php-format', 'js-format', 'fuzzy' ], $merged->getFlags()->toArray() ); - $this->assertSame( - [ - [ 'b.php', 2 ], - [ 'a.php', 1 ], - [ 'a.js', 3 ], - [ 'b.php', 1 ], - ], - $merged->getReferenceEntries() - ); - - $merged = $ours->mergeWith( $theirs, Merge::REFERENCES_THEIRS | Merge::FLAGS_THEIRS ); - - $this->assertInstanceOf( Translation::class, $merged ); - $this->assertSame( [ 'js-format', 'fuzzy' ], $merged->getFlags()->toArray() ); - $this->assertSame( - [ - [ 'a.js', 3 ], - [ 'b.php', 1 ], - [ 'a.php', 1 ], - ], - $merged->getReferenceEntries() - ); - } - public function test_insert_returns_existing_translation() { $translations = Translations::create(); @@ -104,24 +67,22 @@ public function test_insert_keeps_data_of_plain_gettext_translation() { $this->assertSame( [ [ 'a.php', 1 ] ], $translation->getReferenceEntries() ); } - public function test_translations_have_default_headers_in_fixed_order() { + public function test_translations_have_default_headers() { $translations = Translations::create( 'foo-plugin' ); - $translations->getHeaders()->set( 'A-Header', 'value' ); $this->assertSame( [ - 'Project-Id-Version', - 'Report-Msgid-Bugs-To', - 'Last-Translator', + 'Content-Transfer-Encoding', + 'Content-Type', + 'Language', 'Language-Team', + 'Last-Translator', 'MIME-Version', - 'Content-Type', - 'Content-Transfer-Encoding', - 'POT-Creation-Date', 'PO-Revision-Date', - 'Language', + 'POT-Creation-Date', + 'Project-Id-Version', + 'Report-Msgid-Bugs-To', 'X-Domain', - 'A-Header', ], array_keys( $translations->getHeaders()->toArray() ) ); From 310c4f62cfb00d571c854d1ccec514258997b19a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 20 Nov 2026 21:42:10 +0000 Subject: [PATCH 07/14] Use the PO loader and generators of gettext/gettext 5.7.4 5.7.4 treats "0" as a valid string, so the local copies of the PO loader, the PO and MO generators and the `Translation` subclass are no longer needed. * PO and POT files are loaded with the library's loader. Entries that appear more than once in a file are still merged. * `make-mo` uses the library's MO generator. * `update-po` writes PO files with the library's generator. This keeps file-level comments with `--no-purge`, the `#, fuzzy` header flag and `#|` previous strings, and reactivates obsolete entries that are back in the POT file. * The POT generator keeps its own output format, as POT files need empty `msgstr[0]` and `msgstr[1]` lines for plurals. --- composer.json | 2 +- features/updatepo.feature | 51 ++++++ src/AuditCommand.php | 5 +- src/MakeJsonCommand.php | 5 +- src/MakeMoCommand.php | 5 +- src/MakePhpCommand.php | 2 +- src/MakePotCommand.php | 9 +- src/MoGenerator.php | 152 ------------------ src/PoGenerator.php | 164 -------------------- src/PoLoader.php | 250 ------------------------------ src/PotGenerator.php | 122 ++++++++++++++- src/Translation.php | 109 ------------- src/Translations.php | 107 +++++++++++-- src/UpdatePoCommand.php | 104 ++----------- tests/PhpFunctionsScannerTest.php | 4 +- tests/PoLoaderTest.php | 191 ----------------------- tests/TranslationTest.php | 116 -------------- tests/TranslationsTest.php | 207 +++++++++++++++++++++++++ 18 files changed, 505 insertions(+), 1100 deletions(-) delete mode 100644 src/MoGenerator.php delete mode 100644 src/PoGenerator.php delete mode 100644 src/PoLoader.php delete mode 100644 src/Translation.php delete mode 100644 tests/PoLoaderTest.php delete mode 100644 tests/TranslationTest.php create mode 100644 tests/TranslationsTest.php diff --git a/composer.json b/composer.json index d2d880d6..8eba4045 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "eftec/bladeone": "3.52", - "gettext/gettext": "^5.7", + "gettext/gettext": "^5.7.4", "mck89/peast": "^1.13.11", "wp-cli/wp-cli": "^3.0" }, diff --git a/features/updatepo.feature b/features/updatepo.feature index 11e47c96..2aa89990 100644 --- a/features/updatepo.feature +++ b/features/updatepo.feature @@ -989,3 +989,54 @@ Feature: Update existing PO files from a POT file msgid "Hello %s" msgstr "Hallo %s" """ + + Scenario: Reactivates obsolete translations that are back in the POT file + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.pot file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:1 + msgid "Some string" + msgstr "" + """ + And a foo-plugin/foo-plugin-de_DE.po file: + """ + # Copyright (C) 2018 Foo Plugin + #, fuzzy + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "Language: de_DE\n" + "X-Domain: foo-plugin\n" + + #~ msgid "Some string" + #~ msgstr "Some translated string" + """ + + When I run `wp i18n update-po foo-plugin/foo-plugin.pot --no-purge` + Then STDOUT should be: + """ + Success: Updated 1 file. + """ + And STDERR should be empty + And the foo-plugin/foo-plugin-de_DE.po file should contain: + """ + # Copyright (C) 2018 Foo Plugin + # + #, fuzzy + msgid "" + """ + And the foo-plugin/foo-plugin-de_DE.po file should contain: + """ + #: foo-plugin.php:1 + msgid "Some string" + msgstr "Some translated string" + """ + And the foo-plugin/foo-plugin-de_DE.po file should not contain: + """ + #~ msgid "Some string" + """ diff --git a/src/AuditCommand.php b/src/AuditCommand.php index b53f92aa..eb9e038d 100644 --- a/src/AuditCommand.php +++ b/src/AuditCommand.php @@ -2,6 +2,7 @@ namespace WP_CLI\I18n; +use Gettext\Translation; use WP_CLI; use WP_CLI\Path; use WP_CLI\Utils; @@ -394,7 +395,7 @@ protected function collect_audit_issues( $translations ) { foreach ( $translations as $translation ) { /** @var Translation $translation */ - $references = $translation->getReferenceEntries(); + $references = Translations::getReferenceList( $translation ); // File headers don't have any file references. if ( empty( $references ) ) { @@ -482,7 +483,7 @@ function ( $comment ) { ]; } - if ( $translation->hasPlural() ) { + if ( Translations::hasValue( $translation->getPlural() ) ) { preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $single_placeholders ); $single_placeholders = $single_placeholders[0]; diff --git a/src/MakeJsonCommand.php b/src/MakeJsonCommand.php index 6d170790..5dbf1c3f 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -2,6 +2,7 @@ namespace WP_CLI\I18n; +use Gettext\Translation; use WP_CLI; use WP_CLI_Command; use WP_CLI\Utils; @@ -236,7 +237,7 @@ static function ( $value ) { protected function make_json( $source_file, $destination, $map, $domain, $extensions ) { /** @var Translations[] $mapping */ $mapping = []; - $translations = PoLoader::fromFiles( $source_file ); + $translations = Translations::fromPoFiles( $source_file ); $result = []; $extensions = array_merge( [ 'js' ], $extensions ); @@ -271,7 +272,7 @@ static function ( $reference ) use ( $extensions ) { ? (string) preg_replace( "/\.min\.{$extension}$/", ".{$extension}", $file ) : null; }, - $this->reference_map( $translation->getReferenceEntries(), $map ) + $this->reference_map( Translations::getReferenceList( $translation ), $map ) ); $sources = array_unique( array_filter( $sources ) ); diff --git a/src/MakeMoCommand.php b/src/MakeMoCommand.php index 466ba1f2..51ea06bb 100644 --- a/src/MakeMoCommand.php +++ b/src/MakeMoCommand.php @@ -3,6 +3,7 @@ namespace WP_CLI\I18n; use DirectoryIterator; +use Gettext\Generator\MoGenerator; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -92,12 +93,12 @@ public function __invoke( $args, $assoc_args ) { } $destination_file = "{$destination}/{$file_name}"; - $translations = PoLoader::fromFiles( $file->getPathname() ); + $translations = Translations::fromPoFiles( $file->getPathname() ); // Remove JS-only strings from MO files to keep them small. $this->remove_js_only_strings( $translations ); - if ( ! MoGenerator::toFile( $translations, $destination_file ) ) { + if ( ! ( new MoGenerator() )->includeHeaders()->generateFile( $translations, $destination_file ) ) { WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) ); continue; } diff --git a/src/MakePhpCommand.php b/src/MakePhpCommand.php index ab810874..3f313b4f 100644 --- a/src/MakePhpCommand.php +++ b/src/MakePhpCommand.php @@ -89,7 +89,7 @@ public function __invoke( $args, $assoc_args ) { $file_basename = basename( $file->getFilename(), '.po' ); $destination_file = "{$destination}/{$file_basename}.l10n.php"; - $translations = PoLoader::fromFiles( $file->getPathname() ); + $translations = Translations::fromPoFiles( $file->getPathname() ); // Remove JS-only strings from PHP files to keep them small. $this->remove_js_only_strings( $translations ); diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 416c7500..8bf7c311 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -3,6 +3,7 @@ namespace WP_CLI\I18n; use Gettext\Headers; +use Gettext\Translation; use WP_CLI; use WP_CLI_Command; use WP_CLI\Path; @@ -462,7 +463,7 @@ function ( $file ) { WP_CLI::debug( sprintf( 'Ignoring any string already existing in: %s', $file ), 'make-pot' ); - $this->exceptions[ $file ] = PoLoader::fromFiles( $file ); + $this->exceptions[ $file ] = Translations::fromPoFiles( $file ); } } @@ -625,7 +626,7 @@ protected function extract_strings() { // Add existing strings first but don't keep headers. if ( ! empty( $this->merge ) ) { - foreach ( PoLoader::fromFiles( $this->merge ) as $existing_translation ) { + foreach ( Translations::fromPoFiles( $this->merge ) as $existing_translation ) { /** @var Translation $existing_translation */ $translations->add( $existing_translation ); } @@ -821,7 +822,7 @@ protected function audit_strings( $translations ) { foreach ( $translations as $translation ) { /** @var Translation $translation */ - $references = $translation->getReferenceEntries(); + $references = Translations::getReferenceList( $translation ); // File headers don't have any file references. $location = ! empty( $references ) ? '(' . implode( ':', $references[0] ) . ')' : ''; @@ -899,7 +900,7 @@ function ( $comment ) { WP_CLI::warning( $message ); } - if ( $translation->hasPlural() ) { + if ( Translations::hasValue( $translation->getPlural() ) ) { preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $single_placeholders ); $single_placeholders = $single_placeholders[0]; diff --git a/src/MoGenerator.php b/src/MoGenerator.php deleted file mode 100644 index 534a361e..00000000 --- a/src/MoGenerator.php +++ /dev/null @@ -1,152 +0,0 @@ - $options - * @return string - */ - public static function toString( Translations $translations, array $options = [] ) { - $headers = ''; - - foreach ( $translations->getHeaders()->toArray() as $name => $value ) { - $headers .= sprintf( "%s: %s\n", $name, $value ); - } - - /** @var array $messages */ - $messages = [ '' => $headers ]; - - foreach ( $translations as $translation ) { - /** @var Translation $translation */ - - if ( null === $translation->getTranslation() || '' === $translation->getTranslation() || $translation->isDisabled() ) { - continue; - } - - $original_string = $translation->getOriginal(); - - if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { - $original_string = $translation->getContext() . "\x04" . $original_string; - } - - $messages[ $original_string ] = $translation; - } - - ksort( $messages ); - - $num_entries = count( $messages ); - $originals_table = ''; - $translations_table = ''; - $originals_index = []; - $translations_index = []; - - $plural_form = $translations->getHeaders()->getPluralForm(); - $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : null; - - foreach ( $messages as $original_string => $translation ) { - $original_string = (string) $original_string; - - if ( is_string( $translation ) ) { - // Headers. - $translation_string = $translation; - } else { - $translation_string = (string) $translation->getTranslation(); - - if ( - null !== $translation->getPlural() && - '' !== $translation->getPlural() && - '' !== implode( '', $translation->getPluralTranslations() ) - ) { - $original_string .= "\x00" . $translation->getPlural(); - $translation_string .= "\x00" . implode( "\x00", $translation->getPluralTranslations( $plural_size ) ); - } - } - - $originals_index[] = [ - 'relativeOffset' => strlen( $originals_table ), - 'length' => strlen( $original_string ), - ]; - - $originals_table .= $original_string . "\x00"; - - $translations_index[] = [ - 'relativeOffset' => strlen( $translations_table ), - 'length' => strlen( $translation_string ), - ]; - - $translations_table .= $translation_string . "\x00"; - } - - // Offset of table with the original strings index: right after the header (which is 7 words). - $originals_index_offset = 7 * 4; - - // Size of table with the original strings index. - $originals_index_size = $num_entries * ( 4 + 4 ); - - // Offset of table with the translation strings index: right after the original strings index table. - $translations_index_offset = $originals_index_offset + $originals_index_size; - - // Size of table with the translation strings index. - $translations_index_size = $num_entries * ( 4 + 4 ); - - // Hashing table starts after the header and after the index table. - $originals_strings_offset = $translations_index_offset + $translations_index_size; - - // Translations start after the keys. - $translations_strings_offset = $originals_strings_offset + strlen( $originals_table ); - - // Magic number. - $mo = pack( 'L', 0x950412de ); - - // File format revision. - $mo .= pack( 'L', 0 ); - - // Number of strings. - $mo .= pack( 'L', $num_entries ); - - // Offset of table with original strings. - $mo .= pack( 'L', $originals_index_offset ); - - // Offset of table with translation strings. - $mo .= pack( 'L', $translations_index_offset ); - - // Size of hashing table: we don't use it. - $mo .= pack( 'L', 0 ); - - // Offset of hashing table: it would start right after the translations index table. - $mo .= pack( 'L', $translations_index_offset + $translations_index_size ); - - // Write the lengths & offsets of the original strings. - foreach ( $originals_index as $info ) { - $mo .= pack( 'L', $info['length'] ); - $mo .= pack( 'L', $originals_strings_offset + $info['relativeOffset'] ); - } - - // Write the lengths & offsets of the translated strings. - foreach ( $translations_index as $info ) { - $mo .= pack( 'L', $info['length'] ); - $mo .= pack( 'L', $translations_strings_offset + $info['relativeOffset'] ); - } - - // Write original strings. - $mo .= $originals_table; - - // Write translation strings. - $mo .= $translations_table; - - return $mo; - } -} diff --git a/src/PoGenerator.php b/src/PoGenerator.php deleted file mode 100644 index 488f922c..00000000 --- a/src/PoGenerator.php +++ /dev/null @@ -1,164 +0,0 @@ - $options - * @return string - */ - public static function toString( Translations $translations, array $options = [] ) { - $plural_form = $translations->getHeaders()->getPluralForm(); - $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : null; - - $lines = static::getHeaderLines( $translations ); - - foreach ( $translations as $translation ) { - /** @var Translation $translation */ - - $prefix = static::addMetaLines( $lines, $translation ); - - if ( null !== $translation->getPlural() && '' !== $translation->getPlural() ) { - static::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); - static::addLines( $lines, $prefix . 'msgstr[0]', (string) $translation->getTranslation() ); - - foreach ( $translation->getPluralTranslations( $plural_size ) as $k => $v ) { - static::addLines( $lines, $prefix . 'msgstr[' . ( $k + 1 ) . ']', $v ); - } - } else { - static::addLines( $lines, $prefix . 'msgstr', (string) $translation->getTranslation() ); - } - - $lines[] = ''; - } - - return implode( "\n", $lines ); - } - - /** - * Returns the lines for the header entry. - * - * @param Translations $translations Translations instance. - * @return array - */ - protected static function getHeaderLines( Translations $translations ) { - $lines = [ 'msgid ""', 'msgstr ""' ]; - - foreach ( $translations->getHeaders()->toArray() as $name => $value ) { - $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); - } - - $lines[] = ''; - - return $lines; - } - - /** - * Adds the comments, references, flags, context and original string of an entry. - * - * @param array &$lines Array lines should be added to. - * @param Translation $translation Translation entry. - * @return string Prefix to use for the remaining lines of the entry. - */ - protected static function addMetaLines( array &$lines, Translation $translation ) { - foreach ( $translation->getComments()->toArray() as $comment ) { - $lines[] = '# ' . $comment; - } - - foreach ( $translation->getExtractedComments()->toArray() as $comment ) { - $lines[] = '#. ' . $comment; - } - - foreach ( \WP_CLI\I18n\Translation::flattenReferences( $translation->getReferences() ) as $reference ) { - $lines[] = '#: ' . $reference[0] . ( null !== $reference[1] ? ':' . $reference[1] : '' ); - } - - if ( count( $translation->getFlags() ) > 0 ) { - $lines[] = '#, ' . implode( ',', $translation->getFlags()->toArray() ); - } - - $prefix = $translation->isDisabled() ? '#~ ' : ''; - - if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { - $lines[] = $prefix . 'msgctxt ' . static::convertString( $translation->getContext() ); - } - - static::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); - - return $prefix; - } - - /** - * Escapes and adds double quotes to a string. - * - * @param string $text Multiline string. - * - * @return string[] - */ - protected static function multilineQuote( $text ) { - $lines = explode( "\n", $text ); - $last = count( $lines ) - 1; - - foreach ( $lines as $k => $line ) { - if ( $k === $last ) { - $lines[ $k ] = static::convertString( $line ); - } else { - $lines[ $k ] = static::convertString( $line . "\n" ); - } - } - - return $lines; - } - - /** - * Add one or more lines depending whether the string is multiline or not. - * - * @param array &$lines Array lines should be added to. - * @param string $name Name of the line, e.g. msgstr or msgid_plural. - * @param string $value The line to add. - * @return void - */ - protected static function addLines( array &$lines, $name, $value ) { - $newlines = static::multilineQuote( $value ); - - if ( count( $newlines ) === 1 ) { - $lines[] = $name . ' ' . $newlines[0]; - } else { - $lines[] = $name . ' ""'; - - foreach ( $newlines as $line ) { - $lines[] = $line; - } - } - } - - /** - * Converts a string to its PO representation. - * - * @param string $value String to convert. - * @return string - */ - public static function convertString( $value ) { - return '"' . strtr( - $value, - [ - "\x00" => '', - '\\' => '\\\\', - "\t" => '\t', - "\r" => '\r', - "\n" => '\n', - '"' => '\\"', - ] - ) . '"'; - } -} diff --git a/src/PoLoader.php b/src/PoLoader.php deleted file mode 100644 index 4b5a38d4..00000000 --- a/src/PoLoader.php +++ /dev/null @@ -1,250 +0,0 @@ - $files A file path or a list of file paths. - * @return Translations - */ - public static function fromFiles( $files ): Translations { - $loader = new self(); - $translations = Translations::create(); - - foreach ( (array) $files as $file ) { - $loader->loadFile( $file, $translations ); - } - - return $translations; - } - - public function loadString( string $text, ?GettextTranslations $translations = null ): GettextTranslations { - $translations = parent::loadString( $text, $translations ); - - $lines = explode( "\n", $text ); - $count = count( $lines ); - $translation = Translation::create( null, '' ); - - for ( $i = 0; $i < $count; ++$i ) { - $line = trim( $lines[ $i ] ); - $line = self::fixMultiLines( $line, $lines, $i ); - - // Treat empty comments as empty lines. - if ( '#' === $line ) { - $line = ''; - } - - if ( '' === $line ) { - self::addTranslation( $translation, $translations ); - $translation = Translation::create( null, '' ); - continue; - } - - $split_line = preg_split( '/\s+/', $line, 2 ) ?: []; - $key = $split_line[0] ?? ''; - $data = $split_line[1] ?? ''; - - if ( '#~' === $key ) { - $translation->disable(); - - $split_line = preg_split( '/\s+/', $data, 2 ) ?: []; - $key = $split_line[0] ?? ''; - $data = $split_line[1] ?? ''; - } - - switch ( $key ) { - case '#': - $translation->getComments()->add( $data ); - break; - - case '#.': - $translation->getExtractedComments()->add( $data ); - break; - - case '#,': - foreach ( array_map( 'trim', explode( ',', trim( $data ) ) ) as $flag ) { - $translation->getFlags()->add( $flag ); - } - break; - - case '#:': - foreach ( preg_split( '/\s+/', trim( $data ) ) ?: [] as $reference ) { - if ( preg_match( '/^(.+)(:(\d*))?$/U', $reference, $matches ) ) { - $translation->getReferences()->add( - $matches[1], - isset( $matches[3] ) && '' !== $matches[3] ? (int) $matches[3] : null - ); - } - } - break; - - case 'msgctxt': - $translation = $translation->withContext( self::decode( $data ) ); - break; - - case 'msgid': - $translation = $translation->withOriginal( self::decode( $data ) ); - break; - - case 'msgid_plural': - $translation->setPlural( self::decode( $data ) ); - break; - - case 'msgstr': - case 'msgstr[0]': - $translation->translate( self::decode( $data ) ); - break; - - case 'msgstr[1]': - $translation->translatePlural( self::decode( $data ) ); - break; - - default: - if ( 0 === strpos( $key, 'msgstr[' ) ) { - $plural_translations = $translation->getPluralTranslations(); - $plural_translations[] = self::decode( $data ); - $translation->translatePlural( ...$plural_translations ); - } - break; - } - } - - self::addTranslation( $translation, $translations ); - - return $translations; - } - - protected function createTranslations(): GettextTranslations { - return Translations::create(); - } - - /** - * Adds a parsed entry to the translations. - * - * The entry with an empty original string and no context holds the headers. - * - * @param GettextTranslation $translation The parsed entry. - * @param GettextTranslations $translations Translations instance to add the entry to. - * @return void - */ - private static function addTranslation( GettextTranslation $translation, GettextTranslations $translations ) { - if ( '' !== $translation->getOriginal() ) { - $translations->addOrMerge( $translation ); - return; - } - - if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { - return; - } - - self::extractHeaders( (string) $translation->getTranslation(), $translations ); - } - - /** - * Adds the headers found in the header entry to the translations. - * - * @param string $headers Translation of the header entry. - * @param GettextTranslations $translations Translations instance. - * @return void - */ - private static function extractHeaders( $headers, GettextTranslations $translations ) { - $current_header = null; - - foreach ( explode( "\n", $headers ) as $line ) { - $line = self::decode( $line ); - - if ( '' === $line ) { - continue; - } - - // A header definition, as opposed to the continuation of the previous header. - if ( preg_match( '/^[\w-]+:/', $line ) ) { - list( $current_header, $value ) = array_map( 'trim', explode( ':', $line, 2 ) ); - $translations->getHeaders()->set( $current_header, $value ); - continue; - } - - if ( null !== $current_header ) { - $translations->getHeaders()->set( - $current_header, - $translations->getHeaders()->get( $current_header ) . $line - ); - } - } - } - - /** - * Gets one string from multiline strings. - * - * @param string $line Current line. - * @param array $lines All lines. - * @param int $i Index of the current line, moved forward to the last line that was consumed. - * @return string - */ - private static function fixMultiLines( $line, array $lines, &$i ) { - for ( $j = $i, $t = count( $lines ); $j < $t; ++$j ) { - if ( '"' === substr( $line, -1, 1 ) && isset( $lines[ $j + 1 ] ) ) { - $next_line = trim( $lines[ $j + 1 ] ); - - if ( '"' === substr( $next_line, 0, 1 ) ) { - $line = substr( $line, 0, -1 ) . substr( $next_line, 1 ); - continue; - } - - if ( '#~ "' === substr( $next_line, 0, 4 ) ) { - $line = substr( $line, 0, -1 ) . substr( $next_line, 4 ); - continue; - } - } - - $i = $j; - break; - } - - return $line; - } - - /** - * Converts a string from its PO representation. - * - * @param string $value String as found in a PO file. - * @return string - */ - public static function decode( $value ) { - if ( ! $value ) { - return ''; - } - - if ( '"' === $value[0] ) { - $value = substr( $value, 1, -1 ); - } - - return strtr( - $value, - [ - '\\\\' => '\\', - '\\a' => "\x07", - '\\b' => "\x08", - '\\t' => "\t", - '\\n' => "\n", - '\\v' => "\x0b", - '\\f' => "\x0c", - '\\r' => "\r", - '\\"' => '"', - ] - ); - } -} diff --git a/src/PotGenerator.php b/src/PotGenerator.php index 0a4f2fb0..dfc569c9 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -8,10 +8,9 @@ /** * POT file generator. * - * The only difference to the PO file generator is that this adds some - * comments at the very beginning of the file and leaves plural forms untranslated. + * Adds a comment at the very beginning of the file and leaves plural forms untranslated. */ -class PotGenerator extends PoGenerator { +class PotGenerator extends Generator { /** * @var array */ @@ -68,4 +67,121 @@ public static function toString( Translations $translations, array $options = [] return implode( "\n", $lines ); } + + /** + * Returns the lines for the header entry. + * + * @param Translations $translations Translations instance. + * @return array + */ + protected static function getHeaderLines( Translations $translations ) { + $lines = [ 'msgid ""', 'msgstr ""' ]; + + foreach ( $translations->getHeaders()->toArray() as $name => $value ) { + $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); + } + + $lines[] = ''; + + return $lines; + } + + /** + * Adds the comments, references, flags, context and original string of an entry. + * + * @param array &$lines Array lines should be added to. + * @param Translation $translation Translation entry. + * @return string Prefix to use for the remaining lines of the entry. + */ + protected static function addMetaLines( array &$lines, Translation $translation ) { + foreach ( $translation->getComments()->toArray() as $comment ) { + $lines[] = '# ' . $comment; + } + + foreach ( $translation->getExtractedComments()->toArray() as $comment ) { + $lines[] = '#. ' . $comment; + } + + foreach ( \WP_CLI\I18n\Translations::getReferenceList( $translation ) as $reference ) { + $lines[] = '#: ' . $reference[0] . ( null !== $reference[1] ? ':' . $reference[1] : '' ); + } + + if ( count( $translation->getFlags() ) > 0 ) { + $lines[] = '#, ' . implode( ',', $translation->getFlags()->toArray() ); + } + + $prefix = $translation->isDisabled() ? '#~ ' : ''; + + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { + $lines[] = $prefix . 'msgctxt ' . static::convertString( $translation->getContext() ); + } + + static::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); + + return $prefix; + } + + /** + * Escapes and adds double quotes to a string. + * + * @param string $text Multiline string. + * + * @return string[] + */ + protected static function multilineQuote( $text ) { + $lines = explode( "\n", $text ); + $last = count( $lines ) - 1; + + foreach ( $lines as $k => $line ) { + if ( $k === $last ) { + $lines[ $k ] = static::convertString( $line ); + } else { + $lines[ $k ] = static::convertString( $line . "\n" ); + } + } + + return $lines; + } + + /** + * Add one or more lines depending whether the string is multiline or not. + * + * @param array &$lines Array lines should be added to. + * @param string $name Name of the line, e.g. msgstr or msgid_plural. + * @param string $value The line to add. + * @return void + */ + protected static function addLines( array &$lines, $name, $value ) { + $newlines = static::multilineQuote( $value ); + + if ( count( $newlines ) === 1 ) { + $lines[] = $name . ' ' . $newlines[0]; + } else { + $lines[] = $name . ' ""'; + + foreach ( $newlines as $line ) { + $lines[] = $line; + } + } + } + + /** + * Converts a string to its PO representation. + * + * @param string $value String to convert. + * @return string + */ + public static function convertString( $value ) { + return '"' . strtr( + $value, + [ + "\x00" => '', + '\\' => '\\\\', + "\t" => '\t', + "\r" => '\r', + "\n" => '\n', + '"' => '\\"', + ] + ) . '"'; + } } diff --git a/src/Translation.php b/src/Translation.php deleted file mode 100644 index 6efb4055..00000000 --- a/src/Translation.php +++ /dev/null @@ -1,109 +0,0 @@ -context && '' !== $this->context; - } - - public function hasPlural(): bool { - return null !== $this->plural && '' !== $this->plural; - } - - public function hasTranslation(): bool { - return null !== $this->translation && '' !== $this->translation; - } - - /** - * @param bool $check_content Whether to check that at least one plural translation is not empty. - */ - public function hasPluralTranslations( bool $check_content = false ): bool { - if ( $check_content ) { - return '' !== implode( '', $this->getPluralTranslations() ); - } - - return ! empty( $this->getPluralTranslations() ); - } - - /** - * Returns the references as a flat list. - * - * @return array List of file name and line number pairs. - */ - public function getReferenceEntries(): array { - return self::flattenReferences( $this->getReferences() ); - } - - /** - * Turns references grouped by file name into a flat list. - * - * @param References $references References instance. - * @return array List of file name and line number pairs. - */ - public static function flattenReferences( References $references ): array { - $entries = []; - - foreach ( $references->toArray() as $filename => $lines ) { - if ( empty( $lines ) ) { - $entries[] = [ (string) $filename, null ]; - continue; - } - - foreach ( $lines as $line ) { - $entries[] = [ (string) $filename, $line ]; - } - } - - return $entries; - } - - /** - * {@inheritdoc} - * - * Unlike the parent method, this keeps translations and plurals that are "0", - * and does not change whether the translation is disabled. - */ - public function mergeWith( GettextTranslation $translation, int $strategy = 0 ): GettextTranslation { - /** @var Translation $merged */ - $merged = parent::mergeWith( $translation, $strategy ); - $override = (bool) ( $strategy & Merge::TRANSLATIONS_OVERRIDE ); - - $their_translation = $translation->getTranslation(); - $their_plural = $translation->getPlural(); - - $merged->translation = $this->translation; - if ( ! $this->hasTranslation() || ( $override && null !== $their_translation && '' !== $their_translation ) ) { - $merged->translation = $their_translation; - } - - $merged->plural = $this->plural; - if ( ! $this->hasPlural() || ( $override && null !== $their_plural && '' !== $their_plural ) ) { - $merged->plural = $their_plural; - } - - $merged->disabled = $this->disabled; - - return $merged; - } -} diff --git a/src/Translations.php b/src/Translations.php index 43fb6c99..c892e425 100644 --- a/src/Translations.php +++ b/src/Translations.php @@ -2,6 +2,8 @@ namespace WP_CLI\I18n; +use Gettext\Loader\PoLoader; +use Gettext\Translation; use Gettext\Translations as GettextTranslations; /** @@ -44,6 +46,62 @@ public static function create( ?string $domain = null, ?string $language = null return $translations; } + /** + * Loads one or more PO or POT files into a single set of translations. + * + * Entries that appear in more than one file are merged. + * + * @param string|array $files A file path or a list of file paths. + * @return static + */ + public static function fromPoFiles( $files ): GettextTranslations { + $translations = null; + + foreach ( (array) $files as $file ) { + $loaded = static::fromPoString( (string) file_get_contents( $file ) ); + + /** @var static $translations */ + $translations = null === $translations ? $loaded : $translations->mergeWith( $loaded ); + } + + return $translations ?? static::create(); + } + + /** + * Loads translations from the contents of a PO or POT file. + * + * Unlike the loader of gettext/gettext, this merges entries that appear more than once + * instead of keeping only the last one. + * + * @param string $content File contents. + * @return static + */ + public static function fromPoString( string $content ): GettextTranslations { + $loader = new PoLoader(); + + /** @var static $translations */ + $translations = $loader->loadString( $content, static::create() ); + + // One msgid line belongs to the header entry. + $entries = (int) preg_match_all( '/^(#~\s*)?msgid\s/m', $content ) - 1; + + if ( $entries <= count( $translations ) ) { + return $translations; + } + + foreach ( $translations->getTranslations() as $translation ) { + $translations->remove( $translation ); + } + + foreach ( preg_split( '/\R\s*\R/', $content ) ?: [] as $block ) { + foreach ( $loader->loadString( $block, static::create() ) as $translation ) { + $translations->addOrMerge( $translation ); + } + } + + return $translations; + } + /** * Returns the translation with the given context and original string, creating it if needed. * @@ -53,27 +111,50 @@ public static function create( ?string $domain = null, ?string $language = null * @return Translation The existing or newly added translation. */ public function insert( ?string $context, string $original, ?string $plural = null ): Translation { - $existing = $this->find( $context, $original ); + $translation = $this->find( $context, $original ); - if ( $existing instanceof Translation ) { - $translation = $existing; - } else { + if ( null === $translation ) { $translation = Translation::create( $context, $original ); - - // Keep the data of an entry that was added as a plain gettext translation. - if ( null !== $existing ) { - /** @var Translation $translation */ - $translation = $translation->mergeWith( $existing ); - $translation->disable( $existing->isDisabled() ); - } - $this->add( $translation ); } - if ( null !== $plural && '' !== $plural && ! $translation->hasPlural() ) { + if ( null !== $plural && '' !== $plural && ! self::hasValue( $translation->getPlural() ) ) { $translation->setPlural( $plural ); } return $translation; } + + /** + * Returns the references of a translation as a flat list. + * + * @param Translation $translation Translation entry. + * @return array List of file name and line number pairs. + */ + public static function getReferenceList( Translation $translation ): array { + $entries = []; + + foreach ( $translation->getReferences()->toArray() as $filename => $lines ) { + if ( empty( $lines ) ) { + $entries[] = [ (string) $filename, null ]; + continue; + } + + foreach ( $lines as $line ) { + $entries[] = [ (string) $filename, $line ]; + } + } + + return $entries; + } + + /** + * Checks whether a string is set and not empty. Unlike a truthiness check, "0" is a valid value. + * + * @param string|null $value Value to check. + * @return bool + */ + public static function hasValue( ?string $value ): bool { + return null !== $value && '' !== $value; + } } diff --git a/src/UpdatePoCommand.php b/src/UpdatePoCommand.php index e982b84c..e9087e7f 100644 --- a/src/UpdatePoCommand.php +++ b/src/UpdatePoCommand.php @@ -3,8 +3,10 @@ namespace WP_CLI\I18n; use DirectoryIterator; +use Gettext\Generator\PoGenerator; use Gettext\Headers; use Gettext\Merge; +use Gettext\Translation; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -82,7 +84,7 @@ public function __invoke( $args, $assoc_args ) { $files = new IteratorIterator( new DirectoryIterator( $destination ) ); } - $pot_translations = PoLoader::fromFiles( $source ); + $pot_translations = Translations::fromPoFiles( $source ); /** @var array $assoc_args */ $purge = (bool) Utils\get_flag_value( $assoc_args, 'purge', true ); @@ -100,19 +102,14 @@ public function __invoke( $args, $assoc_args ) { continue; } - // Preserve file-level comments when --no-purge is set - $file_comments = ''; - if ( ! $purge ) { - $file_comments = $this->extract_file_comments( $file->getPathname() ); - } - $original_translations = PoLoader::fromFiles( $file->getPathname() ); + $original_translations = Translations::fromPoFiles( $file->getPathname() ); $po_translations = $this->merge_translations( $original_translations, $pot_translations, $purge ); // Check if the translations actually changed by comparing the objects. $has_changes = $this->translations_differ( $original_translations, $po_translations ); - // When using --no-purge, file-level comments being restored counts as a change. - if ( ! $purge && ! empty( $file_comments ) ) { + // When using --no-purge, file-level comments being kept counts as a change. + if ( ! $purge && null !== $original_translations->getDescription() ) { $has_changes = true; } @@ -123,18 +120,11 @@ public function __invoke( $args, $assoc_args ) { // Reorder translations to match POT file order. $ordered_translations = $this->reorder_translations( $po_translations, $pot_translations ); - if ( ! PoGenerator::toFile( $ordered_translations, $file->getPathname() ) ) { + if ( ! ( new PoGenerator() )->generateFile( $ordered_translations, $file->getPathname() ) ) { WP_CLI::warning( sprintf( 'Could not update file %s', $file->getPathname() ) ); continue; } - // Restore file-level comments when --no-purge is set - if ( ! $purge && ! empty( $file_comments ) ) { - if ( ! $this->restore_file_comments( $file->getPathname(), $file_comments ) ) { - WP_CLI::warning( sprintf( 'Could not restore file-level comments for %s', $file->getPathname() ) ); - } - } - if ( $has_changes ) { ++$updated_count; } else { @@ -152,74 +142,6 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::success( implode( '. ', $message_parts ) . '.' ); } - /** - * Extract file-level comments from a PO file. - * - * These are comments that appear before the first msgid in the file. - * - * @param string $file_path Path to the PO file. - * @return string The file-level comments. - */ - private function extract_file_comments( $file_path ) { - $content = file_get_contents( $file_path ); - if ( false === $content ) { - return ''; - } - - $lines = explode( "\n", $content ); - $file_comments = []; - $found_msgid = false; - - foreach ( $lines as $line ) { - $trimmed = trim( $line ); - - // Stop when we hit the first msgid - if ( preg_match( '/^msgid\s/', $trimmed ) ) { - $found_msgid = true; - break; - } - - // Collect comment lines - if ( preg_match( '/^#([^.,:~]|$)/', $trimmed ) ) { - $file_comments[] = $line; - } - } - - return ! empty( $file_comments ) ? implode( "\n", $file_comments ) . "\n" : ''; - } - - /** - * Restore file-level comments to a PO file. - * - * @param string $file_path Path to the PO file. - * @param string $comments The file-level comments to restore. - * @return bool True on success, false on failure. - */ - private function restore_file_comments( $file_path, $comments ) { - $content = file_get_contents( $file_path ); - if ( false === $content ) { - return false; - } - - // Prepend the comments to the file content - $updated_content = $comments . $content; - - // Use atomic file operation with temporary file - $temp_file = $file_path . '.tmp'; - if ( false === file_put_contents( $temp_file, $updated_content ) ) { - return false; - } - - // Rename is atomic on most filesystems - if ( ! rename( $temp_file, $file_path ) ) { - // Clean up temp file on failure - unlink( $temp_file ); - return false; - } - - return true; - } - /** * Merges the entries of a POT file into the translations of a PO file. * @@ -242,6 +164,9 @@ private function merge_translations( Translations $po_translations, Translations if ( $purge ) { $strategy |= Merge::COMMENTS_THEIRS; + // File-level comments, like a copyright notice. + $merged->setDescription( null ); + foreach ( $merged->getTranslations() as $po_entry ) { if ( ! $pot_translations->has( $po_entry ) ) { $merged->remove( $po_entry ); @@ -298,8 +223,8 @@ private function translations_differ( Translations $original, Translations $upda } // Compare references (source code locations). - $original_refs = Translation::flattenReferences( $translation->getReferences() ); - $updated_refs = Translation::flattenReferences( $updated_translation->getReferences() ); + $original_refs = Translations::getReferenceList( $translation ); + $updated_refs = Translations::getReferenceList( $updated_translation ); $original_refs_sorted = $original_refs; $updated_refs_sorted = $updated_refs; @@ -335,11 +260,14 @@ private function translations_differ( Translations $original, Translations $upda private function reorder_translations( Translations $po_translations, Translations $pot_translations ) { $ordered = Translations::create(); - // Copy headers from the merged PO translations. + // Copy headers, file-level comments and flags from the merged PO translations. foreach ( $po_translations->getHeaders()->toArray() as $name => $value ) { $ordered->getHeaders()->set( $name, $value ); } + $ordered->setDescription( $po_translations->getDescription() ); + $ordered->getFlags()->add( ...$po_translations->getFlags()->toArray() ); + // Add translations in POT file order. foreach ( $pot_translations as $pot_entry ) { /** @var Translation $pot_entry */ diff --git a/tests/PhpFunctionsScannerTest.php b/tests/PhpFunctionsScannerTest.php index a0d51a5e..98b8d787 100644 --- a/tests/PhpFunctionsScannerTest.php +++ b/tests/PhpFunctionsScannerTest.php @@ -4,7 +4,7 @@ use WP_CLI\I18n\PhpCodeExtractor; use WP_CLI\I18n\PhpFunctionsScanner; -use WP_CLI\I18n\Translation; +use Gettext\Translation; use WP_CLI\I18n\Translations; use WP_CLI\Tests\TestCase; @@ -101,6 +101,6 @@ public function test_extracts_strings_for_text_domain() { $this->assertSame( '%d items', $translation->getPlural() ); $this->assertSame( [ 'php-format' ], $translation->getFlags()->toArray() ); $this->assertSame( [ 'translators: %d: Number of items.' ], $translation->getExtractedComments()->toArray() ); - $this->assertSame( [ [ 'foo-plugin.php', 3 ] ], $translation->getReferenceEntries() ); + $this->assertSame( [ [ 'foo-plugin.php', 3 ] ], Translations::getReferenceList( $translation ) ); } } diff --git a/tests/PoLoaderTest.php b/tests/PoLoaderTest.php deleted file mode 100644 index 66f020ba..00000000 --- a/tests/PoLoaderTest.php +++ /dev/null @@ -1,191 +0,0 @@ -loadString( $po ); - - $this->assertInstanceOf( Translations::class, $translations ); - - return $translations; - } - - public function test_keeps_strings_and_translations_that_are_zero() { - $po = <<<'PO' -msgid "" -msgstr "" -"Language: de\n" - -msgid "0" -msgstr "" - -msgctxt "start of week" -msgid "1" -msgstr "0" - -msgid "%d item" -msgid_plural "%d items" -msgstr[0] "0" -msgstr[1] "0" - -PO; - - $translations = $this->load( $po ); - - $this->assertCount( 3, $translations ); - $this->assertNotNull( $translations->find( null, '0' ) ); - - $translation = $translations->find( 'start of week', '1' ); - $this->assertInstanceOf( Translation::class, $translation ); - $this->assertSame( '0', $translation->getTranslation() ); - - $result = PoGenerator::toString( $translations ); - - $this->assertStringContainsString( "msgid \"0\"\nmsgstr \"\"\n", $result ); - $this->assertStringContainsString( "msgid \"1\"\nmsgstr \"0\"\n", $result ); - $this->assertStringContainsString( "msgstr[0] \"0\"\nmsgstr[1] \"0\"\n", $result ); - } - - public function test_sorts_headers_and_flags_and_groups_references() { - $po = <<<'PO' -msgid "" -msgstr "" -"X-Generator: Poedit\n" -"Language: de\n" -"Plural-Forms: nplurals=2; plural=(n != 1);\n" -"A-Custom-Header: multi " -"line\n" - -#: b.php:2 a.php:1 -#: b.php:1 -#: c.php -#, php-format, fuzzy,js-format -msgid "Hello %s" -msgstr "Hello %s (DE)" - -PO; - - $translations = $this->load( $po ); - - $headers = array_keys( $translations->getHeaders()->toArray() ); - - $this->assertSame( 'A-Custom-Header', $headers[0] ); - $this->assertSame( 'X-Generator', end( $headers ) ); - $this->assertSame( 'de', $translations->getLanguage() ); - $this->assertSame( 'multi line', $translations->getHeaders()->get( 'A-Custom-Header' ) ); - - $translation = $translations->find( null, 'Hello %s' ); - $this->assertInstanceOf( Translation::class, $translation ); - - $this->assertSame( [ 'fuzzy', 'js-format', 'php-format' ], $translation->getFlags()->toArray() ); - $this->assertSame( - [ - [ 'b.php', 2 ], - [ 'b.php', 1 ], - [ 'a.php', 1 ], - [ 'c.php', null ], - ], - $translation->getReferenceEntries() - ); - - $result = PoGenerator::toString( $translations ); - - $this->assertStringContainsString( "#: b.php:2\n#: b.php:1\n#: a.php:1\n#: c.php\n#, fuzzy,js-format,php-format\n", $result ); - } - - public function test_merges_duplicate_entries() { - $po = <<<'PO' -#: a.php:1 -msgid "Hello" -msgstr "" - -#. A comment -#: b.php:2 -msgid "Hello" -msgstr "Hello (DE)" - -PO; - - $translations = $this->load( $po ); - - $this->assertCount( 1, $translations ); - - $translation = $translations->find( null, 'Hello' ); - $this->assertInstanceOf( Translation::class, $translation ); - - $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); - $this->assertSame( [ 'A comment' ], $translation->getExtractedComments()->toArray() ); - $this->assertCount( 2, $translation->getReferenceEntries() ); - } - - public function test_loads_multiline_and_disabled_entries() { - $po = <<<'PO' -msgctxt "context" -msgid "" -"First\n" -"second \"quoted\"" -msgstr "" -"First (DE)\n" -"second (DE)" - -#~ msgid "Obsolete " -#~ "string" -#~ msgstr "Obsolete (DE)" -PO; - - $translations = $this->load( $po ); - - $translation = $translations->find( 'context', "First\nsecond \"quoted\"" ); - $this->assertInstanceOf( Translation::class, $translation ); - $this->assertSame( "First (DE)\nsecond (DE)", $translation->getTranslation() ); - $this->assertFalse( $translation->isDisabled() ); - - $translation = $translations->find( null, 'Obsolete string' ); - $this->assertInstanceOf( Translation::class, $translation ); - $this->assertSame( 'Obsolete (DE)', $translation->getTranslation() ); - $this->assertTrue( $translation->isDisabled() ); - - $result = PoGenerator::toString( $translations ); - - $this->assertStringContainsString( "msgctxt \"context\"\nmsgid \"\"\n\"First\\n\"\n\"second \\\"quoted\\\"\"\n", $result ); - $this->assertStringContainsString( "#~ msgid \"Obsolete string\"\n#~ msgstr \"Obsolete (DE)\"\n", $result ); - } - - public function test_loads_multiple_files_into_one_set() { - $dir = sys_get_temp_dir() . '/' . uniqid( 'wp-cli-i18n-test-', true ); - mkdir( $dir ); - - file_put_contents( "$dir/a.pot", "#: a.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n" ); - file_put_contents( "$dir/b.pot", "#: b.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n\nmsgid \"World\"\nmsgstr \"\"\n" ); - - $translations = PoLoader::fromFiles( [ "$dir/a.pot", "$dir/b.pot" ] ); - - unlink( "$dir/a.pot" ); - unlink( "$dir/b.pot" ); - rmdir( $dir ); - - $this->assertCount( 2, $translations ); - - $translation = $translations->find( null, 'Hello' ); - $this->assertInstanceOf( Translation::class, $translation ); - $this->assertSame( - [ - [ 'a.php', 1 ], - [ 'b.php', 1 ], - ], - $translation->getReferenceEntries() - ); - } -} diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php deleted file mode 100644 index 763f0018..00000000 --- a/tests/TranslationTest.php +++ /dev/null @@ -1,116 +0,0 @@ -translate( '0' ); - $theirs = Translation::create( null, '1' ); - - $this->assertSame( '0', $ours->mergeWith( $theirs )->getTranslation() ); - $this->assertSame( '0', $theirs->mergeWith( $ours )->getTranslation() ); - - $theirs->translate( '7' ); - - $this->assertSame( '0', $ours->mergeWith( $theirs )->getTranslation() ); - $this->assertSame( '7', $ours->mergeWith( $theirs, Merge::TRANSLATIONS_OVERRIDE )->getTranslation() ); - $this->assertSame( '0', $theirs->mergeWith( $ours, Merge::TRANSLATIONS_OVERRIDE )->getTranslation() ); - } - - public function test_merge_keeps_disabled_state() { - $ours = Translation::create( null, 'Hello' )->disable(); - $theirs = Translation::create( null, 'Hello' ); - - $this->assertTrue( $ours->mergeWith( $theirs )->isDisabled() ); - $this->assertFalse( $theirs->mergeWith( $ours )->isDisabled() ); - } - - public function test_insert_returns_existing_translation() { - $translations = Translations::create(); - - $first = $translations->insert( 'context', 'Hello' ); - $first->getReferences()->add( 'a.php', 1 ); - - $second = $translations->insert( 'context', 'Hello', 'Hellos' ); - - $this->assertSame( $first, $second ); - $this->assertCount( 1, $translations ); - $this->assertSame( 'Hellos', $first->getPlural() ); - $this->assertTrue( $first->hasPlural() ); - $this->assertTrue( $first->hasContext() ); - $this->assertFalse( $first->hasTranslation() ); - } - - public function test_insert_keeps_data_of_plain_gettext_translation() { - $translations = Translations::create(); - - $plain = \Gettext\Translation::create( null, 'Hello' )->translate( 'Hello (DE)' )->disable(); - $plain->getReferences()->add( 'a.php', 1 ); - $plain->getFlags()->add( 'fuzzy' ); - $translations->add( $plain ); - - $translation = $translations->insert( null, 'Hello', 'Hellos' ); - - $this->assertCount( 1, $translations ); - $this->assertSame( $translation, $translations->find( null, 'Hello' ) ); - $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); - $this->assertSame( 'Hellos', $translation->getPlural() ); - $this->assertTrue( $translation->isDisabled() ); - $this->assertSame( [ 'fuzzy' ], $translation->getFlags()->toArray() ); - $this->assertSame( [ [ 'a.php', 1 ] ], $translation->getReferenceEntries() ); - } - - public function test_translations_have_default_headers() { - $translations = Translations::create( 'foo-plugin' ); - - $this->assertSame( - [ - 'Content-Transfer-Encoding', - 'Content-Type', - 'Language', - 'Language-Team', - 'Last-Translator', - 'MIME-Version', - 'PO-Revision-Date', - 'POT-Creation-Date', - 'Project-Id-Version', - 'Report-Msgid-Bugs-To', - 'X-Domain', - ], - array_keys( $translations->getHeaders()->toArray() ) - ); - } - - public function test_mo_file_includes_headers_and_translations_that_are_zero() { - $translations = Translations::create(); - $translations->getHeaders()->set( 'Plural-Forms', 'nplurals=2; plural=(n != 1);' ); - - $translations->insert( 'start of week', '1' )->translate( '0' ); - $translations->insert( null, 'Untranslated' ); - $translations->insert( null, 'Disabled' )->translate( 'Disabled (DE)' )->disable(); - $translations->insert( null, '%d item', '%d items' )->translate( '%d item (DE)' )->translatePlural( '%d items (DE)' ); - - $mo = MoGenerator::toString( $translations ); - - $this->assertSame( 0x950412de, current( (array) unpack( 'L', $mo ) ) ); - $this->assertSame( 3, current( (array) unpack( 'L', substr( $mo, 8 ) ) ) ); - $this->assertStringContainsString( "Plural-Forms: nplurals=2; plural=(n != 1);\n", $mo ); - $this->assertStringContainsString( "start of week\x041\x00", $mo ); - $this->assertStringContainsString( "%d item\x00%d items\x00", $mo ); - $this->assertStringContainsString( "%d item (DE)\x00%d items (DE)\x00", $mo ); - - $loaded = ( new \Gettext\Loader\MoLoader() )->loadString( $mo )->find( 'start of week', '1' ); - $this->assertNotNull( $loaded ); - $this->assertSame( '0', $loaded->getTranslation() ); - - $this->assertStringNotContainsString( 'Untranslated', $mo ); - $this->assertStringNotContainsString( 'Disabled', $mo ); - } -} diff --git a/tests/TranslationsTest.php b/tests/TranslationsTest.php new file mode 100644 index 00000000..1ee9edec --- /dev/null +++ b/tests/TranslationsTest.php @@ -0,0 +1,207 @@ +dir = sys_get_temp_dir() . '/' . uniqid( 'wp-cli-i18n-test-', true ); + mkdir( $this->dir ); + } + + public function tear_down() { + array_map( 'unlink', (array) glob( $this->dir . '/*' ) ); + rmdir( $this->dir ); + + parent::tear_down(); + } + + public function test_loads_strings_and_translations_that_are_zero() { + file_put_contents( + "$this->dir/de_DE.po", + <<<'PO' +msgid "" +msgstr "" +"Language: de\n" + +msgid "0" +msgstr "" + +msgctxt "start of week" +msgid "1" +msgstr "0" + +PO + ); + + $translations = Translations::fromPoFiles( "$this->dir/de_DE.po" ); + + $this->assertInstanceOf( Translations::class, $translations ); + $this->assertCount( 2, $translations ); + $this->assertNotNull( $translations->find( null, '0' ) ); + + $translation = $translations->find( 'start of week', '1' ); + $this->assertNotNull( $translation ); + $this->assertSame( '0', $translation->getTranslation() ); + } + + public function test_loads_multiple_files_into_one_set() { + file_put_contents( "$this->dir/a.pot", "#: a.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n" ); + file_put_contents( "$this->dir/b.pot", "#: b.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n\nmsgid \"World\"\nmsgstr \"\"\n" ); + + $translations = Translations::fromPoFiles( [ "$this->dir/a.pot", "$this->dir/b.pot" ] ); + + $this->assertCount( 2, $translations ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertNotNull( $translation ); + $this->assertSame( + [ + [ 'a.php', 1 ], + [ 'b.php', 1 ], + ], + Translations::getReferenceList( $translation ) + ); + } + + public function test_merges_duplicate_entries() { + $translations = Translations::fromPoString( + <<<'PO' +msgid "" +msgstr "" +"Language: de\n" + +#: a.php:1 +msgid "Hello" +msgstr "" + +#. A comment +#: b.php:2 +msgid "Hello" +msgstr "Hello (DE)" + +msgid "World" +msgstr "World (DE)" + +PO + ); + + $this->assertCount( 2, $translations ); + $this->assertSame( 'de', $translations->getLanguage() ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertNotNull( $translation ); + $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); + $this->assertSame( [ 'A comment' ], $translation->getExtractedComments()->toArray() ); + $this->assertSame( + [ + [ 'a.php', 1 ], + [ 'b.php', 2 ], + ], + Translations::getReferenceList( $translation ) + ); + } + + public function test_keeps_file_comments_and_flags() { + file_put_contents( + "$this->dir/de_DE.po", + <<<'PO' +# Copyright (C) 2026 Foo Plugin +# +#, fuzzy +msgid "" +msgstr "" +"Language: de\n" + +#, php-format +msgid "Hello %s" +msgstr "Hello %s (DE)" + +PO + ); + + $translations = Translations::fromPoFiles( "$this->dir/de_DE.po" ); + + $this->assertSame( 'Copyright (C) 2026 Foo Plugin', $translations->getDescription() ); + $this->assertSame( [ 'fuzzy' ], $translations->getFlags()->toArray() ); + + $result = ( new PoGenerator() )->generateString( $translations ); + + $this->assertStringStartsWith( "# Copyright (C) 2026 Foo Plugin\n#\n#, fuzzy\nmsgid \"\"\n", $result ); + } + + public function test_insert_returns_existing_translation() { + $translations = Translations::create(); + + $first = $translations->insert( 'context', 'Hello' ); + $first->getReferences()->add( 'a.php', 1 ); + + $second = $translations->insert( 'context', 'Hello', 'Hellos' ); + + $this->assertSame( $first, $second ); + $this->assertCount( 1, $translations ); + $this->assertSame( 'Hellos', $first->getPlural() ); + $this->assertFalse( $first->isTranslated() ); + $this->assertSame( [ [ 'a.php', 1 ] ], Translations::getReferenceList( $first ) ); + } + + public function test_translations_have_default_headers() { + $translations = Translations::create( 'foo-plugin' ); + + $this->assertSame( + [ + 'Content-Transfer-Encoding', + 'Content-Type', + 'Language', + 'Language-Team', + 'Last-Translator', + 'MIME-Version', + 'PO-Revision-Date', + 'POT-Creation-Date', + 'Project-Id-Version', + 'Report-Msgid-Bugs-To', + 'X-Domain', + ], + array_keys( $translations->getHeaders()->toArray() ) + ); + } + + public function test_mo_file_includes_headers_and_translations_that_are_zero() { + $translations = Translations::create(); + $translations->getHeaders()->set( 'Plural-Forms', 'nplurals=2; plural=(n != 1);' ); + + $translations->insert( 'start of week', '1' )->translate( '0' ); + $translations->insert( null, 'Untranslated' ); + $translations->insert( null, 'Disabled' )->translate( 'Disabled (DE)' )->disable(); + $translations->insert( null, '%d item', '%d items' )->translate( '%d item (DE)' )->translatePlural( '%d items (DE)' ); + + $mo = ( new MoGenerator() )->includeHeaders()->generateString( $translations ); + + $this->assertSame( 0x950412de, current( (array) unpack( 'L', $mo ) ) ); + $this->assertSame( 3, current( (array) unpack( 'L', substr( $mo, 8 ) ) ) ); + $this->assertStringContainsString( "Plural-Forms: nplurals=2; plural=(n != 1);\n", $mo ); + $this->assertStringContainsString( "start of week\x041\x00", $mo ); + $this->assertStringContainsString( "%d item\x00%d items\x00", $mo ); + $this->assertStringContainsString( "%d item (DE)\x00%d items (DE)\x00", $mo ); + + $loaded = ( new MoLoader() )->loadString( $mo )->find( 'start of week', '1' ); + $this->assertNotNull( $loaded ); + $this->assertSame( '0', $loaded->getTranslation() ); + + $this->assertStringNotContainsString( 'Untranslated', $mo ); + $this->assertStringNotContainsString( 'Disabled', $mo ); + } +} From 1c2ecb425555dcfa07b36b8d37d67e49feafb954 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 20 Nov 2026 21:43:25 +0000 Subject: [PATCH 08/14] Fix PHPStan error when merging duplicate entries --- src/Translations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Translations.php b/src/Translations.php index c892e425..4796e2f3 100644 --- a/src/Translations.php +++ b/src/Translations.php @@ -94,7 +94,7 @@ public static function fromPoString( string $content ): GettextTranslations { } foreach ( preg_split( '/\R\s*\R/', $content ) ?: [] as $block ) { - foreach ( $loader->loadString( $block, static::create() ) as $translation ) { + foreach ( $loader->loadString( $block, static::create() )->getTranslations() as $translation ) { $translations->addOrMerge( $translation ); } } From 64da93274c50739b5a24d4529b01a1b601e55bd5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 20 Nov 2026 21:46:32 +0000 Subject: [PATCH 09/14] Mark removed strings as obsolete with `update-po --no-purge` Entries that are no longer in the POT file were kept as active entries, although the option is documented to keep them marked with `#~`. --- features/updatepo.feature | 54 +++++++++++++++++++++++++++++++++++++++ src/UpdatePoCommand.php | 12 +++++++++ 2 files changed, 66 insertions(+) diff --git a/features/updatepo.feature b/features/updatepo.feature index 2aa89990..fc886af5 100644 --- a/features/updatepo.feature +++ b/features/updatepo.feature @@ -1040,3 +1040,57 @@ Feature: Update existing PO files from a POT file """ #~ msgid "Some string" """ + + Scenario: Marks translations that are no longer in the POT file as obsolete with --no-purge + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.pot file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:1 + msgid "Some string" + msgstr "" + """ + And a foo-plugin/foo-plugin-de_DE.po file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "Language: de_DE\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:1 + msgid "Some string" + msgstr "Some translated string" + + #: foo-plugin.php:2 + msgid "Removed string" + msgstr "Removed translated string" + """ + + When I run `wp i18n update-po foo-plugin/foo-plugin.pot --no-purge` + Then STDOUT should be: + """ + Success: Updated 1 file. + """ + And STDERR should be empty + And the foo-plugin/foo-plugin-de_DE.po file should contain: + """ + #: foo-plugin.php:1 + msgid "Some string" + msgstr "Some translated string" + """ + And the foo-plugin/foo-plugin-de_DE.po file should contain: + """ + #~ msgid "Removed string" + #~ msgstr "Removed translated string" + """ + + When I run `wp i18n update-po foo-plugin/foo-plugin.pot --no-purge` + Then STDOUT should be: + """ + Success: Updated 0 files. 1 file unchanged. + """ diff --git a/src/UpdatePoCommand.php b/src/UpdatePoCommand.php index e9087e7f..28638737 100644 --- a/src/UpdatePoCommand.php +++ b/src/UpdatePoCommand.php @@ -172,6 +172,13 @@ private function merge_translations( Translations $po_translations, Translations $merged->remove( $po_entry ); } } + } else { + // Keep entries that are no longer in the POT file, but mark them as obsolete. + foreach ( $merged->getTranslations() as $po_entry ) { + if ( ! $pot_translations->has( $po_entry ) ) { + $po_entry->disable(); + } + } } foreach ( $pot_translations as $pot_entry ) { @@ -214,6 +221,11 @@ private function translations_differ( Translations $original, Translations $upda return true; } + // Compare whether the entries are obsolete. + if ( $translation->isDisabled() !== $updated_translation->isDisabled() ) { + return true; + } + // Compare plural translations if they exist. $original_plurals = $translation->getPluralTranslations(); $updated_plurals = $updated_translation->getPluralTranslations(); From 5ed4318d37a56bb3ca84fe95719c66c5ef9d1be7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 20 Nov 2026 21:58:03 +0000 Subject: [PATCH 10/14] Harden PO loading and writing in `update-po` * Stop when the source POT file cannot be read instead of treating it as empty, which would remove all entries from the PO files. * Prefix every line of a multiline obsolete entry with `#~`. gettext/gettext writes the continuation lines without it. * When merging duplicate entries, also treat a `#` line after an entry as its end, as the PO loader does. --- features/updatepo.feature | 66 ++++++++++++++++++++++++++++++++++++++ src/Translations.php | 45 ++++++++++++++++++++++++-- src/UpdatePoCommand.php | 32 +++++++++++++++++- tests/TranslationsTest.php | 39 ++++++++++++++++++++++ 4 files changed, 179 insertions(+), 3 deletions(-) diff --git a/features/updatepo.feature b/features/updatepo.feature index fc886af5..8e2b966d 100644 --- a/features/updatepo.feature +++ b/features/updatepo.feature @@ -1094,3 +1094,69 @@ Feature: Update existing PO files from a POT file """ Success: Updated 0 files. 1 file unchanged. """ + + Scenario: Marks multiline translations that are no longer in the POT file as obsolete with --no-purge + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.pot file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:1 + msgid "" + "Some\n" + "string" + msgstr "" + """ + And a foo-plugin/foo-plugin-de_DE.po file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "Language: de_DE\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:1 + msgid "" + "Some\n" + "string" + msgstr "" + "Some translated\n" + "string" + + #: foo-plugin.php:2 + msgid "" + "Removed\n" + "string" + msgstr "" + "Removed translated\n" + "string" + """ + + When I run `wp i18n update-po foo-plugin/foo-plugin.pot --no-purge` + Then STDOUT should be: + """ + Success: Updated 1 file. + """ + And STDERR should be empty + And the foo-plugin/foo-plugin-de_DE.po file should contain: + """ + #: foo-plugin.php:1 + msgid "" + "Some\n" + "string" + msgstr "" + "Some translated\n" + "string" + """ + And the foo-plugin/foo-plugin-de_DE.po file should contain: + """ + #~ msgid "" + #~ "Removed\n" + #~ "string" + #~ msgstr "" + #~ "Removed translated\n" + #~ "string" + """ diff --git a/src/Translations.php b/src/Translations.php index 4796e2f3..cfd55e32 100644 --- a/src/Translations.php +++ b/src/Translations.php @@ -5,6 +5,7 @@ use Gettext\Loader\PoLoader; use Gettext\Translation; use Gettext\Translations as GettextTranslations; +use RuntimeException; /** * A set of translations. @@ -53,12 +54,20 @@ public static function create( ?string $domain = null, ?string $language = null * * @param string|array $files A file path or a list of file paths. * @return static + * + * @throws RuntimeException If a file cannot be read. */ public static function fromPoFiles( $files ): GettextTranslations { $translations = null; foreach ( (array) $files as $file ) { - $loaded = static::fromPoString( (string) file_get_contents( $file ) ); + $content = is_file( $file ) && is_readable( $file ) ? file_get_contents( $file ) : false; + + if ( false === $content ) { + throw new RuntimeException( sprintf( 'Could not read file %s', $file ) ); + } + + $loaded = static::fromPoString( $content ); /** @var static $translations */ $translations = null === $translations ? $loaded : $translations->mergeWith( $loaded ); @@ -93,7 +102,7 @@ public static function fromPoString( string $content ): GettextTranslations { $translations->remove( $translation ); } - foreach ( preg_split( '/\R\s*\R/', $content ) ?: [] as $block ) { + foreach ( self::splitPoEntries( $content ) as $block ) { foreach ( $loader->loadString( $block, static::create() )->getTranslations() as $translation ) { $translations->addOrMerge( $translation ); } @@ -102,6 +111,38 @@ public static function fromPoString( string $content ): GettextTranslations { return $translations; } + /** + * Splits the contents of a PO file into its entries. + * + * Like the PO loader, an entry ends at an empty line, or at a `#` line once its `msgid` has been read. + * + * @param string $content File contents. + * @return array The entries. + */ + private static function splitPoEntries( string $content ): array { + $entries = []; + $entry = []; + $has_id = false; + + foreach ( preg_split( '/\R/', $content ) ?: [] as $line ) { + $trimmed = trim( $line ); + + if ( '' === $trimmed || ( '#' === $trimmed && $has_id ) ) { + $entries[] = implode( "\n", $entry ); + $entry = []; + $has_id = false; + continue; + } + + $entry[] = $line; + $has_id = $has_id || 1 === preg_match( '/^(#~\s*)?msgid\s/', $trimmed ); + } + + $entries[] = implode( "\n", $entry ); + + return array_filter( $entries ); + } + /** * Returns the translation with the given context and original string, creating it if needed. * diff --git a/src/UpdatePoCommand.php b/src/UpdatePoCommand.php index 28638737..0cf2d5ec 100644 --- a/src/UpdatePoCommand.php +++ b/src/UpdatePoCommand.php @@ -68,6 +68,10 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::error( 'Source file does not exist.' ); } + if ( ! is_readable( $source ) ) { + WP_CLI::error( 'Could not read source file.' ); + } + $destination = dirname( $source ); if ( isset( $args[1] ) ) { @@ -120,7 +124,7 @@ public function __invoke( $args, $assoc_args ) { // Reorder translations to match POT file order. $ordered_translations = $this->reorder_translations( $po_translations, $pot_translations ); - if ( ! ( new PoGenerator() )->generateFile( $ordered_translations, $file->getPathname() ) ) { + if ( false === file_put_contents( $file->getPathname(), $this->generate_po( $ordered_translations ) ) ) { WP_CLI::warning( sprintf( 'Could not update file %s', $file->getPathname() ) ); continue; } @@ -189,6 +193,32 @@ private function merge_translations( Translations $po_translations, Translations return $merged; } + /** + * Generates the contents of a PO file. + * + * gettext/gettext writes the continuation lines of a multiline obsolete entry + * without the `#~` prefix, which would turn them into active lines. + * + * @param Translations $translations Translations instance. + * @return string + */ + private function generate_po( Translations $translations ) { + $lines = explode( "\n", ( new PoGenerator() )->generateString( $translations ) ); + $obsolete = false; + + foreach ( $lines as $i => $line ) { + if ( '' === $line ) { + $obsolete = false; + } elseif ( 0 === strpos( $line, '#~ ' ) ) { + $obsolete = true; + } elseif ( $obsolete && 0 === strpos( $line, '"' ) ) { + $lines[ $i ] = '#~ ' . $line; + } + } + + return implode( "\n", $lines ); + } + /** * Check if two Translations objects differ. * diff --git a/tests/TranslationsTest.php b/tests/TranslationsTest.php index 1ee9edec..2ec24456 100644 --- a/tests/TranslationsTest.php +++ b/tests/TranslationsTest.php @@ -5,6 +5,7 @@ use Gettext\Generator\MoGenerator; use Gettext\Generator\PoGenerator; use Gettext\Loader\MoLoader; +use RuntimeException; use WP_CLI\I18n\Translations; use WP_CLI\Tests\TestCase; @@ -115,6 +116,44 @@ public function test_merges_duplicate_entries() { ); } + public function test_merges_duplicate_entries_separated_by_empty_comment() { + $translations = Translations::fromPoString( + <<<'PO' +msgid "" +msgstr "" +"Language: de\n" + +#: a.php:1 +msgid "Hello" +msgstr "" +# +#: b.php:2 +msgid "Hello" +msgstr "Hello (DE)" + +PO + ); + + $this->assertCount( 1, $translations ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertNotNull( $translation ); + $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); + $this->assertSame( + [ + [ 'a.php', 1 ], + [ 'b.php', 2 ], + ], + Translations::getReferenceList( $translation ) + ); + } + + public function test_throws_for_files_that_cannot_be_read() { + $this->expectException( RuntimeException::class ); + + Translations::fromPoFiles( "$this->dir/missing.po" ); + } + public function test_keeps_file_comments_and_flags() { file_put_contents( "$this->dir/de_DE.po", From 0083c3c54091e5e0ed20cd048d76539460dbfa3a Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 20 Nov 2026 22:02:26 +0000 Subject: [PATCH 11/14] Add Behat scenario for an unreadable POT file in `update-po` --- features/updatepo.feature | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/features/updatepo.feature b/features/updatepo.feature index 8e2b966d..6e79211c 100644 --- a/features/updatepo.feature +++ b/features/updatepo.feature @@ -1160,3 +1160,42 @@ Feature: Update existing PO files from a POT file #~ "Removed translated\n" #~ "string" """ + + Scenario: Bails when the POT file cannot be read + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.pot file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "X-Domain: foo-plugin\n" + """ + And a foo-plugin/foo-plugin-de_DE.po file: + """ + msgid "" + msgstr "" + "Project-Id-Version: Foo Plugin\n" + "Language: de_DE\n" + "X-Domain: foo-plugin\n" + + #: foo-plugin.php:1 + msgid "Some string" + msgstr "Some translated string" + """ + And I run `chmod 000 foo-plugin/foo-plugin.pot` + + When I try `wp i18n update-po foo-plugin/foo-plugin.pot` + Then STDERR should be: + """ + Error: Could not read source file. + """ + And the return code should be 1 + And the foo-plugin/foo-plugin-de_DE.po file should contain: + """ + #: foo-plugin.php:1 + msgid "Some string" + msgstr "Some translated string" + """ + + When I run `chmod 644 foo-plugin/foo-plugin.pot` + Then STDERR should be empty From ab5d802984160627e97701774fa9522877695aea Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 23 Nov 2026 17:38:52 +0000 Subject: [PATCH 12/14] Use gettext/php-scanner to find functions in PHP code Replaces the tokenizer-based scanner ported from gettext/gettext 4.x with the one from gettext/php-scanner, which uses nikic/php-parser. The subclass only adds error recovery, so that a file with a syntax error is still scanned instead of being skipped. As before, only the translator comment closest to a function call is extracted, and the lines of a multiline comment are joined with spaces. gettext/php-scanner is required as dev-master until 2.1.0 is released. --- composer.json | 1 + phpcs.xml.dist | 2 +- src/ParsedComment.php | 10 - src/ParsedFunction.php | 141 ----------- src/PhpCodeExtractor.php | 94 ++++++- src/PhpFunctionsScanner.php | 390 ++---------------------------- tests/PhpFunctionsScannerTest.php | 79 +++--- 7 files changed, 150 insertions(+), 567 deletions(-) delete mode 100644 src/ParsedFunction.php diff --git a/composer.json b/composer.json index 8eba4045..11a3a0e5 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "require": { "eftec/bladeone": "3.52", "gettext/gettext": "^5.7.4", + "gettext/php-scanner": "dev-master", "mck89/peast": "^1.13.11", "wp-cli/wp-cli": "^3.0" }, diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 4d54ff25..89438443 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -58,7 +58,7 @@ */src/*(Extractor|Generator|Scanner)\.php$ - */src/Parsed(Comment|Function)\.php$ + */src/ParsedComment\.php$ diff --git a/src/ParsedComment.php b/src/ParsedComment.php index 2df44a31..38834421 100644 --- a/src/ParsedComment.php +++ b/src/ParsedComment.php @@ -84,16 +84,6 @@ public function getComment() { return $this->comment; } - /** - * Whether this comment is on the same line as the given function call or on the one before. - * - * @param ParsedFunction $parsed_function The function to check. - * @return bool - */ - public function isRelatedWith( ParsedFunction $parsed_function ) { - return $this->getLastLine() === $parsed_function->getLine() || $this->getLastLine() === $parsed_function->getLine() - 1; - } - /** * Whether the comment starts with one of the given prefixes. * diff --git a/src/ParsedFunction.php b/src/ParsedFunction.php deleted file mode 100644 index 22140f6a..00000000 --- a/src/ParsedFunction.php +++ /dev/null @@ -1,141 +0,0 @@ - - */ - protected $arguments = []; - - /** - * @var int - */ - protected $argument_index = -1; - - /** - * Whether the current argument can no longer be a plain string. - * - * @var bool - */ - protected $argument_stopped = false; - - /** - * @var ParsedComment[]|null - */ - protected $comments; - - /** - * @param string $name The function name. - * @param int $line The line where the function call starts. - */ - public function __construct( $name, $line ) { - $this->name = $name; - $this->line = $line; - } - - /** - * Stops extracting strings for the current argument. - * - * @return void - */ - public function stopArgument() { - if ( -1 === $this->argument_index ) { - $this->argument_index = 0; - } - - $this->argument_stopped = true; - } - - /** - * Moves on to the next argument. - * - * @return void - */ - public function nextArgument() { - if ( -1 === $this->argument_index ) { - $this->argument_index = 1; - } else { - ++$this->argument_index; - } - - $this->argument_stopped = false; - } - - /** - * Adds a string to the current argument. - * - * @param string|null $chunk Part of the argument. - * @return void - */ - public function addArgumentChunk( $chunk ) { - if ( $this->argument_stopped ) { - return; - } - - if ( -1 === $this->argument_index ) { - $this->argument_index = 0; - } - - if ( isset( $this->arguments[ $this->argument_index ] ) ) { - $this->arguments[ $this->argument_index ] .= $chunk; - } else { - $this->arguments[ $this->argument_index ] = $chunk; - } - } - - /** - * Adds a comment related to this function call. - * - * @param ParsedComment $comment The comment. - * @return void - */ - public function addComment( ParsedComment $comment ) { - if ( null === $this->comments ) { - $this->comments = []; - } - - $this->comments[] = $comment; - } - - /** - * @return int - */ - public function getLine() { - return $this->line; - } - - /** - * Returns the function call once all its arguments have been read. - * - * @return array{0: string, 1: int, 2: array, 3: ParsedComment[]|null} Name, line, arguments and comments. - */ - public function close() { - $arguments = []; - - for ( $i = 0; $i <= $this->argument_index; ++$i ) { - $arguments[ $i ] = isset( $this->arguments[ $i ] ) ? $this->arguments[ $i ] : null; - } - - return [ - $this->name, - $this->line, - $arguments, - $this->comments, - ]; - } -} diff --git a/src/PhpCodeExtractor.php b/src/PhpCodeExtractor.php index 4434201b..55598ab8 100644 --- a/src/PhpCodeExtractor.php +++ b/src/PhpCodeExtractor.php @@ -82,14 +82,94 @@ public static function fromString( $text, Translations $translations, array $opt * @return void */ public static function scan( $text, Translations $translations, array $options ) { - $scanner = new PhpFunctionsScanner( $text ); + /** @var array $functions */ + $functions = isset( $options['functions'] ) && is_array( $options['functions'] ) ? $options['functions'] : []; - $extract_comments = $options['extractComments'] ?? false; - if ( is_string( $extract_comments ) || is_array( $extract_comments ) ) { - /** @var string|array $extract_comments */ - $scanner->enableCommentsExtraction( $extract_comments ); - } + $file = isset( $options['file'] ) && is_scalar( $options['file'] ) ? (string) $options['file'] : ''; + + $prefixes = $options['extractComments'] ?? false; + $prefixes = is_string( $prefixes ) || is_array( $prefixes ) ? array_filter( (array) $prefixes ) : false; + + $add_reference = ! empty( $options['addReferences'] ); + $target_domain = isset( $options['domain'] ) ? $options['domain'] : $translations->getDomain(); + + $scanner = new PhpFunctionsScanner( array_keys( $functions ) ); + + foreach ( $scanner->scan( $text, $file ) as $function ) { + $args = $function->getArguments(); + $original = null; + $domain = null; + $context = null; + $plural = null; + + switch ( $functions[ $function->getName() ] ) { + case 'text_domain': + case 'gettext': + list( $original, $domain ) = array_pad( $args, 2, null ); + break; + + case 'text_context_domain': + list( $original, $context, $domain ) = array_pad( $args, 3, null ); + break; + + case 'single_plural_number_domain': + list( $original, $plural, , $domain ) = array_pad( $args, 4, null ); + break; + + case 'single_plural_number_context_domain': + list( $original, $plural, , $context, $domain ) = array_pad( $args, 5, null ); + break; + + case 'single_plural_domain': + list( $original, $plural, $domain ) = array_pad( $args, 3, null ); + break; + + case 'single_plural_context_domain': + list( $original, $plural, $context, $domain ) = array_pad( $args, 4, null ); + break; - $scanner->saveGettextFunctions( $translations, $options ); + default: + WP_CLI::error( sprintf( "Internal error: unknown function map '%s' for '%s'.", $functions[ $function->getName() ], $function->getName() ) ); + } + + if ( ! is_string( $original ) || '' === $original ) { + continue; + } + + if ( $domain !== $target_domain && null !== $target_domain ) { + continue; + } + + $translation = $translations->insert( + is_string( $context ) ? $context : null, + $original, + is_string( $plural ) ? $plural : null + ); + + if ( $add_reference ) { + $translation->getReferences()->add( $file, $function->getLine() ); + } + + if ( + 1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original ) || + 1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original ) + ) { + $translation->getFlags()->add( 'php-format' ); + } + + if ( false === $prefixes ) { + continue; + } + + // Only the comment closest to the function call applies to it. + foreach ( array_reverse( $function->getComments() ) as $comment ) { + $comment = ParsedComment::create( $comment, $function->getLine() ); + + if ( $comment->checkPrefixes( $prefixes ) ) { + $translation->getExtractedComments()->add( $comment->getComment() ); + break; + } + } + } } } diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index 9fee9627..d9498d3b 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -2,387 +2,37 @@ namespace WP_CLI\I18n; +use Gettext\Scanner\ParsedFunction; +use Gettext\Scanner\PhpFunctionsScanner as GettextPhpFunctionsScanner; +use PhpParser\ErrorHandler\Collecting; +use PhpParser\NodeTraverser; + /** - * Finds function calls and their string arguments in PHP code. + * Finds function calls and their arguments in PHP code. * - * Based on the PHP functions scanner of gettext/gettext 4.x. + * Unlike the scanner of gettext/php-scanner, this one recovers from syntax errors + * instead of skipping the whole file. */ -class PhpFunctionsScanner { - - /** - * PHP tokens of the code to be parsed. - * - * @var array - */ - protected $tokens; - - /** - * If not false, comments will be extracted. - * - * @var string|false|array - */ - protected $extract_comments = false; - - /** - * @param string $code The PHP code to scan. - */ - public function __construct( $code ) { - $this->tokens = array_values( - array_filter( - token_get_all( $code ), - static function ( $token ) { - return ! is_array( $token ) || T_WHITESPACE !== $token[0]; - } - ) - ); - } +class PhpFunctionsScanner extends GettextPhpFunctionsScanner { /** - * Enable extracting comments that start with a tag (if $tag is empty all the comments will be extracted). + * {@inheritdoc} * - * @param string|array $tag Tag to extract. - * @return void + * @return ParsedFunction[] */ - public function enableCommentsExtraction( $tag = '' ) { - $this->extract_comments = $tag; - } - - /** - * Disable comments extraction. - * - * @return void - */ - public function disableCommentsExtraction() { - $this->extract_comments = false; - } - - /** - * Scans the code and returns the function calls and their arguments. - * - * @param array $constants Constants used in the code to replace. - * @return array, 3: ParsedComment[]|null}> List of name, line, arguments and comments. - */ - public function getFunctions( array $constants = [] ) { - $count = count( $this->tokens ); - - /** @var ParsedFunction[] $buffer_functions */ - $buffer_functions = []; - - /** @var ParsedComment[] $buffer_comments */ - $buffer_comments = []; - - $functions = []; + public function scan( string $code, string $filename ): array { + $ast = $this->parser->parse( $code, new Collecting() ); - for ( $k = 0; $k < $count; ++$k ) { - $value = $this->tokens[ $k ]; - - if ( is_string( $value ) ) { - if ( isset( $buffer_functions[0] ) ) { - switch ( $value ) { - case ',': - $buffer_functions[0]->nextArgument(); - break; - - case ')': - $functions[] = $buffer_functions[0]->close(); - array_shift( $buffer_functions ); - break; - - case '.': - break; - - default: - $buffer_functions[0]->stopArgument(); - break; - } - } - - continue; - } - - // phpcs:ignore PHPCompatibility.Constants.NewConstants.t_name_fully_qualifiedFound - if ( defined( 'T_NAME_FULLY_QUALIFIED' ) && T_NAME_FULLY_QUALIFIED === $value[0] ) { - $value[0] = T_STRING; - $value[1] = ltrim( $value[1], '\\' ); - } - - switch ( $value[0] ) { - case T_CONSTANT_ENCAPSED_STRING: - // Add an argument to the current function. - if ( isset( $buffer_functions[0] ) ) { - $buffer_functions[0]->addArgumentChunk( self::convertString( $value[1] ) ); - } - break; - - case T_STRING: - if ( isset( $buffer_functions[0] ) ) { - if ( isset( $constants[ $value[1] ] ) ) { - $buffer_functions[0]->addArgumentChunk( $constants[ $value[1] ] ); - break; - } - - if ( 'null' === strtolower( $value[1] ) ) { - $buffer_functions[0]->addArgumentChunk( null ); - break; - } - - $buffer_functions[0]->stopArgument(); - } - - // New function found. - for ( $j = $k + 1; $j < $count; ++$j ) { - $next_token = $this->tokens[ $j ]; - - if ( is_array( $next_token ) && T_COMMENT === $next_token[0] ) { - continue; - } - - if ( '(' === $next_token ) { - $new_function = new ParsedFunction( $value[1], $value[2] ); - - // Add comment that was on the line before. - if ( isset( $buffer_comments[0] ) && $buffer_comments[0]->isRelatedWith( $new_function ) ) { - $new_function->addComment( $buffer_comments[0] ); - } - - array_unshift( $buffer_functions, $new_function ); - $k = $j; - } - - break; - } - break; - - case T_COMMENT: - $comment = $this->parsePhpComment( $value[1], $value[2] ); - - if ( $comment ) { - array_unshift( $buffer_comments, $comment ); - - // The comment is inside the function call. - if ( isset( $buffer_functions[0] ) ) { - $buffer_functions[0]->addComment( $comment ); - } - } - break; - - default: - if ( isset( $buffer_functions[0] ) ) { - $buffer_functions[0]->stopArgument(); - } - break; - } + if ( empty( $ast ) ) { + return []; } - return $functions; - } - - /** - * Extracts the actual text from a PHP comment. - * - * @param string $value The PHP comment. - * @param int $line Line number. - * @return ParsedComment|null Comment or null if comment extraction is disabled or if there is a prefix mismatch. - */ - protected function parsePhpComment( $value, $line ) { - if ( false === $this->extract_comments ) { - return null; - } + $visitor = $this->createNodeVisitor( $filename ); - $comment = ParsedComment::create( $value, $line ); - $prefixes = array_filter( (array) $this->extract_comments ); + $traverser = new NodeTraverser(); + $traverser->addVisitor( $visitor ); + $traverser->traverse( $ast ); - return $comment->checkPrefixes( $prefixes ) ? $comment : null; - } - - /** - * Decodes a T_CONSTANT_ENCAPSED_STRING string. - * - * @param string $value String literal including its quotes. - * @return string - */ - public static function convertString( $value ) { - if ( false === strpos( $value, '\\' ) ) { - return substr( $value, 1, -1 ); - } - - if ( "'" === $value[0] ) { - return strtr( - substr( $value, 1, -1 ), - [ - '\\\\' => '\\', - '\\\'' => '\'', - ] - ); - } - - $value = substr( $value, 1, -1 ); - - return (string) preg_replace_callback( - '/\\\(n|r|t|v|e|f|\$|"|\\\|x[0-9A-Fa-f]{1,2}|u{[0-9a-f]{1,6}}|[0-7]{1,3})/', - static function ( $matches ) { - switch ( $matches[1][0] ) { - case 'n': - return "\n"; - case 'r': - return "\r"; - case 't': - return "\t"; - case 'v': - return "\v"; - case 'e': - return "\e"; - case 'f': - return "\f"; - case '$': - return '$'; - case '"': - return '"'; - case '\\': - return '\\'; - case 'x': - return chr( (int) hexdec( substr( $matches[1], 1 ) ) & 0xFF ); - case 'u': - return (string) self::unicodeChar( (int) hexdec( trim( substr( $matches[1], 1 ), '{}' ) ) ); - default: - return chr( (int) octdec( $matches[1] ) & 0xFF ); - } - }, - $value - ); - } - - /** - * Returns the UTF-8 encoded character for a Unicode code point. - * - * @param int $dec Code point. - * @return string|null - */ - protected static function unicodeChar( $dec ) { - if ( $dec < 0x80 ) { - return chr( $dec & 0x7F ); - } - - if ( $dec < 0x0800 ) { - return chr( 0xC0 + ( $dec >> 6 ) ) - . chr( 0x80 + ( $dec & 0x3f ) ); - } - - if ( $dec < 0x010000 ) { - return chr( 0xE0 + ( $dec >> 12 ) ) - . chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) ) - . chr( 0x80 + ( $dec & 0x3f ) ); - } - - if ( $dec < 0x200000 ) { - return chr( 0xF0 + ( $dec >> 18 ) ) - . chr( 0x80 + ( ( $dec >> 12 ) & 0x3f ) ) - . chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) ) - . chr( 0x80 + ( $dec & 0x3f ) ); - } - - return null; - } - - /** - * Searches for specific functions and creates translations. - * - * @param Translations $translations Translations instance. - * @param array $options Options. - * @return void - */ - public function saveGettextFunctions( Translations $translations, array $options ) { - $functions = isset( $options['functions'] ) ? $options['functions'] : []; - if ( ! is_array( $functions ) ) { - $functions = []; - } - - $file = ''; - if ( isset( $options['file'] ) && is_scalar( $options['file'] ) ) { - $file = (string) $options['file']; - } - - $add_reference = ! empty( $options['addReferences'] ); - - /** @var array $constants */ - $constants = isset( $options['constants'] ) && is_array( $options['constants'] ) ? $options['constants'] : []; - - foreach ( $this->getFunctions( $constants ) as $function ) { - list( $name, $line, $args ) = $function; - - if ( ! isset( $functions[ $name ] ) ) { - continue; - } - - $original = null; - $domain = null; - $context = null; - $plural = null; - - switch ( $functions[ $name ] ) { - case 'text_domain': - case 'gettext': - list( $original, $domain ) = array_pad( $args, 2, null ); - break; - - case 'text_context_domain': - list( $original, $context, $domain ) = array_pad( $args, 3, null ); - break; - - case 'single_plural_number_domain': - list( $original, $plural, $number, $domain ) = array_pad( $args, 4, null ); - break; - - case 'single_plural_number_context_domain': - list( $original, $plural, $number, $context, $domain ) = array_pad( $args, 5, null ); - break; - - case 'single_plural_domain': - list( $original, $plural, $domain ) = array_pad( $args, 3, null ); - break; - - case 'single_plural_context_domain': - list( $original, $plural, $context, $domain ) = array_pad( $args, 4, null ); - break; - - default: - // Should never happen. - $func_map = isset( $functions[ $name ] ) ? $functions[ $name ] : ''; - $func_map_str = is_scalar( $func_map ) ? (string) $func_map : ''; - $name_str = is_scalar( $name ) ? (string) $name : ''; - \WP_CLI::error( sprintf( "Internal error: unknown function map '%s' for '%s'.", $func_map_str, $name_str ) ); - } - - $original_str = ''; - if ( is_scalar( $original ) ) { - $original_str = (string) $original; - } - - if ( '' === $original_str ) { - continue; - } - - $target_domain = isset( $options['domain'] ) ? $options['domain'] : $translations->getDomain(); - if ( $domain !== $target_domain && null !== $target_domain ) { - continue; - } - - $translation = $translations->insert( $context, $original_str, $plural ); - - if ( $add_reference ) { - $translation->getReferences()->add( $file, $line ); - } - - if ( - 1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original_str ) || - 1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original_str ) - ) { - $translation->getFlags()->add( 'php-format' ); - } - - foreach ( $function[3] ?? [] as $extracted_comment ) { - $translation->getExtractedComments()->add( $extracted_comment->getComment() ); - } - } + return $visitor->getFunctions(); } } diff --git a/tests/PhpFunctionsScannerTest.php b/tests/PhpFunctionsScannerTest.php index 98b8d787..5d8c1f5d 100644 --- a/tests/PhpFunctionsScannerTest.php +++ b/tests/PhpFunctionsScannerTest.php @@ -10,33 +10,6 @@ class PhpFunctionsScannerTest extends TestCase { - /** - * @dataProvider data_convert_string - * - * @param string $literal String literal as found in PHP code. - * @param string $expected Expected value. - */ - #[\PHPUnit\Framework\Attributes\DataProvider( 'data_convert_string' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound - public function test_convert_string( $literal, $expected ) { - $this->assertSame( $expected, PhpFunctionsScanner::convertString( $literal ) ); - } - - /** - * @return array - */ - public static function data_convert_string() { - return [ - 'single quoted' => [ "'Hello'", 'Hello' ], - 'double quoted' => [ '"Hello"', 'Hello' ], - 'escaped single quote' => [ "'It\\'s'", "It's" ], - 'single quoted backslashes' => [ "'a\\\\b\\nc'", 'a\\b\\nc' ], - 'double quoted escapes' => [ '"a\\nb\\tc\\\\d\\"e\\$f"', "a\nb\tc\\d\"e\$f" ], - 'hexadecimal and octal' => [ '"\\x41\\101"', 'AA' ], - 'unicode code point' => [ '"\\u{e9}\\u{1f600}"', "\u{e9}\u{1f600}" ], - 'unknown escape is kept' => [ '"100\\%"', '100\\%' ], - ]; - } - public function test_finds_functions_with_arguments_and_comments() { $code = <<<'PHP' enableCommentsExtraction( [ 'translators' ] ); + $scanner = new PhpFunctionsScanner( [ '__', '_x', 'esc_html__' ] ); $functions = []; - foreach ( $scanner->getFunctions() as $function ) { - $comments = []; - foreach ( $function[3] ?? [] as $comment ) { - $comments[] = $comment->getComment(); - } - - $functions[] = [ $function[0], $function[1], $function[2], $comments ]; + foreach ( $scanner->scan( $code, 'file.php' ) as $function ) { + $functions[] = [ $function->getName(), $function->getLine(), $function->getArguments(), $function->getComments() ]; } $this->assertSame( [ [ '__', 3, [ 'Hello %s', 'foo' ], [ 'translators: %s: Name' ] ], - [ 'printf', 3, [ null, null ], [ 'translators: %s: Name' ] ], [ '_x', 4, [ 'Concat', 'context', 'foo' ], [] ], [ '__', 5, [ 'With ', 'foo' ], [] ], - [ 'esc_html__', 6, [ 'inner', 'foo' ], [] ], [ '__', 6, [ 'Nested ', null ], [] ], + [ 'esc_html__', 6, [ 'inner', 'foo' ], [] ], ], $functions ); } + public function test_recovers_from_syntax_errors() { + $code = <<<'PHP' +scan( $code, 'file.php' ) as $function ) { + $strings[] = $function->getArguments()[0]; + } + + $this->assertSame( [ 'Before', 'After' ], $strings ); + } + + public function test_joins_lines_of_multiline_comments() { + $code = <<<'PHP' +find( null, 'Hello %s' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( [ 'translators: %s: Name of the person.' ], $translation->getExtractedComments()->toArray() ); + } + public function test_extracts_strings_for_text_domain() { $code = <<<'PHP' Date: Mon, 23 Nov 2026 17:40:41 +0000 Subject: [PATCH 13/14] Fix PHPStan error for comment prefixes --- src/PhpCodeExtractor.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/PhpCodeExtractor.php b/src/PhpCodeExtractor.php index 55598ab8..e87b6242 100644 --- a/src/PhpCodeExtractor.php +++ b/src/PhpCodeExtractor.php @@ -87,8 +87,17 @@ public static function scan( $text, Translations $translations, array $options ) $file = isset( $options['file'] ) && is_scalar( $options['file'] ) ? (string) $options['file'] : ''; - $prefixes = $options['extractComments'] ?? false; - $prefixes = is_string( $prefixes ) || is_array( $prefixes ) ? array_filter( (array) $prefixes ) : false; + $prefixes = false; + if ( isset( $options['extractComments'] ) && ( is_string( $options['extractComments'] ) || is_array( $options['extractComments'] ) ) ) { + $prefixes = array_values( + array_filter( + (array) $options['extractComments'], + static function ( $prefix ) { + return is_string( $prefix ) && '' !== $prefix; + } + ) + ); + } $add_reference = ! empty( $options['addReferences'] ); $target_domain = isset( $options['domain'] ) ? $options['domain'] : $translations->getDomain(); From 1adb4b6a8da25fb746131423a2f6273e8fba93c4 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 24 Nov 2026 00:24:25 +0000 Subject: [PATCH 14/14] Require gettext/php-scanner 2.1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 11a3a0e5..cd436d1a 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "require": { "eftec/bladeone": "3.52", "gettext/gettext": "^5.7.4", - "gettext/php-scanner": "dev-master", + "gettext/php-scanner": "^2.1", "mck89/peast": "^1.13.11", "wp-cli/wp-cli": "^3.0" },