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..d2d880d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "eftec/bladeone": "3.52", - "gettext/gettext": "^4.8", + "gettext/gettext": "^5.7", "mck89/peast": "^1.13.11", "wp-cli/wp-cli": "^3.0" }, diff --git a/features/makemo.feature b/features/makemo.feature index 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/Parsed(Comment|Function)\.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..b53f92a 100644 --- a/src/AuditCommand.php +++ b/src/AuditCommand.php @@ -2,9 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Translation; -use Gettext\Translations; -use Gettext\Utils\ParsedComment; use WP_CLI; use WP_CLI\Path; use WP_CLI\Utils; @@ -268,7 +265,7 @@ protected function get_main_file_data() { * @return Translations A Translation set. */ protected function extract_strings_for_audit() { - $translations = new Translations(); + $translations = Translations::create(); if ( $this->domain ) { $translations->setDomain( $this->domain ); @@ -383,16 +380,6 @@ protected function extract_strings_for_audit() { return $translations; } - /** - * Extracts comment text from a ParsedComment or string. - * - * @param ParsedComment|string $comment Comment object or string. - * @return string Comment text. - */ - protected function get_comment_text( $comment ) { - return $comment instanceof ParsedComment ? $comment->getComment() : $comment; - } - /** * Collects audit issues from translations. * @@ -407,19 +394,21 @@ protected function collect_audit_issues( $translations ) { foreach ( $translations as $translation ) { /** @var Translation $translation */ - $references = $translation->getReferences(); + $references = $translation->getReferenceEntries(); // File headers don't have any file references. - if ( ! $translation->hasReferences() ) { + if ( empty( $references ) ) { continue; } $file = $references[0][0]; - $line = $references[0][1] ?? null; + $line = $references[0][1]; + + $comments = $translation->getExtractedComments()->toArray(); // Check 1: Flag strings with placeholders that should have translator comments. if ( - ! $translation->hasExtractedComments() && + empty( $comments ) && preg_match( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal() ) >= 1 ) { $issues[] = [ @@ -434,15 +423,13 @@ protected function collect_audit_issues( $translations ) { } // Check 2: Flag strings with different translator comments. - if ( $translation->hasExtractedComments() ) { - $comments = $translation->getExtractedComments(); - + if ( ! empty( $comments ) ) { // Remove plugin header information from comments. $comments = array_filter( $comments, function ( $comment ) { foreach ( $this->get_file_headers( $this->project_type ) as $file_header ) { - if ( 0 === strpos( $this->get_comment_text( $comment ), $file_header ) ) { + if ( 0 === strpos( $comment, $file_header ) ) { return false; } } @@ -451,9 +438,7 @@ function ( $comment ) { } ); - $unique_comment_texts = array_unique( - array_map( [ $this, 'get_comment_text' ], $comments ) - ); + $unique_comment_texts = array_unique( $comments ); $comments_count = count( $unique_comment_texts ); @@ -501,7 +486,7 @@ function ( $comment ) { preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $single_placeholders ); $single_placeholders = $single_placeholders[0]; - preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getPlural(), $plural_placeholders ); + preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, (string) $translation->getPlural(), $plural_placeholders ); $plural_placeholders = $plural_placeholders[0]; // see https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals diff --git a/src/BladeCodeExtractor.php b/src/BladeCodeExtractor.php index 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..6d17079 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -2,9 +2,6 @@ namespace WP_CLI\I18n; -use Gettext\Extractors\Po as PoExtractor; -use Gettext\Translation; -use Gettext\Translations; use WP_CLI; use WP_CLI_Command; use WP_CLI\Utils; @@ -239,12 +236,10 @@ static function ( $value ) { protected function make_json( $source_file, $destination, $map, $domain, $extensions ) { /** @var Translations[] $mapping */ $mapping = []; - $translations = new Translations(); + $translations = PoLoader::fromFiles( $source_file ); $result = []; $extensions = array_merge( [ 'js' ], $extensions ); - PoExtractor::fromFile( $source_file, $translations ); - $base_file_name = basename( $source_file, '.po' ); $domain = ( ! empty( $domain ) ) ? $domain : $translations->getDomain(); @@ -276,31 +271,31 @@ static function ( $reference ) use ( $extensions ) { ? (string) preg_replace( "/\.min\.{$extension}$/", ".{$extension}", $file ) : null; }, - $this->reference_map( $translation->getReferences(), $map ) + $this->reference_map( $translation->getReferenceEntries(), $map ) ); $sources = array_unique( array_filter( $sources ) ); foreach ( $sources as $source ) { if ( ! isset( $mapping[ $source ] ) ) { - $mapping[ $source ] = new Translations(); + $mapping[ $source ] = Translations::create(); // phpcs:ignore Squiz.PHP.CommentedOutCode.Found -- Provide code that is meant to be used once the bug is fixed. // See https://core.trac.wordpress.org/ticket/45441 // $mapping[ $source ]->setDomain( $translations->getDomain() ); - $mapping[ $source ]->setHeader( 'Language', $translations->getLanguage() ); - $mapping[ $source ]->setHeader( 'PO-Revision-Date', $translations->getHeader( 'PO-Revision-Date' ) ?? '' ); + $mapping[ $source ]->getHeaders()->set( 'Language', (string) $translations->getLanguage() ); + $mapping[ $source ]->getHeaders()->set( 'PO-Revision-Date', (string) $translations->getHeaders()->get( 'PO-Revision-Date' ) ); - $plural_forms = $translations->getPluralForms(); + $plural_forms = $translations->getHeaders()->getPluralForm(); if ( $plural_forms ) { list( $count, $rule ) = $plural_forms; - $mapping[ $source ]->setPluralForms( $count, $rule ); + $mapping[ $source ]->getHeaders()->setPluralForm( $count, $rule ); } } - $mapping[ $source ][] = $translation; + $mapping[ $source ]->addOrMerge( $translation ); } } @@ -359,9 +354,9 @@ static function ( $value ) { * * Exports translations for each JS file to a separate translation file. * - * @param array $mapping A mapping of files to translation entries. - * @param string $base_file_name Base file name for JSON files. - * @param string $destination Path to the destination directory. + * @param array $mapping A mapping of files to translation entries. + * @param string $base_file_name Base file name for JSON files. + * @param string $destination Path to the destination directory. * * @return array List of created JSON files. */ diff --git a/src/MakeMoCommand.php b/src/MakeMoCommand.php index 2b45cd6..466ba1f 100644 --- a/src/MakeMoCommand.php +++ b/src/MakeMoCommand.php @@ -3,7 +3,6 @@ namespace WP_CLI\I18n; use DirectoryIterator; -use Gettext\Translations; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -93,12 +92,12 @@ public function __invoke( $args, $assoc_args ) { } $destination_file = "{$destination}/{$file_name}"; - $translations = Translations::fromPoFile( $file->getPathname() ); + $translations = PoLoader::fromFiles( $file->getPathname() ); // Remove JS-only strings from MO files to keep them small. $this->remove_js_only_strings( $translations ); - if ( ! $translations->toMoFile( $destination_file ) ) { + if ( ! MoGenerator::toFile( $translations, $destination_file ) ) { WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) ); continue; } diff --git a/src/MakePhpCommand.php b/src/MakePhpCommand.php index 0ef0948..ab81087 100644 --- a/src/MakePhpCommand.php +++ b/src/MakePhpCommand.php @@ -3,7 +3,6 @@ namespace WP_CLI\I18n; use DirectoryIterator; -use Gettext\Translations; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -90,7 +89,7 @@ public function __invoke( $args, $assoc_args ) { $file_basename = basename( $file->getFilename(), '.po' ); $destination_file = "{$destination}/{$file_basename}.l10n.php"; - $translations = Translations::fromPoFile( $file->getPathname() ); + $translations = PoLoader::fromFiles( $file->getPathname() ); // Remove JS-only strings from PHP files to keep them small. $this->remove_js_only_strings( $translations ); diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index eb9986e..416c750 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -2,11 +2,7 @@ namespace WP_CLI\I18n; -use Gettext\Extractors\Po; -use Gettext\Merge; -use Gettext\Translation; -use Gettext\Translations; -use Gettext\Utils\ParsedComment; +use Gettext\Headers; use WP_CLI; use WP_CLI_Command; use WP_CLI\Path; @@ -466,8 +462,7 @@ function ( $file ) { WP_CLI::debug( sprintf( 'Ignoring any string already existing in: %s', $file ), 'make-pot' ); - $this->exceptions[ $file ] = new Translations(); - Po::fromFile( $file, $this->exceptions[ $file ] ); + $this->exceptions[ $file ] = PoLoader::fromFiles( $file ); } } @@ -626,13 +621,14 @@ protected function get_file_headers( $type ) { * @return Translations A Translation set. */ protected function extract_strings() { - $translations = new Translations(); + $translations = Translations::create(); // Add existing strings first but don't keep headers. if ( ! empty( $this->merge ) ) { - $existing_translations = new Translations(); - Po::fromFile( $this->merge, $existing_translations ); - $translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE ); + foreach ( PoLoader::fromFiles( $this->merge ) as $existing_translation ) { + /** @var Translation $existing_translation */ + $translations->add( $existing_translation ); + } } PotGenerator::setCommentBeforeHeaders( $this->get_file_comment() ); @@ -640,10 +636,10 @@ protected function extract_strings() { $this->set_default_headers( $translations ); // POT files have no Language header. - $translations->deleteHeader( Translations::HEADER_LANGUAGE ); + $translations->getHeaders()->delete( Headers::HEADER_LANGUAGE ); // Only relevant for PO files, not POT files. - $translations->setHeader( 'PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE' ); + $translations->getHeaders()->set( 'PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE' ); if ( $this->domain ) { $translations->setDomain( $this->domain ); @@ -660,12 +656,12 @@ protected function extract_strings() { } $val = $data['value']; - $translation = new Translation( '', is_scalar( $val ) ? (string) $val : '' ); + $translation = $translations->insert( null, is_scalar( $val ) ? (string) $val : '' ); if ( $is_theme ) { - $translation->addExtractedComment( sprintf( '%s of the theme', $header ) ); + $translation->getExtractedComments()->add( sprintf( '%s of the theme', $header ) ); } else { - $translation->addExtractedComment( sprintf( '%s of the plugin', $header ) ); + $translation->getExtractedComments()->add( sprintf( '%s of the plugin', $header ) ); } if ( $this->main_file_path && $this->location ) { @@ -673,13 +669,11 @@ protected function extract_strings() { // Add line number if available if ( ! empty( $data['line'] ) ) { $line_val = $data['line']; - $translation->addReference( $file_reference, is_numeric( $line_val ) ? (int) $line_val : null ); + $translation->getReferences()->add( $file_reference, is_numeric( $line_val ) ? (int) $line_val : null ); } else { - $translation->addReference( $file_reference ); + $translation->getReferences()->add( $file_reference ); } } - - $translations[] = $translation; } try { @@ -790,18 +784,17 @@ protected function extract_strings() { foreach ( $this->exceptions as $file => $exception_translations ) { /** @var Translation $exception_translation */ foreach ( $exception_translations as $exception_translation ) { - if ( ! $translations->find( $exception_translation ) ) { + $translation = $translations->find( $exception_translation->getContext(), $exception_translation->getOriginal() ); + + if ( ! $translation ) { continue; } if ( $this->subtract_and_merge ) { - $translation = $translations[ $exception_translation->getId() ]; - if ( $translation instanceof Translation ) { - $exception_translation->mergeWith( $translation ); - } + $exception_translations->add( $exception_translation->mergeWith( $translation ) ); } - unset( $translations[ $exception_translation->getId() ] ); + $translations->remove( $translation ); } if ( $this->subtract_and_merge ) { @@ -821,21 +814,23 @@ protected function extract_strings() { * * Goes through all extracted strings to find possible mistakes. * - * @param \Gettext\Translations $translations Translations object. + * @param Translations $translations Translations object. * @return void */ protected function audit_strings( $translations ) { foreach ( $translations as $translation ) { /** @var Translation $translation */ - $references = $translation->getReferences(); + $references = $translation->getReferenceEntries(); // File headers don't have any file references. - $location = $translation->hasReferences() ? '(' . implode( ':', $references[0] ) . ')' : ''; + $location = ! empty( $references ) ? '(' . implode( ':', $references[0] ) . ')' : ''; + + $comments = $translation->getExtractedComments()->toArray(); // Check 1: Flag strings with placeholders that should have translator comments. if ( - ! $translation->hasExtractedComments() && + empty( $comments ) && preg_match( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $placeholders ) >= 1 ) { $message = sprintf( @@ -847,15 +842,13 @@ protected function audit_strings( $translations ) { } // Check 2: Flag strings with different translator comments. - if ( $translation->hasExtractedComments() ) { - $comments = $translation->getExtractedComments(); - + if ( ! empty( $comments ) ) { // Remove plugin header information from comments. $comments = array_filter( $comments, function ( $comment ) { foreach ( $this->get_file_headers( $this->project_type ) as $file_header ) { - if ( 0 === strpos( ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ), $file_header ) ) { + if ( 0 === strpos( $comment, $file_header ) ) { return null; } } @@ -864,22 +857,8 @@ function ( $comment ) { } ); - $unique_comments = array(); - - // Remove duplicate comments. - $comments = array_filter( - $comments, - function ( $comment ) use ( &$unique_comments ) { - /** @var ParsedComment|string $comment */ - if ( in_array( ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ), $unique_comments, true ) ) { - return false; - } - - $unique_comments[] = ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ); - - return true; - } - ); + $unique_comments = array_values( array_unique( $comments ) ); + $comments = $unique_comments; $comments_count = count( $comments ); @@ -924,7 +903,7 @@ function ( $comment ) use ( &$unique_comments ) { preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getOriginal(), $single_placeholders ); $single_placeholders = $single_placeholders[0]; - preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, $translation->getPlural(), $plural_placeholders ); + preg_match_all( self::SPRINTF_PLACEHOLDER_REGEX, (string) $translation->getPlural(), $plural_placeholders ); $plural_placeholders = $plural_placeholders[0]; // see https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#plurals @@ -1033,7 +1012,7 @@ protected function get_file_comment() { /** * Sets default POT file headers for the project. * - * @param \Gettext\Translations $translations Translations object. + * @param Translations $translations Translations object. * @return void */ protected function set_default_headers( $translations ) { @@ -1067,20 +1046,20 @@ protected function set_default_headers( $translations ) { } if ( null !== $name ) { - $translations->setHeader( 'Project-Id-Version', $name . ( $version ? ' ' . $version : '' ) ); + $translations->getHeaders()->set( 'Project-Id-Version', $name . ( $version ? ' ' . $version : '' ) ); } if ( null !== $bugs_address ) { - $translations->setHeader( 'Report-Msgid-Bugs-To', $bugs_address ); + $translations->getHeaders()->set( 'Report-Msgid-Bugs-To', $bugs_address ); } - $translations->setHeader( 'Last-Translator', 'FULL NAME ' ); - $translations->setHeader( 'Language-Team', 'LANGUAGE ' ); - $translations->setHeader( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION ); + $translations->getHeaders()->set( 'Last-Translator', 'FULL NAME ' ); + $translations->getHeaders()->set( 'Language-Team', 'LANGUAGE ' ); + $translations->getHeaders()->set( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION ); foreach ( $this->headers as $key => $value ) { if ( is_scalar( $value ) ) { - $translations->setHeader( $key, (string) $value ); + $translations->getHeaders()->set( $key, (string) $value ); } } } diff --git a/src/MapCodeExtractor.php b/src/MapCodeExtractor.php index 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/MoGenerator.php b/src/MoGenerator.php new file mode 100644 index 0000000..534a361 --- /dev/null +++ b/src/MoGenerator.php @@ -0,0 +1,152 @@ + $options + * @return string + */ + public static function toString( Translations $translations, array $options = [] ) { + $headers = ''; + + foreach ( $translations->getHeaders()->toArray() as $name => $value ) { + $headers .= sprintf( "%s: %s\n", $name, $value ); + } + + /** @var array $messages */ + $messages = [ '' => $headers ]; + + foreach ( $translations as $translation ) { + /** @var Translation $translation */ + + if ( null === $translation->getTranslation() || '' === $translation->getTranslation() || $translation->isDisabled() ) { + continue; + } + + $original_string = $translation->getOriginal(); + + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { + $original_string = $translation->getContext() . "\x04" . $original_string; + } + + $messages[ $original_string ] = $translation; + } + + ksort( $messages ); + + $num_entries = count( $messages ); + $originals_table = ''; + $translations_table = ''; + $originals_index = []; + $translations_index = []; + + $plural_form = $translations->getHeaders()->getPluralForm(); + $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : null; + + foreach ( $messages as $original_string => $translation ) { + $original_string = (string) $original_string; + + if ( is_string( $translation ) ) { + // Headers. + $translation_string = $translation; + } else { + $translation_string = (string) $translation->getTranslation(); + + if ( + null !== $translation->getPlural() && + '' !== $translation->getPlural() && + '' !== implode( '', $translation->getPluralTranslations() ) + ) { + $original_string .= "\x00" . $translation->getPlural(); + $translation_string .= "\x00" . implode( "\x00", $translation->getPluralTranslations( $plural_size ) ); + } + } + + $originals_index[] = [ + 'relativeOffset' => strlen( $originals_table ), + 'length' => strlen( $original_string ), + ]; + + $originals_table .= $original_string . "\x00"; + + $translations_index[] = [ + 'relativeOffset' => strlen( $translations_table ), + 'length' => strlen( $translation_string ), + ]; + + $translations_table .= $translation_string . "\x00"; + } + + // Offset of table with the original strings index: right after the header (which is 7 words). + $originals_index_offset = 7 * 4; + + // Size of table with the original strings index. + $originals_index_size = $num_entries * ( 4 + 4 ); + + // Offset of table with the translation strings index: right after the original strings index table. + $translations_index_offset = $originals_index_offset + $originals_index_size; + + // Size of table with the translation strings index. + $translations_index_size = $num_entries * ( 4 + 4 ); + + // Hashing table starts after the header and after the index table. + $originals_strings_offset = $translations_index_offset + $translations_index_size; + + // Translations start after the keys. + $translations_strings_offset = $originals_strings_offset + strlen( $originals_table ); + + // Magic number. + $mo = pack( 'L', 0x950412de ); + + // File format revision. + $mo .= pack( 'L', 0 ); + + // Number of strings. + $mo .= pack( 'L', $num_entries ); + + // Offset of table with original strings. + $mo .= pack( 'L', $originals_index_offset ); + + // Offset of table with translation strings. + $mo .= pack( 'L', $translations_index_offset ); + + // Size of hashing table: we don't use it. + $mo .= pack( 'L', 0 ); + + // Offset of hashing table: it would start right after the translations index table. + $mo .= pack( 'L', $translations_index_offset + $translations_index_size ); + + // Write the lengths & offsets of the original strings. + foreach ( $originals_index as $info ) { + $mo .= pack( 'L', $info['length'] ); + $mo .= pack( 'L', $originals_strings_offset + $info['relativeOffset'] ); + } + + // Write the lengths & offsets of the translated strings. + foreach ( $translations_index as $info ) { + $mo .= pack( 'L', $info['length'] ); + $mo .= pack( 'L', $translations_strings_offset + $info['relativeOffset'] ); + } + + // Write original strings. + $mo .= $originals_table; + + // Write translation strings. + $mo .= $translations_table; + + return $mo; + } +} diff --git a/src/ParsedComment.php b/src/ParsedComment.php new file mode 100644 index 0000000..2df44a3 --- /dev/null +++ b/src/ParsedComment.php @@ -0,0 +1,120 @@ +comment = $comment; + $this->first_line = $first_line; + $this->last_line = $last_line; + } + + /** + * Creates a new instance from a raw comment. + * + * @param string $value The comment as found in the source code. + * @param int $line The line where the comment starts. + * @return static + */ + public static function create( $value, $line ) { + $last_line = $line + substr_count( $value, "\n" ); + + $lines = array_map( + static function ( $line ) { + if ( '' === trim( $line ) ) { + return null; + } + + $line = ltrim( $line, "#*/ \t" ); + $line = rtrim( $line, "#*/ \t" ); + + return trim( $line ); + }, + explode( "\n", $value ) + ); + + // Remove empty lines. + $lines = array_filter( $lines ); + + return new static( implode( ' ', $lines ), $line, $last_line ); + } + + /** + * @return int + */ + public function getFirstLine() { + return $this->first_line; + } + + /** + * @return int + */ + public function getLastLine() { + return $this->last_line; + } + + /** + * @return string + */ + public function getComment() { + return $this->comment; + } + + /** + * Whether this comment is on the same line as the given function call or on the one before. + * + * @param ParsedFunction $parsed_function The function to check. + * @return bool + */ + public function isRelatedWith( ParsedFunction $parsed_function ) { + return $this->getLastLine() === $parsed_function->getLine() || $this->getLastLine() === $parsed_function->getLine() - 1; + } + + /** + * Whether the comment starts with one of the given prefixes. + * + * @param array $prefixes Prefixes to check. Every non-empty comment matches if there are none. + * @return bool + */ + public function checkPrefixes( array $prefixes ) { + if ( '' === $this->comment ) { + return false; + } + + if ( empty( $prefixes ) ) { + return true; + } + + foreach ( $prefixes as $prefix ) { + if ( 0 === strpos( $this->comment, $prefix ) ) { + return true; + } + } + + return false; + } +} diff --git a/src/ParsedFunction.php b/src/ParsedFunction.php new file mode 100644 index 0000000..22140f6 --- /dev/null +++ b/src/ParsedFunction.php @@ -0,0 +1,141 @@ + + */ + protected $arguments = []; + + /** + * @var int + */ + protected $argument_index = -1; + + /** + * Whether the current argument can no longer be a plain string. + * + * @var bool + */ + protected $argument_stopped = false; + + /** + * @var ParsedComment[]|null + */ + protected $comments; + + /** + * @param string $name The function name. + * @param int $line The line where the function call starts. + */ + public function __construct( $name, $line ) { + $this->name = $name; + $this->line = $line; + } + + /** + * Stops extracting strings for the current argument. + * + * @return void + */ + public function stopArgument() { + if ( -1 === $this->argument_index ) { + $this->argument_index = 0; + } + + $this->argument_stopped = true; + } + + /** + * Moves on to the next argument. + * + * @return void + */ + public function nextArgument() { + if ( -1 === $this->argument_index ) { + $this->argument_index = 1; + } else { + ++$this->argument_index; + } + + $this->argument_stopped = false; + } + + /** + * Adds a string to the current argument. + * + * @param string|null $chunk Part of the argument. + * @return void + */ + public function addArgumentChunk( $chunk ) { + if ( $this->argument_stopped ) { + return; + } + + if ( -1 === $this->argument_index ) { + $this->argument_index = 0; + } + + if ( isset( $this->arguments[ $this->argument_index ] ) ) { + $this->arguments[ $this->argument_index ] .= $chunk; + } else { + $this->arguments[ $this->argument_index ] = $chunk; + } + } + + /** + * Adds a comment related to this function call. + * + * @param ParsedComment $comment The comment. + * @return void + */ + public function addComment( ParsedComment $comment ) { + if ( null === $this->comments ) { + $this->comments = []; + } + + $this->comments[] = $comment; + } + + /** + * @return int + */ + public function getLine() { + return $this->line; + } + + /** + * Returns the function call once all its arguments have been read. + * + * @return array{0: string, 1: int, 2: array, 3: ParsedComment[]|null} Name, line, arguments and comments. + */ + public function close() { + $arguments = []; + + for ( $i = 0; $i <= $this->argument_index; ++$i ) { + $arguments[ $i ] = isset( $this->arguments[ $i ] ) ? $this->arguments[ $i ] : null; + } + + return [ + $this->name, + $this->line, + $arguments, + $this->comments, + ]; + } +} diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 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..4434201 100644 --- a/src/PhpCodeExtractor.php +++ b/src/PhpCodeExtractor.php @@ -3,11 +3,9 @@ namespace WP_CLI\I18n; use Exception; -use Gettext\Extractors\PhpCode; -use Gettext\Translations; use WP_CLI; -final class PhpCodeExtractor extends PhpCode { +final class PhpCodeExtractor { use IterableCodeExtractor; /** @@ -47,16 +45,11 @@ final class PhpCodeExtractor extends PhpCode { ]; /** - * @var string - */ - protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner'; - - /** - * {@inheritdoc} + * Extracts translations from PHP code. * - * @param string $text - * @param Translations $translations - * @param array $options + * @param string $text The code to extract strings from. + * @param Translations $translations Translations instance. + * @param array $options Extraction options. * @return void */ public static function fromString( $text, Translations $translations, array $options = [] ) { @@ -67,7 +60,7 @@ public static function fromString( $text, Translations $translations, array $opt WP_CLI::debug( "Parsing file {$file}", 'make-pot' ); try { - self::fromStringMultiple( $text, [ $translations ], $options ); + self::scan( $text, $translations, $options + self::$options ); } catch ( Exception $exception ) { WP_CLI::debug( sprintf( @@ -79,4 +72,24 @@ public static function fromString( $text, Translations $translations, array $opt ); } } + + /** + * Scans PHP code for the configured functions and adds the strings found. + * + * @param string $text The code to extract strings from. + * @param Translations $translations Translations instance. + * @param array $options Extraction options. + * @return void + */ + public static function scan( $text, Translations $translations, array $options ) { + $scanner = new PhpFunctionsScanner( $text ); + + $extract_comments = $options['extractComments'] ?? false; + if ( is_string( $extract_comments ) || is_array( $extract_comments ) ) { + /** @var string|array $extract_comments */ + $scanner->enableCommentsExtraction( $extract_comments ); + } + + $scanner->saveGettextFunctions( $translations, $options ); + } } diff --git a/src/PhpFunctionsScanner.php b/src/PhpFunctionsScanner.php index e1bf9e6..9fee962 100644 --- a/src/PhpFunctionsScanner.php +++ b/src/PhpFunctionsScanner.php @@ -2,24 +2,296 @@ namespace WP_CLI\I18n; -use Gettext\Utils\PhpFunctionsScanner as GettextPhpFunctionsScanner; +/** + * Finds function calls and their string arguments in PHP code. + * + * Based on the PHP functions scanner of gettext/gettext 4.x. + */ +class PhpFunctionsScanner { -class PhpFunctionsScanner extends GettextPhpFunctionsScanner { + /** + * PHP tokens of the code to be parsed. + * + * @var array + */ + protected $tokens; /** - * {@inheritdoc} + * If not false, comments will be extracted. * - * @param \Gettext\Translations $translations Translations instance. - * @param array $options Options. + * @var string|false|array + */ + protected $extract_comments = false; + + /** + * @param string $code The PHP code to scan. + */ + public function __construct( $code ) { + $this->tokens = array_values( + array_filter( + token_get_all( $code ), + static function ( $token ) { + return ! is_array( $token ) || T_WHITESPACE !== $token[0]; + } + ) + ); + } + + /** + * Enable extracting comments that start with a tag (if $tag is empty all the comments will be extracted). + * + * @param string|array $tag Tag to extract. * @return void */ - public function saveGettextFunctions( $translations, array $options ) { - // Ignore multiple translations for now. - // @todo Add proper support for multiple translations. - if ( is_array( $translations ) ) { - $translations = $translations[0]; + public function enableCommentsExtraction( $tag = '' ) { + $this->extract_comments = $tag; + } + + /** + * Disable comments extraction. + * + * @return void + */ + public function disableCommentsExtraction() { + $this->extract_comments = false; + } + + /** + * Scans the code and returns the function calls and their arguments. + * + * @param array $constants Constants used in the code to replace. + * @return array, 3: ParsedComment[]|null}> List of name, line, arguments and comments. + */ + public function getFunctions( array $constants = [] ) { + $count = count( $this->tokens ); + + /** @var ParsedFunction[] $buffer_functions */ + $buffer_functions = []; + + /** @var ParsedComment[] $buffer_comments */ + $buffer_comments = []; + + $functions = []; + + for ( $k = 0; $k < $count; ++$k ) { + $value = $this->tokens[ $k ]; + + if ( is_string( $value ) ) { + if ( isset( $buffer_functions[0] ) ) { + switch ( $value ) { + case ',': + $buffer_functions[0]->nextArgument(); + break; + + case ')': + $functions[] = $buffer_functions[0]->close(); + array_shift( $buffer_functions ); + break; + + case '.': + break; + + default: + $buffer_functions[0]->stopArgument(); + break; + } + } + + continue; + } + + // phpcs:ignore PHPCompatibility.Constants.NewConstants.t_name_fully_qualifiedFound + if ( defined( 'T_NAME_FULLY_QUALIFIED' ) && T_NAME_FULLY_QUALIFIED === $value[0] ) { + $value[0] = T_STRING; + $value[1] = ltrim( $value[1], '\\' ); + } + + switch ( $value[0] ) { + case T_CONSTANT_ENCAPSED_STRING: + // Add an argument to the current function. + if ( isset( $buffer_functions[0] ) ) { + $buffer_functions[0]->addArgumentChunk( self::convertString( $value[1] ) ); + } + break; + + case T_STRING: + if ( isset( $buffer_functions[0] ) ) { + if ( isset( $constants[ $value[1] ] ) ) { + $buffer_functions[0]->addArgumentChunk( $constants[ $value[1] ] ); + break; + } + + if ( 'null' === strtolower( $value[1] ) ) { + $buffer_functions[0]->addArgumentChunk( null ); + break; + } + + $buffer_functions[0]->stopArgument(); + } + + // New function found. + for ( $j = $k + 1; $j < $count; ++$j ) { + $next_token = $this->tokens[ $j ]; + + if ( is_array( $next_token ) && T_COMMENT === $next_token[0] ) { + continue; + } + + if ( '(' === $next_token ) { + $new_function = new ParsedFunction( $value[1], $value[2] ); + + // Add comment that was on the line before. + if ( isset( $buffer_comments[0] ) && $buffer_comments[0]->isRelatedWith( $new_function ) ) { + $new_function->addComment( $buffer_comments[0] ); + } + + array_unshift( $buffer_functions, $new_function ); + $k = $j; + } + + break; + } + break; + + case T_COMMENT: + $comment = $this->parsePhpComment( $value[1], $value[2] ); + + if ( $comment ) { + array_unshift( $buffer_comments, $comment ); + + // The comment is inside the function call. + if ( isset( $buffer_functions[0] ) ) { + $buffer_functions[0]->addComment( $comment ); + } + } + break; + + default: + if ( isset( $buffer_functions[0] ) ) { + $buffer_functions[0]->stopArgument(); + } + break; + } + } + + return $functions; + } + + /** + * Extracts the actual text from a PHP comment. + * + * @param string $value The PHP comment. + * @param int $line Line number. + * @return ParsedComment|null Comment or null if comment extraction is disabled or if there is a prefix mismatch. + */ + protected function parsePhpComment( $value, $line ) { + if ( false === $this->extract_comments ) { + return null; } + $comment = ParsedComment::create( $value, $line ); + $prefixes = array_filter( (array) $this->extract_comments ); + + return $comment->checkPrefixes( $prefixes ) ? $comment : null; + } + + /** + * Decodes a T_CONSTANT_ENCAPSED_STRING string. + * + * @param string $value String literal including its quotes. + * @return string + */ + public static function convertString( $value ) { + if ( false === strpos( $value, '\\' ) ) { + return substr( $value, 1, -1 ); + } + + if ( "'" === $value[0] ) { + return strtr( + substr( $value, 1, -1 ), + [ + '\\\\' => '\\', + '\\\'' => '\'', + ] + ); + } + + $value = substr( $value, 1, -1 ); + + return (string) preg_replace_callback( + '/\\\(n|r|t|v|e|f|\$|"|\\\|x[0-9A-Fa-f]{1,2}|u{[0-9a-f]{1,6}}|[0-7]{1,3})/', + static function ( $matches ) { + switch ( $matches[1][0] ) { + case 'n': + return "\n"; + case 'r': + return "\r"; + case 't': + return "\t"; + case 'v': + return "\v"; + case 'e': + return "\e"; + case 'f': + return "\f"; + case '$': + return '$'; + case '"': + return '"'; + case '\\': + return '\\'; + case 'x': + return chr( (int) hexdec( substr( $matches[1], 1 ) ) & 0xFF ); + case 'u': + return (string) self::unicodeChar( (int) hexdec( trim( substr( $matches[1], 1 ), '{}' ) ) ); + default: + return chr( (int) octdec( $matches[1] ) & 0xFF ); + } + }, + $value + ); + } + + /** + * Returns the UTF-8 encoded character for a Unicode code point. + * + * @param int $dec Code point. + * @return string|null + */ + protected static function unicodeChar( $dec ) { + if ( $dec < 0x80 ) { + return chr( $dec & 0x7F ); + } + + if ( $dec < 0x0800 ) { + return chr( 0xC0 + ( $dec >> 6 ) ) + . chr( 0x80 + ( $dec & 0x3f ) ); + } + + if ( $dec < 0x010000 ) { + return chr( 0xE0 + ( $dec >> 12 ) ) + . chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) ) + . chr( 0x80 + ( $dec & 0x3f ) ); + } + + if ( $dec < 0x200000 ) { + return chr( 0xF0 + ( $dec >> 18 ) ) + . chr( 0x80 + ( ( $dec >> 12 ) & 0x3f ) ) + . chr( 0x80 + ( ( $dec >> 6 ) & 0x3f ) ) + . chr( 0x80 + ( $dec & 0x3f ) ); + } + + return null; + } + + /** + * Searches for specific functions and creates translations. + * + * @param Translations $translations Translations instance. + * @param array $options Options. + * @return void + */ + public function saveGettextFunctions( Translations $translations, array $options ) { $functions = isset( $options['functions'] ) ? $options['functions'] : []; if ( ! is_array( $functions ) ) { $functions = []; @@ -32,10 +304,8 @@ public function saveGettextFunctions( $translations, array $options ) { $add_reference = ! empty( $options['addReferences'] ); - $constants = isset( $options['constants'] ) ? $options['constants'] : []; - if ( ! is_array( $constants ) ) { - $constants = []; - } + /** @var array $constants */ + $constants = isset( $options['constants'] ) && is_array( $options['constants'] ) ? $options['constants'] : []; foreach ( $this->getFunctions( $constants ) as $function ) { list( $name, $line, $args ) = $function; @@ -100,24 +370,18 @@ public function saveGettextFunctions( $translations, array $options ) { $translation = $translations->insert( $context, $original_str, $plural ); if ( $add_reference ) { - $translation = $translation->addReference( $file, $line ); + $translation->getReferences()->add( $file, $line ); } if ( 1 === preg_match( MakePotCommand::SPRINTF_PLACEHOLDER_REGEX, $original_str ) || 1 === preg_match( MakePotCommand::UNORDERED_SPRINTF_PLACEHOLDER_REGEX, $original_str ) ) { - $translation->addFlag( 'php-format' ); + $translation->getFlags()->add( 'php-format' ); } - if ( isset( $function[3] ) && is_array( $function[3] ) ) { - foreach ( $function[3] as $extracted_comment ) { - if ( is_scalar( $extracted_comment ) ) { - $translation = $translation->addExtractedComment( (string) $extracted_comment ); - } elseif ( is_object( $extracted_comment ) && method_exists( $extracted_comment, 'getComment' ) ) { - $translation = $translation->addExtractedComment( $extracted_comment->getComment() ); - } - } + foreach ( $function[3] ?? [] as $extracted_comment ) { + $translation->getExtractedComments()->add( $extracted_comment->getComment() ); } } } diff --git a/src/PoGenerator.php b/src/PoGenerator.php new file mode 100644 index 0000000..488f922 --- /dev/null +++ b/src/PoGenerator.php @@ -0,0 +1,164 @@ + $options + * @return string + */ + public static function toString( Translations $translations, array $options = [] ) { + $plural_form = $translations->getHeaders()->getPluralForm(); + $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : null; + + $lines = static::getHeaderLines( $translations ); + + foreach ( $translations as $translation ) { + /** @var Translation $translation */ + + $prefix = static::addMetaLines( $lines, $translation ); + + if ( null !== $translation->getPlural() && '' !== $translation->getPlural() ) { + static::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); + static::addLines( $lines, $prefix . 'msgstr[0]', (string) $translation->getTranslation() ); + + foreach ( $translation->getPluralTranslations( $plural_size ) as $k => $v ) { + static::addLines( $lines, $prefix . 'msgstr[' . ( $k + 1 ) . ']', $v ); + } + } else { + static::addLines( $lines, $prefix . 'msgstr', (string) $translation->getTranslation() ); + } + + $lines[] = ''; + } + + return implode( "\n", $lines ); + } + + /** + * Returns the lines for the header entry. + * + * @param Translations $translations Translations instance. + * @return array + */ + protected static function getHeaderLines( Translations $translations ) { + $lines = [ 'msgid ""', 'msgstr ""' ]; + + foreach ( $translations->getHeaders()->toArray() as $name => $value ) { + $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); + } + + $lines[] = ''; + + return $lines; + } + + /** + * Adds the comments, references, flags, context and original string of an entry. + * + * @param array &$lines Array lines should be added to. + * @param Translation $translation Translation entry. + * @return string Prefix to use for the remaining lines of the entry. + */ + protected static function addMetaLines( array &$lines, Translation $translation ) { + foreach ( $translation->getComments()->toArray() as $comment ) { + $lines[] = '# ' . $comment; + } + + foreach ( $translation->getExtractedComments()->toArray() as $comment ) { + $lines[] = '#. ' . $comment; + } + + foreach ( \WP_CLI\I18n\Translation::flattenReferences( $translation->getReferences() ) as $reference ) { + $lines[] = '#: ' . $reference[0] . ( null !== $reference[1] ? ':' . $reference[1] : '' ); + } + + if ( count( $translation->getFlags() ) > 0 ) { + $lines[] = '#, ' . implode( ',', $translation->getFlags()->toArray() ); + } + + $prefix = $translation->isDisabled() ? '#~ ' : ''; + + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { + $lines[] = $prefix . 'msgctxt ' . static::convertString( $translation->getContext() ); + } + + static::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); + + return $prefix; + } + + /** + * Escapes and adds double quotes to a string. + * + * @param string $text Multiline string. + * + * @return string[] + */ + protected static function multilineQuote( $text ) { + $lines = explode( "\n", $text ); + $last = count( $lines ) - 1; + + foreach ( $lines as $k => $line ) { + if ( $k === $last ) { + $lines[ $k ] = static::convertString( $line ); + } else { + $lines[ $k ] = static::convertString( $line . "\n" ); + } + } + + return $lines; + } + + /** + * Add one or more lines depending whether the string is multiline or not. + * + * @param array &$lines Array lines should be added to. + * @param string $name Name of the line, e.g. msgstr or msgid_plural. + * @param string $value The line to add. + * @return void + */ + protected static function addLines( array &$lines, $name, $value ) { + $newlines = static::multilineQuote( $value ); + + if ( count( $newlines ) === 1 ) { + $lines[] = $name . ' ' . $newlines[0]; + } else { + $lines[] = $name . ' ""'; + + foreach ( $newlines as $line ) { + $lines[] = $line; + } + } + } + + /** + * Converts a string to its PO representation. + * + * @param string $value String to convert. + * @return string + */ + public static function convertString( $value ) { + return '"' . strtr( + $value, + [ + "\x00" => '', + '\\' => '\\\\', + "\t" => '\t', + "\r" => '\r', + "\n" => '\n', + '"' => '\\"', + ] + ) . '"'; + } +} diff --git a/src/PoLoader.php b/src/PoLoader.php new file mode 100644 index 0000000..4b5a38d --- /dev/null +++ b/src/PoLoader.php @@ -0,0 +1,250 @@ + $files A file path or a list of file paths. + * @return Translations + */ + public static function fromFiles( $files ): Translations { + $loader = new self(); + $translations = Translations::create(); + + foreach ( (array) $files as $file ) { + $loader->loadFile( $file, $translations ); + } + + return $translations; + } + + public function loadString( string $text, ?GettextTranslations $translations = null ): GettextTranslations { + $translations = parent::loadString( $text, $translations ); + + $lines = explode( "\n", $text ); + $count = count( $lines ); + $translation = Translation::create( null, '' ); + + for ( $i = 0; $i < $count; ++$i ) { + $line = trim( $lines[ $i ] ); + $line = self::fixMultiLines( $line, $lines, $i ); + + // Treat empty comments as empty lines. + if ( '#' === $line ) { + $line = ''; + } + + if ( '' === $line ) { + self::addTranslation( $translation, $translations ); + $translation = Translation::create( null, '' ); + continue; + } + + $split_line = preg_split( '/\s+/', $line, 2 ) ?: []; + $key = $split_line[0] ?? ''; + $data = $split_line[1] ?? ''; + + if ( '#~' === $key ) { + $translation->disable(); + + $split_line = preg_split( '/\s+/', $data, 2 ) ?: []; + $key = $split_line[0] ?? ''; + $data = $split_line[1] ?? ''; + } + + switch ( $key ) { + case '#': + $translation->getComments()->add( $data ); + break; + + case '#.': + $translation->getExtractedComments()->add( $data ); + break; + + case '#,': + foreach ( array_map( 'trim', explode( ',', trim( $data ) ) ) as $flag ) { + $translation->getFlags()->add( $flag ); + } + break; + + case '#:': + foreach ( preg_split( '/\s+/', trim( $data ) ) ?: [] as $reference ) { + if ( preg_match( '/^(.+)(:(\d*))?$/U', $reference, $matches ) ) { + $translation->getReferences()->add( + $matches[1], + isset( $matches[3] ) && '' !== $matches[3] ? (int) $matches[3] : null + ); + } + } + break; + + case 'msgctxt': + $translation = $translation->withContext( self::decode( $data ) ); + break; + + case 'msgid': + $translation = $translation->withOriginal( self::decode( $data ) ); + break; + + case 'msgid_plural': + $translation->setPlural( self::decode( $data ) ); + break; + + case 'msgstr': + case 'msgstr[0]': + $translation->translate( self::decode( $data ) ); + break; + + case 'msgstr[1]': + $translation->translatePlural( self::decode( $data ) ); + break; + + default: + if ( 0 === strpos( $key, 'msgstr[' ) ) { + $plural_translations = $translation->getPluralTranslations(); + $plural_translations[] = self::decode( $data ); + $translation->translatePlural( ...$plural_translations ); + } + break; + } + } + + self::addTranslation( $translation, $translations ); + + return $translations; + } + + protected function createTranslations(): GettextTranslations { + return Translations::create(); + } + + /** + * Adds a parsed entry to the translations. + * + * The entry with an empty original string and no context holds the headers. + * + * @param GettextTranslation $translation The parsed entry. + * @param GettextTranslations $translations Translations instance to add the entry to. + * @return void + */ + private static function addTranslation( GettextTranslation $translation, GettextTranslations $translations ) { + if ( '' !== $translation->getOriginal() ) { + $translations->addOrMerge( $translation ); + return; + } + + if ( null !== $translation->getContext() && '' !== $translation->getContext() ) { + return; + } + + self::extractHeaders( (string) $translation->getTranslation(), $translations ); + } + + /** + * Adds the headers found in the header entry to the translations. + * + * @param string $headers Translation of the header entry. + * @param GettextTranslations $translations Translations instance. + * @return void + */ + private static function extractHeaders( $headers, GettextTranslations $translations ) { + $current_header = null; + + foreach ( explode( "\n", $headers ) as $line ) { + $line = self::decode( $line ); + + if ( '' === $line ) { + continue; + } + + // A header definition, as opposed to the continuation of the previous header. + if ( preg_match( '/^[\w-]+:/', $line ) ) { + list( $current_header, $value ) = array_map( 'trim', explode( ':', $line, 2 ) ); + $translations->getHeaders()->set( $current_header, $value ); + continue; + } + + if ( null !== $current_header ) { + $translations->getHeaders()->set( + $current_header, + $translations->getHeaders()->get( $current_header ) . $line + ); + } + } + } + + /** + * Gets one string from multiline strings. + * + * @param string $line Current line. + * @param array $lines All lines. + * @param int $i Index of the current line, moved forward to the last line that was consumed. + * @return string + */ + private static function fixMultiLines( $line, array $lines, &$i ) { + for ( $j = $i, $t = count( $lines ); $j < $t; ++$j ) { + if ( '"' === substr( $line, -1, 1 ) && isset( $lines[ $j + 1 ] ) ) { + $next_line = trim( $lines[ $j + 1 ] ); + + if ( '"' === substr( $next_line, 0, 1 ) ) { + $line = substr( $line, 0, -1 ) . substr( $next_line, 1 ); + continue; + } + + if ( '#~ "' === substr( $next_line, 0, 4 ) ) { + $line = substr( $line, 0, -1 ) . substr( $next_line, 4 ); + continue; + } + } + + $i = $j; + break; + } + + return $line; + } + + /** + * Converts a string from its PO representation. + * + * @param string $value String as found in a PO file. + * @return string + */ + public static function decode( $value ) { + if ( ! $value ) { + return ''; + } + + if ( '"' === $value[0] ) { + $value = substr( $value, 1, -1 ); + } + + return strtr( + $value, + [ + '\\\\' => '\\', + '\\a' => "\x07", + '\\b' => "\x08", + '\\t' => "\t", + '\\n' => "\n", + '\\v' => "\x0b", + '\\f' => "\x0c", + '\\r' => "\r", + '\\"' => '"', + ] + ); + } +} diff --git a/src/PotGenerator.php b/src/PotGenerator.php index e2a1dfc..0a4f2fb 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -2,15 +2,14 @@ namespace WP_CLI\I18n; -use Gettext\Generators\Po as PoGenerator; +use Gettext\Translation; use Gettext\Translations; -use Gettext\Utils\ParsedComment; /** * POT file generator. * - * The only difference to the existing PO file generator is that this - * adds some comments at the very beginning of the file. + * The only difference to the PO file generator is that this adds some + * comments at the very beginning of the file and leaves plural forms untranslated. */ class PotGenerator extends PoGenerator { /** @@ -39,135 +38,34 @@ public static function setCommentBeforeHeaders( $comment ) { /** * {@inheritdoc} * - * @param \Gettext\Translations $translations - * @param array $options + * @param Translations $translations + * @param array $options * @return string */ public static function toString( Translations $translations, array $options = [] ) { - /** @var array $lines */ - $lines = static::$comments_before_headers; - $lines[] = 'msgid ""'; - $lines[] = 'msgstr ""'; + $plural_form = $translations->getHeaders()->getPluralForm(); + $plural_size = is_array( $plural_form ) ? ( (int) $plural_form[0] - 1 ) : 1; - $plural_form = $translations->getPluralForms(); - $plural_size = 1; - if ( is_array( $plural_form ) && isset( $plural_form[0] ) && is_numeric( $plural_form[0] ) ) { - $plural_size = (int) $plural_form[0] - 1; - } - - foreach ( $translations->getHeaders() as $name => $value ) { - $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); - } - - $lines[] = ''; + $lines = array_merge( static::$comments_before_headers, static::getHeaderLines( $translations ) ); foreach ( $translations as $translation ) { - /** @var \Gettext\Translation $translation */ - if ( $translation->hasComments() ) { - foreach ( $translation->getComments() as $comment ) { - if ( is_scalar( $comment ) ) { - $lines[] = '# ' . $comment; - } - } - } - - if ( $translation->hasExtractedComments() ) { - $unique_comments = array(); - - /** @var ParsedComment|string $comment */ - foreach ( $translation->getExtractedComments() as $comment ) { - $comment = ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ); - if ( is_scalar( $comment ) ) { - $comment_str = (string) $comment; - if ( ! in_array( $comment_str, $unique_comments, true ) ) { - $lines[] = '#. ' . $comment_str; - $unique_comments[] = $comment_str; - } - } - } - } + /** @var Translation $translation */ - foreach ( $translation->getReferences() as $reference ) { - if ( is_array( $reference ) && isset( $reference[0] ) ) { - $ref_str = is_scalar( $reference[0] ) ? (string) $reference[0] : ''; - $line_str = ''; - if ( isset( $reference[1] ) && is_scalar( $reference[1] ) ) { - $line_str = ':' . $reference[1]; - } - $lines[] = '#: ' . $ref_str . $line_str; - } - } - - if ( $translation->hasFlags() ) { - $lines[] = '#, ' . implode( ',', $translation->getFlags() ); - } - - $prefix = $translation->isDisabled() ? '#~ ' : ''; + $prefix = static::addMetaLines( $lines, $translation ); - if ( $translation->hasContext() ) { - $lines[] = $prefix . 'msgctxt ' . self::convertString( $translation->getContext() ); - } - - self::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); - - if ( $translation->hasPlural() ) { - self::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); + if ( null !== $translation->getPlural() && '' !== $translation->getPlural() ) { + static::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); for ( $i = 0; $i <= $plural_size; $i++ ) { - self::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' ); + static::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' ); } } else { - self::addLines( $lines, $prefix . 'msgstr', $translation->getTranslation() ); + static::addLines( $lines, $prefix . 'msgstr', (string) $translation->getTranslation() ); } $lines[] = ''; } - $string_lines = array_filter( $lines, 'is_string' ); - return implode( "\n", $string_lines ); - } - - /** - * Escapes and adds double quotes to a string. - * - * @param string $text Multiline string. - * - * @return string[] - */ - protected static function wpMultilineQuote( $text ) { - $lines = explode( "\n", $text ); - $last = count( $lines ) - 1; - - foreach ( $lines as $k => $line ) { - if ( $k === $last ) { - $lines[ $k ] = self::convertString( $line ); - } else { - $lines[ $k ] = self::convertString( $line . "\n" ); - } - } - - return $lines; - } - - /** - * Add one or more lines depending whether the string is multiline or not. - * - * @param array &$lines Array lines should be added to. - * @param string $name Name of the line, e.g. msgstr or msgid_plural. - * @param string $value The line to add. - * @return void - */ - protected static function addLines( array &$lines, $name, $value ) { - $newlines = self::wpMultilineQuote( $value ); - - if ( count( $newlines ) === 1 ) { - $lines[] = $name . ' ' . $newlines[0]; - } else { - $lines[] = $name . ' ""'; - - foreach ( $newlines as $line ) { - $lines[] = $line; - } - } + return implode( "\n", $lines ); } } diff --git a/src/Translation.php b/src/Translation.php new file mode 100644 index 0000000..6efb405 --- /dev/null +++ b/src/Translation.php @@ -0,0 +1,109 @@ +context && '' !== $this->context; + } + + public function hasPlural(): bool { + return null !== $this->plural && '' !== $this->plural; + } + + public function hasTranslation(): bool { + return null !== $this->translation && '' !== $this->translation; + } + + /** + * @param bool $check_content Whether to check that at least one plural translation is not empty. + */ + public function hasPluralTranslations( bool $check_content = false ): bool { + if ( $check_content ) { + return '' !== implode( '', $this->getPluralTranslations() ); + } + + return ! empty( $this->getPluralTranslations() ); + } + + /** + * Returns the references as a flat list. + * + * @return array List of file name and line number pairs. + */ + public function getReferenceEntries(): array { + return self::flattenReferences( $this->getReferences() ); + } + + /** + * Turns references grouped by file name into a flat list. + * + * @param References $references References instance. + * @return array List of file name and line number pairs. + */ + public static function flattenReferences( References $references ): array { + $entries = []; + + foreach ( $references->toArray() as $filename => $lines ) { + if ( empty( $lines ) ) { + $entries[] = [ (string) $filename, null ]; + continue; + } + + foreach ( $lines as $line ) { + $entries[] = [ (string) $filename, $line ]; + } + } + + return $entries; + } + + /** + * {@inheritdoc} + * + * Unlike the parent method, this keeps translations and plurals that are "0", + * and does not change whether the translation is disabled. + */ + public function mergeWith( GettextTranslation $translation, int $strategy = 0 ): GettextTranslation { + /** @var Translation $merged */ + $merged = parent::mergeWith( $translation, $strategy ); + $override = (bool) ( $strategy & Merge::TRANSLATIONS_OVERRIDE ); + + $their_translation = $translation->getTranslation(); + $their_plural = $translation->getPlural(); + + $merged->translation = $this->translation; + if ( ! $this->hasTranslation() || ( $override && null !== $their_translation && '' !== $their_translation ) ) { + $merged->translation = $their_translation; + } + + $merged->plural = $this->plural; + if ( ! $this->hasPlural() || ( $override && null !== $their_plural && '' !== $their_plural ) ) { + $merged->plural = $their_plural; + } + + $merged->disabled = $this->disabled; + + return $merged; + } +} diff --git a/src/Translations.php b/src/Translations.php new file mode 100644 index 0000000..43fb6c9 --- /dev/null +++ b/src/Translations.php @@ -0,0 +1,79 @@ + '', + '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; + } + + /** + * 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 { + $existing = $this->find( $context, $original ); + + if ( $existing instanceof Translation ) { + $translation = $existing; + } else { + $translation = Translation::create( $context, $original ); + + // Keep the data of an entry that was added as a plain gettext translation. + if ( null !== $existing ) { + /** @var Translation $translation */ + $translation = $translation->mergeWith( $existing ); + $translation->disable( $existing->isDisabled() ); + } + + $this->add( $translation ); + } + + if ( null !== $plural && '' !== $plural && ! $translation->hasPlural() ) { + $translation->setPlural( $plural ); + } + + return $translation; + } +} diff --git a/src/UpdatePoCommand.php b/src/UpdatePoCommand.php index c9e0a22..e982b84 100644 --- a/src/UpdatePoCommand.php +++ b/src/UpdatePoCommand.php @@ -3,8 +3,8 @@ namespace WP_CLI\I18n; use DirectoryIterator; +use Gettext\Headers; use Gettext\Merge; -use Gettext\Translations; use IteratorIterator; use SplFileInfo; use WP_CLI; @@ -82,18 +82,10 @@ public function __invoke( $args, $assoc_args ) { $files = new IteratorIterator( new DirectoryIterator( $destination ) ); } - $pot_translations = Translations::fromPoFile( $source ); - - // Build merge flags based on options - $merge_flags = Merge::ADD | Merge::EXTRACTED_COMMENTS_THEIRS | Merge::REFERENCES_THEIRS | Merge::DOMAIN_OVERRIDE; + $pot_translations = PoLoader::fromFiles( $source ); /** @var array $assoc_args */ - $purge = Utils\get_flag_value( $assoc_args, 'purge', true ); - - if ( $purge ) { - // By default, remove obsolete entries and replace translator comments - $merge_flags |= Merge::REMOVE | Merge::COMMENTS_THEIRS; - } + $purge = (bool) Utils\get_flag_value( $assoc_args, 'purge', true ); $updated_count = 0; $unchanged_count = 0; @@ -113,13 +105,8 @@ public function __invoke( $args, $assoc_args ) { if ( ! $purge ) { $file_comments = $this->extract_file_comments( $file->getPathname() ); } - $po_translations = Translations::fromPoFile( $file->getPathname() ); - $original_translations = clone $po_translations; - - $po_translations->mergeWith( - $pot_translations, - $merge_flags - ); + $original_translations = PoLoader::fromFiles( $file->getPathname() ); + $po_translations = $this->merge_translations( $original_translations, $pot_translations, $purge ); // Check if the translations actually changed by comparing the objects. $has_changes = $this->translations_differ( $original_translations, $po_translations ); @@ -131,12 +118,12 @@ public function __invoke( $args, $assoc_args ) { // Update PO-Revision-Date to current date and time in UTC. // Uses gmdate() for consistency across different server timezones. - $po_translations->setHeader( 'PO-Revision-Date', gmdate( 'Y-m-d\TH:i:sP' ) ); + $po_translations->getHeaders()->set( 'PO-Revision-Date', gmdate( 'Y-m-d\TH:i:sP' ) ); // Reorder translations to match POT file order. $ordered_translations = $this->reorder_translations( $po_translations, $pot_translations ); - if ( ! $ordered_translations->toPoFile( $file->getPathname() ) ) { + if ( ! PoGenerator::toFile( $ordered_translations, $file->getPathname() ) ) { WP_CLI::warning( sprintf( 'Could not update file %s', $file->getPathname() ) ); continue; } @@ -233,6 +220,43 @@ private function restore_file_comments( $file_path, $comments ) { return true; } + /** + * Merges the entries of a POT file into the translations of a PO file. + * + * @param Translations $po_translations Translations of the PO file. + * @param Translations $pot_translations Translations of the POT file. + * @param bool $purge Whether to remove obsolete entries and replace translator comments. + * @return Translations The merged translations. + */ + private function merge_translations( Translations $po_translations, Translations $pot_translations, $purge ) { + $merged = clone $po_translations; + + // The text domain is taken from the POT file if both files have one. + $pot_domain = $pot_translations->getDomain(); + if ( ! empty( $merged->getDomain() ) && ! empty( $pot_domain ) ) { + $merged->getHeaders()->set( Headers::HEADER_DOMAIN, $pot_domain ); + } + + $strategy = Merge::EXTRACTED_COMMENTS_THEIRS | Merge::REFERENCES_THEIRS; + + if ( $purge ) { + $strategy |= Merge::COMMENTS_THEIRS; + + foreach ( $merged->getTranslations() as $po_entry ) { + if ( ! $pot_translations->has( $po_entry ) ) { + $merged->remove( $po_entry ); + } + } + } + + foreach ( $pot_translations as $pot_entry ) { + /** @var Translation $pot_entry */ + $merged->addOrMerge( clone $pot_entry, $strategy ); + } + + return $merged; + } + /** * Check if two Translations objects differ. * @@ -248,7 +272,7 @@ private function translations_differ( Translations $original, Translations $upda // Compare each translation entry. foreach ( $original as $translation ) { - /** @var \Gettext\Translation $translation */ + /** @var Translation $translation */ $context = $translation->getContext(); $original_str = $translation->getOriginal(); @@ -274,8 +298,8 @@ private function translations_differ( Translations $original, Translations $upda } // Compare references (source code locations). - $original_refs = $translation->getReferences(); - $updated_refs = $updated_translation->getReferences(); + $original_refs = Translation::flattenReferences( $translation->getReferences() ); + $updated_refs = Translation::flattenReferences( $updated_translation->getReferences() ); $original_refs_sorted = $original_refs; $updated_refs_sorted = $updated_refs; @@ -288,11 +312,11 @@ private function translations_differ( Translations $original, Translations $upda } // Compare comments. - if ( $translation->getExtractedComments() !== $updated_translation->getExtractedComments() ) { + if ( $translation->getExtractedComments()->toArray() !== $updated_translation->getExtractedComments()->toArray() ) { return true; } - if ( $translation->getComments() !== $updated_translation->getComments() ) { + if ( $translation->getComments()->toArray() !== $updated_translation->getComments()->toArray() ) { return true; } } @@ -309,28 +333,28 @@ private function translations_differ( Translations $original, Translations $upda * @return Translations Translations object with entries in POT file order. */ private function reorder_translations( Translations $po_translations, Translations $pot_translations ) { - $ordered = new Translations(); + $ordered = Translations::create(); // Copy headers from the merged PO translations. - foreach ( $po_translations->getHeaders() as $name => $value ) { - $ordered->setHeader( $name, $value ); + foreach ( $po_translations->getHeaders()->toArray() as $name => $value ) { + $ordered->getHeaders()->set( $name, $value ); } // Add translations in POT file order. foreach ( $pot_translations as $pot_entry ) { - /** @var \Gettext\Translation $pot_entry */ - $po_entry = $po_translations->find( $pot_entry ); + /** @var Translation $pot_entry */ + $po_entry = $po_translations->find( $pot_entry->getContext(), $pot_entry->getOriginal() ); if ( $po_entry ) { - $ordered[] = $po_entry->getClone(); + $ordered->add( clone $po_entry ); } } // Add any remaining translations from PO that aren't in POT (e.g., obsolete/disabled translations). foreach ( $po_translations as $po_entry ) { - /** @var \Gettext\Translation $po_entry */ + /** @var Translation $po_entry */ // Check if this entry is already in the ordered set. - if ( ! $ordered->find( $po_entry ) ) { - $ordered[] = $po_entry->getClone(); + if ( ! $ordered->has( $po_entry ) ) { + $ordered->add( clone $po_entry ); } } diff --git a/stubs/Gettext.stub b/stubs/Gettext.stub deleted file mode 100644 index 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..a0d51a5 --- /dev/null +++ b/tests/PhpFunctionsScannerTest.php @@ -0,0 +1,106 @@ +assertSame( $expected, PhpFunctionsScanner::convertString( $literal ) ); + } + + /** + * @return array + */ + public static function data_convert_string() { + return [ + 'single quoted' => [ "'Hello'", 'Hello' ], + 'double quoted' => [ '"Hello"', 'Hello' ], + 'escaped single quote' => [ "'It\\'s'", "It's" ], + 'single quoted backslashes' => [ "'a\\\\b\\nc'", 'a\\b\\nc' ], + 'double quoted escapes' => [ '"a\\nb\\tc\\\\d\\"e\\$f"', "a\nb\tc\\d\"e\$f" ], + 'hexadecimal and octal' => [ '"\\x41\\101"', 'AA' ], + 'unicode code point' => [ '"\\u{e9}\\u{1f600}"', "\u{e9}\u{1f600}" ], + 'unknown escape is kept' => [ '"100\\%"', '100\\%' ], + ]; + } + + public function test_finds_functions_with_arguments_and_comments() { + $code = <<<'PHP' +enableCommentsExtraction( [ 'translators' ] ); + + $functions = []; + foreach ( $scanner->getFunctions() as $function ) { + $comments = []; + foreach ( $function[3] ?? [] as $comment ) { + $comments[] = $comment->getComment(); + } + + $functions[] = [ $function[0], $function[1], $function[2], $comments ]; + } + + $this->assertSame( + [ + [ '__', 3, [ 'Hello %s', 'foo' ], [ 'translators: %s: Name' ] ], + [ 'printf', 3, [ null, null ], [ 'translators: %s: Name' ] ], + [ '_x', 4, [ 'Concat', 'context', 'foo' ], [] ], + [ '__', 5, [ 'With ', 'foo' ], [] ], + [ 'esc_html__', 6, [ 'inner', 'foo' ], [] ], + [ '__', 6, [ 'Nested ', null ], [] ], + ], + $functions + ); + } + + public function test_extracts_strings_for_text_domain() { + $code = <<<'PHP' + 'foo-plugin.php', + 'addReferences' => true, + ] + ); + + $this->assertCount( 2, $translations ); + $this->assertNotNull( $translations->find( null, '0' ) ); + + $translation = $translations->find( null, '%d item' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( '%d items', $translation->getPlural() ); + $this->assertSame( [ 'php-format' ], $translation->getFlags()->toArray() ); + $this->assertSame( [ 'translators: %d: Number of items.' ], $translation->getExtractedComments()->toArray() ); + $this->assertSame( [ [ 'foo-plugin.php', 3 ] ], $translation->getReferenceEntries() ); + } +} diff --git a/tests/PoLoaderTest.php b/tests/PoLoaderTest.php new file mode 100644 index 0000000..66f020b --- /dev/null +++ b/tests/PoLoaderTest.php @@ -0,0 +1,191 @@ +loadString( $po ); + + $this->assertInstanceOf( Translations::class, $translations ); + + return $translations; + } + + public function test_keeps_strings_and_translations_that_are_zero() { + $po = <<<'PO' +msgid "" +msgstr "" +"Language: de\n" + +msgid "0" +msgstr "" + +msgctxt "start of week" +msgid "1" +msgstr "0" + +msgid "%d item" +msgid_plural "%d items" +msgstr[0] "0" +msgstr[1] "0" + +PO; + + $translations = $this->load( $po ); + + $this->assertCount( 3, $translations ); + $this->assertNotNull( $translations->find( null, '0' ) ); + + $translation = $translations->find( 'start of week', '1' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( '0', $translation->getTranslation() ); + + $result = PoGenerator::toString( $translations ); + + $this->assertStringContainsString( "msgid \"0\"\nmsgstr \"\"\n", $result ); + $this->assertStringContainsString( "msgid \"1\"\nmsgstr \"0\"\n", $result ); + $this->assertStringContainsString( "msgstr[0] \"0\"\nmsgstr[1] \"0\"\n", $result ); + } + + public function test_sorts_headers_and_flags_and_groups_references() { + $po = <<<'PO' +msgid "" +msgstr "" +"X-Generator: Poedit\n" +"Language: de\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"A-Custom-Header: multi " +"line\n" + +#: b.php:2 a.php:1 +#: b.php:1 +#: c.php +#, php-format, fuzzy,js-format +msgid "Hello %s" +msgstr "Hello %s (DE)" + +PO; + + $translations = $this->load( $po ); + + $headers = array_keys( $translations->getHeaders()->toArray() ); + + $this->assertSame( 'A-Custom-Header', $headers[0] ); + $this->assertSame( 'X-Generator', end( $headers ) ); + $this->assertSame( 'de', $translations->getLanguage() ); + $this->assertSame( 'multi line', $translations->getHeaders()->get( 'A-Custom-Header' ) ); + + $translation = $translations->find( null, 'Hello %s' ); + $this->assertInstanceOf( Translation::class, $translation ); + + $this->assertSame( [ 'fuzzy', 'js-format', 'php-format' ], $translation->getFlags()->toArray() ); + $this->assertSame( + [ + [ 'b.php', 2 ], + [ 'b.php', 1 ], + [ 'a.php', 1 ], + [ 'c.php', null ], + ], + $translation->getReferenceEntries() + ); + + $result = PoGenerator::toString( $translations ); + + $this->assertStringContainsString( "#: b.php:2\n#: b.php:1\n#: a.php:1\n#: c.php\n#, fuzzy,js-format,php-format\n", $result ); + } + + public function test_merges_duplicate_entries() { + $po = <<<'PO' +#: a.php:1 +msgid "Hello" +msgstr "" + +#. A comment +#: b.php:2 +msgid "Hello" +msgstr "Hello (DE)" + +PO; + + $translations = $this->load( $po ); + + $this->assertCount( 1, $translations ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertInstanceOf( Translation::class, $translation ); + + $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); + $this->assertSame( [ 'A comment' ], $translation->getExtractedComments()->toArray() ); + $this->assertCount( 2, $translation->getReferenceEntries() ); + } + + public function test_loads_multiline_and_disabled_entries() { + $po = <<<'PO' +msgctxt "context" +msgid "" +"First\n" +"second \"quoted\"" +msgstr "" +"First (DE)\n" +"second (DE)" + +#~ msgid "Obsolete " +#~ "string" +#~ msgstr "Obsolete (DE)" +PO; + + $translations = $this->load( $po ); + + $translation = $translations->find( 'context', "First\nsecond \"quoted\"" ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( "First (DE)\nsecond (DE)", $translation->getTranslation() ); + $this->assertFalse( $translation->isDisabled() ); + + $translation = $translations->find( null, 'Obsolete string' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( 'Obsolete (DE)', $translation->getTranslation() ); + $this->assertTrue( $translation->isDisabled() ); + + $result = PoGenerator::toString( $translations ); + + $this->assertStringContainsString( "msgctxt \"context\"\nmsgid \"\"\n\"First\\n\"\n\"second \\\"quoted\\\"\"\n", $result ); + $this->assertStringContainsString( "#~ msgid \"Obsolete string\"\n#~ msgstr \"Obsolete (DE)\"\n", $result ); + } + + public function test_loads_multiple_files_into_one_set() { + $dir = sys_get_temp_dir() . '/' . uniqid( 'wp-cli-i18n-test-', true ); + mkdir( $dir ); + + file_put_contents( "$dir/a.pot", "#: a.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n" ); + file_put_contents( "$dir/b.pot", "#: b.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n\nmsgid \"World\"\nmsgstr \"\"\n" ); + + $translations = PoLoader::fromFiles( [ "$dir/a.pot", "$dir/b.pot" ] ); + + unlink( "$dir/a.pot" ); + unlink( "$dir/b.pot" ); + rmdir( $dir ); + + $this->assertCount( 2, $translations ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertInstanceOf( Translation::class, $translation ); + $this->assertSame( + [ + [ 'a.php', 1 ], + [ 'b.php', 1 ], + ], + $translation->getReferenceEntries() + ); + } +} diff --git a/tests/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/TranslationTest.php b/tests/TranslationTest.php new file mode 100644 index 0000000..763f001 --- /dev/null +++ b/tests/TranslationTest.php @@ -0,0 +1,116 @@ +translate( '0' ); + $theirs = Translation::create( null, '1' ); + + $this->assertSame( '0', $ours->mergeWith( $theirs )->getTranslation() ); + $this->assertSame( '0', $theirs->mergeWith( $ours )->getTranslation() ); + + $theirs->translate( '7' ); + + $this->assertSame( '0', $ours->mergeWith( $theirs )->getTranslation() ); + $this->assertSame( '7', $ours->mergeWith( $theirs, Merge::TRANSLATIONS_OVERRIDE )->getTranslation() ); + $this->assertSame( '0', $theirs->mergeWith( $ours, Merge::TRANSLATIONS_OVERRIDE )->getTranslation() ); + } + + public function test_merge_keeps_disabled_state() { + $ours = Translation::create( null, 'Hello' )->disable(); + $theirs = Translation::create( null, 'Hello' ); + + $this->assertTrue( $ours->mergeWith( $theirs )->isDisabled() ); + $this->assertFalse( $theirs->mergeWith( $ours )->isDisabled() ); + } + + public function test_insert_returns_existing_translation() { + $translations = Translations::create(); + + $first = $translations->insert( 'context', 'Hello' ); + $first->getReferences()->add( 'a.php', 1 ); + + $second = $translations->insert( 'context', 'Hello', 'Hellos' ); + + $this->assertSame( $first, $second ); + $this->assertCount( 1, $translations ); + $this->assertSame( 'Hellos', $first->getPlural() ); + $this->assertTrue( $first->hasPlural() ); + $this->assertTrue( $first->hasContext() ); + $this->assertFalse( $first->hasTranslation() ); + } + + public function test_insert_keeps_data_of_plain_gettext_translation() { + $translations = Translations::create(); + + $plain = \Gettext\Translation::create( null, 'Hello' )->translate( 'Hello (DE)' )->disable(); + $plain->getReferences()->add( 'a.php', 1 ); + $plain->getFlags()->add( 'fuzzy' ); + $translations->add( $plain ); + + $translation = $translations->insert( null, 'Hello', 'Hellos' ); + + $this->assertCount( 1, $translations ); + $this->assertSame( $translation, $translations->find( null, 'Hello' ) ); + $this->assertSame( 'Hello (DE)', $translation->getTranslation() ); + $this->assertSame( 'Hellos', $translation->getPlural() ); + $this->assertTrue( $translation->isDisabled() ); + $this->assertSame( [ 'fuzzy' ], $translation->getFlags()->toArray() ); + $this->assertSame( [ [ 'a.php', 1 ] ], $translation->getReferenceEntries() ); + } + + public function test_translations_have_default_headers() { + $translations = Translations::create( 'foo-plugin' ); + + $this->assertSame( + [ + 'Content-Transfer-Encoding', + 'Content-Type', + 'Language', + 'Language-Team', + 'Last-Translator', + 'MIME-Version', + 'PO-Revision-Date', + 'POT-Creation-Date', + 'Project-Id-Version', + 'Report-Msgid-Bugs-To', + 'X-Domain', + ], + array_keys( $translations->getHeaders()->toArray() ) + ); + } + + public function test_mo_file_includes_headers_and_translations_that_are_zero() { + $translations = Translations::create(); + $translations->getHeaders()->set( 'Plural-Forms', 'nplurals=2; plural=(n != 1);' ); + + $translations->insert( 'start of week', '1' )->translate( '0' ); + $translations->insert( null, 'Untranslated' ); + $translations->insert( null, 'Disabled' )->translate( 'Disabled (DE)' )->disable(); + $translations->insert( null, '%d item', '%d items' )->translate( '%d item (DE)' )->translatePlural( '%d items (DE)' ); + + $mo = MoGenerator::toString( $translations ); + + $this->assertSame( 0x950412de, current( (array) unpack( 'L', $mo ) ) ); + $this->assertSame( 3, current( (array) unpack( 'L', substr( $mo, 8 ) ) ) ); + $this->assertStringContainsString( "Plural-Forms: nplurals=2; plural=(n != 1);\n", $mo ); + $this->assertStringContainsString( "start of week\x041\x00", $mo ); + $this->assertStringContainsString( "%d item\x00%d items\x00", $mo ); + $this->assertStringContainsString( "%d item (DE)\x00%d items (DE)\x00", $mo ); + + $loaded = ( new \Gettext\Loader\MoLoader() )->loadString( $mo )->find( 'start of week', '1' ); + $this->assertNotNull( $loaded ); + $this->assertSame( '0', $loaded->getTranslation() ); + + $this->assertStringNotContainsString( 'Untranslated', $mo ); + $this->assertStringNotContainsString( 'Disabled', $mo ); + } +}