diff --git a/.gitattributes b/.gitattributes index 0708b6b..d84f4ad 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 bcf758d..cd436d1 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ ], "require": { "eftec/bladeone": "3.52", - "gettext/gettext": "^4.8", + "gettext/gettext": "^5.7.4", + "gettext/php-scanner": "^2.1", "mck89/peast": "^1.13.11", "wp-cli/wp-cli": "^3.0" }, diff --git a/features/makemo.feature b/features/makemo.feature index ae0e258..0814ff9 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 eda279c..431d204 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 1abb35d..7adcffc 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: @@ -4401,3 +4407,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/ParsedComment\.php$ @@ -65,7 +66,6 @@ - diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9ec6ffe..6d6c268 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 dcdf1c3..eb9e038 100644 --- a/src/AuditCommand.php +++ b/src/AuditCommand.php @@ -3,8 +3,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 +266,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 +381,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 +395,21 @@ protected function collect_audit_issues( $translations ) { foreach ( $translations as $translation ) { /** @var Translation $translation */ - $references = $translation->getReferences(); + $references = Translations::getReferenceList( $translation ); // 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 +424,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 +439,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 ); @@ -497,11 +483,11 @@ 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]; - 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 c289f51..8d2a4f5 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 66fa844..7b1b1b5 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 3d7d0e1..c4edd69 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 0000000..0c91006 --- /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 a98faf9..060eac6 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 a618d2f..8431ceb 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 ) ? (int) $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 07027a4..f902b82 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 3f1d74d..605ccab 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 c1ace36..8880bc9 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 2ea41f5..c9656a9 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 54e375a..5dbf1c3 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -2,9 +2,7 @@ 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 +237,10 @@ static function ( $value ) { protected function make_json( $source_file, $destination, $map, $domain, $extensions ) { /** @var Translations[] $mapping */ $mapping = []; - $translations = new Translations(); + $translations = Translations::fromPoFiles( $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 +272,31 @@ static function ( $reference ) use ( $extensions ) { ? (string) preg_replace( "/\.min\.{$extension}$/", ".{$extension}", $file ) : null; }, - $this->reference_map( $translation->getReferences(), $map ) + $this->reference_map( Translations::getReferenceList( $translation ), $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 +355,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 2b45cd6..51ea06b 100644 --- a/src/MakeMoCommand.php +++ b/src/MakeMoCommand.php @@ -3,7 +3,7 @@ namespace WP_CLI\I18n; use DirectoryIterator; -use Gettext\Translations; +use Gettext\Generator\MoGenerator; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -93,12 +93,12 @@ public function __invoke( $args, $assoc_args ) { } $destination_file = "{$destination}/{$file_name}"; - $translations = Translations::fromPoFile( $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 ( ! $translations->toMoFile( $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 0ef0948..3f313b4 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 = 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 eb9986e..8bf7c31 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -2,11 +2,8 @@ namespace WP_CLI\I18n; -use Gettext\Extractors\Po; -use Gettext\Merge; +use Gettext\Headers; use Gettext\Translation; -use Gettext\Translations; -use Gettext\Utils\ParsedComment; use WP_CLI; use WP_CLI_Command; use WP_CLI\Path; @@ -466,8 +463,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 ] = Translations::fromPoFiles( $file ); } } @@ -626,13 +622,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 ( Translations::fromPoFiles( $this->merge ) as $existing_translation ) { + /** @var Translation $existing_translation */ + $translations->add( $existing_translation ); + } } PotGenerator::setCommentBeforeHeaders( $this->get_file_comment() ); @@ -640,10 +637,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 +657,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 +670,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 +785,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 +815,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 = Translations::getReferenceList( $translation ); // 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 +843,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 +858,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 ); @@ -920,11 +900,11 @@ function ( $comment ) use ( &$unique_comments ) { 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]; - 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 +1013,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 +1047,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 e1d352f..a3935d6 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/ParsedComment.php b/src/ParsedComment.php new file mode 100644 index 0000000..3883442 --- /dev/null +++ b/src/ParsedComment.php @@ -0,0 +1,110 @@ +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 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/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 2c8a320..c312cc8 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(); @@ -73,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() 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; } } @@ -89,7 +90,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 +99,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 af79974..e87b624 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,113 @@ 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 ) { + /** @var array $functions */ + $functions = isset( $options['functions'] ) && is_array( $options['functions'] ) ? $options['functions'] : []; + + $file = isset( $options['file'] ) && is_scalar( $options['file'] ) ? (string) $options['file'] : ''; + + $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(); + + $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; + + 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 e1bf9e6..d9498d3 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -2,123 +2,37 @@ namespace WP_CLI\I18n; -use Gettext\Utils\PhpFunctionsScanner as GettextPhpFunctionsScanner; - +use Gettext\Scanner\ParsedFunction; +use Gettext\Scanner\PhpFunctionsScanner as GettextPhpFunctionsScanner; +use PhpParser\ErrorHandler\Collecting; +use PhpParser\NodeTraverser; + +/** + * Finds function calls and their arguments in PHP code. + * + * Unlike the scanner of gettext/php-scanner, this one recovers from syntax errors + * instead of skipping the whole file. + */ class PhpFunctionsScanner extends GettextPhpFunctionsScanner { /** * {@inheritdoc} * - * @param \Gettext\Translations $translations Translations instance. - * @param array $options Options. - * @return void + * @return ParsedFunction[] */ - 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]; - } - - $functions = isset( $options['functions'] ) ? $options['functions'] : []; - if ( ! is_array( $functions ) ) { - $functions = []; - } + public function scan( string $code, string $filename ): array { + $ast = $this->parser->parse( $code, new Collecting() ); - $file = ''; - if ( isset( $options['file'] ) && is_scalar( $options['file'] ) ) { - $file = (string) $options['file']; + if ( empty( $ast ) ) { + return []; } - $add_reference = ! empty( $options['addReferences'] ); - - $constants = isset( $options['constants'] ) ? $options['constants'] : []; - if ( ! is_array( $constants ) ) { - $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; + $visitor = $this->createNodeVisitor( $filename ); - case 'single_plural_number_domain': - list( $original, $plural, $number, $domain ) = array_pad( $args, 4, null ); - break; + $traverser = new NodeTraverser(); + $traverser->addVisitor( $visitor ); + $traverser->traverse( $ast ); - 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 = $translation->addReference( $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' ); - } - - 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() ); - } - } - } - } + return $visitor->getFunctions(); } } diff --git a/src/PotGenerator.php b/src/PotGenerator.php index e2a1dfc..dfc569c 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -2,17 +2,15 @@ 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. + * 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 */ @@ -39,92 +37,88 @@ 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->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; - } + $plural_form = $translations->getHeaders()->getPluralForm(); + $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : 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; - } - } - } + /** @var Translation $translation */ - 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; - } - } - } - } + $prefix = static::addMetaLines( $lines, $translation ); + + if ( null !== $translation->getPlural() && '' !== $translation->getPlural() ) { + static::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); - 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; + for ( $i = 0; $i <= $plural_size; $i++ ) { + static::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' ); } + } else { + static::addLines( $lines, $prefix . 'msgstr', (string) $translation->getTranslation() ); } - if ( $translation->hasFlags() ) { - $lines[] = '#, ' . implode( ',', $translation->getFlags() ); - } + $lines[] = ''; + } - $prefix = $translation->isDisabled() ? '#~ ' : ''; + return implode( "\n", $lines ); + } - if ( $translation->hasContext() ) { - $lines[] = $prefix . 'msgctxt ' . self::convertString( $translation->getContext() ); - } + /** + * Returns the lines for the header entry. + * + * @param Translations $translations Translations instance. + * @return array + */ + protected static function getHeaderLines( Translations $translations ) { + $lines = [ 'msgid ""', 'msgstr ""' ]; - self::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); + foreach ( $translations->getHeaders()->toArray() as $name => $value ) { + $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); + } - if ( $translation->hasPlural() ) { - self::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); + $lines[] = ''; - for ( $i = 0; $i <= $plural_size; $i++ ) { - self::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' ); - } - } else { - self::addLines( $lines, $prefix . 'msgstr', $translation->getTranslation() ); - } + return $lines; + } - $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] : '' ); } - $string_lines = array_filter( $lines, 'is_string' ); - return implode( "\n", $string_lines ); + 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; } /** @@ -134,15 +128,15 @@ public static function toString( Translations $translations, array $options = [] * * @return string[] */ - protected static function wpMultilineQuote( $text ) { + protected static function multilineQuote( $text ) { $lines = explode( "\n", $text ); $last = count( $lines ) - 1; foreach ( $lines as $k => $line ) { if ( $k === $last ) { - $lines[ $k ] = self::convertString( $line ); + $lines[ $k ] = static::convertString( $line ); } else { - $lines[ $k ] = self::convertString( $line . "\n" ); + $lines[ $k ] = static::convertString( $line . "\n" ); } } @@ -152,13 +146,13 @@ protected static function wpMultilineQuote( $text ) { /** * Add one or more lines depending whether the string is multiline or not. * - * @param array &$lines Array lines should be added to. + * @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 ); + $newlines = static::multilineQuote( $value ); if ( count( $newlines ) === 1 ) { $lines[] = $name . ' ' . $newlines[0]; @@ -170,4 +164,24 @@ protected static function addLines( array &$lines, $name, $value ) { } } } + + /** + * 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/Translations.php b/src/Translations.php new file mode 100644 index 0000000..cfd55e3 --- /dev/null +++ b/src/Translations.php @@ -0,0 +1,201 @@ + '', + '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 ); + } + } + + /** + * {@inheritdoc} + * + * @return static + */ + public static function create( ?string $domain = null, ?string $language = null ): GettextTranslations { + /** @var static $translations */ + $translations = parent::create( $domain, $language ); + + 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 + * + * @throws RuntimeException If a file cannot be read. + */ + public static function fromPoFiles( $files ): GettextTranslations { + $translations = null; + + foreach ( (array) $files as $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 ); + } + + 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 ( self::splitPoEntries( $content ) as $block ) { + foreach ( $loader->loadString( $block, static::create() )->getTranslations() as $translation ) { + $translations->addOrMerge( $translation ); + } + } + + 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. + * + * @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 ( null === $translation ) { + $translation = Translation::create( $context, $original ); + $this->add( $translation ); + } + + 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 c9e0a22..0cf2d5e 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\Translations; +use Gettext\Translation; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -66,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] ) ) { @@ -82,18 +88,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 = Translations::fromPoFiles( $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; @@ -108,46 +106,29 @@ 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() ); - } - $po_translations = Translations::fromPoFile( $file->getPathname() ); - $original_translations = clone $po_translations; - - $po_translations->mergeWith( - $pot_translations, - $merge_flags - ); + $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; } // 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 ( false === file_put_contents( $file->getPathname(), $this->generate_po( $ordered_translations ) ) ) { 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 { @@ -166,71 +147,76 @@ public function __invoke( $args, $assoc_args ) { } /** - * Extract file-level comments from a PO file. + * Merges the entries of a POT file into the translations of 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. + * @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 extract_file_comments( $file_path ) { - $content = file_get_contents( $file_path ); - if ( false === $content ) { - return ''; + 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 ); } - $lines = explode( "\n", $content ); - $file_comments = []; - $found_msgid = false; + $strategy = Merge::EXTRACTED_COMMENTS_THEIRS | Merge::REFERENCES_THEIRS; - foreach ( $lines as $line ) { - $trimmed = trim( $line ); + if ( $purge ) { + $strategy |= Merge::COMMENTS_THEIRS; - // Stop when we hit the first msgid - if ( preg_match( '/^msgid\s/', $trimmed ) ) { - $found_msgid = true; - break; - } + // File-level comments, like a copyright notice. + $merged->setDescription( null ); - // Collect comment lines - if ( preg_match( '/^#([^.,:~]|$)/', $trimmed ) ) { - $file_comments[] = $line; + foreach ( $merged->getTranslations() as $po_entry ) { + if ( ! $pot_translations->has( $po_entry ) ) { + $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 ) { + /** @var Translation $pot_entry */ + $merged->addOrMerge( clone $pot_entry, $strategy ); } - return ! empty( $file_comments ) ? implode( "\n", $file_comments ) . "\n" : ''; + return $merged; } /** - * Restore file-level comments to a PO file. + * 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 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. + * @param Translations $translations Translations instance. + * @return string */ - 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; + 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 true; + return implode( "\n", $lines ); } /** @@ -248,7 +234,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(); @@ -265,6 +251,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(); @@ -274,8 +265,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 = Translations::getReferenceList( $translation ); + $updated_refs = Translations::getReferenceList( $updated_translation ); $original_refs_sorted = $original_refs; $updated_refs_sorted = $updated_refs; @@ -288,11 +279,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 +300,31 @@ 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 ); + // 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 \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 e25e86a..0000000 --- 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 3b44fa5..e39b896 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 afc9d51..2d0b1e3 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 0000000..5d8c1f5 --- /dev/null +++ b/tests/PhpFunctionsScannerTest.php @@ -0,0 +1,109 @@ +scan( $code, 'file.php' ) as $function ) { + $functions[] = [ $function->getName(), $function->getLine(), $function->getArguments(), $function->getComments() ]; + } + + $this->assertSame( + [ + [ '__', 3, [ 'Hello %s', 'foo' ], [ 'translators: %s: Name' ] ], + [ '_x', 4, [ 'Concat', 'context', 'foo' ], [] ], + [ '__', 5, [ 'With ', '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' + '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 ] ], Translations::getReferenceList( $translation ) ); + } +} diff --git a/tests/PotGeneratorTest.php b/tests/PotGeneratorTest.php index 2e7ff38..226c309 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/TranslationsTest.php b/tests/TranslationsTest.php new file mode 100644 index 0000000..2ec2445 --- /dev/null +++ b/tests/TranslationsTest.php @@ -0,0 +1,246 @@ +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_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", + <<<'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 ); + } +}