From 53685a106ab5daa7d32b84483e084308df42c1c5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 26 Dec 2018 16:08:27 +0100 Subject: [PATCH 1/3] Extend PotGenerator to improve plural forms output --- src/PotGenerator.php | 103 ++++++++++++++++++++++++++++++++++--- tests/PotGeneratorTest.php | 25 +++++++++ 2 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 tests/PotGeneratorTest.php diff --git a/src/PotGenerator.php b/src/PotGenerator.php index 2a7d3760..9fe04949 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -2,7 +2,7 @@ namespace WP_CLI\I18n; -use Gettext\Generators\Po; +use Gettext\Generators\Po as PoGenerator; use Gettext\Translations; /** @@ -11,7 +11,7 @@ * The only difference to the existing PO file generator is that this * adds some comments at the very beginning of the file. */ -class PotGenerator extends Po { +class PotGenerator extends PoGenerator { protected static $comments_before_headers = []; /** @@ -35,12 +35,103 @@ public static function setCommentBeforeHeaders( $comment ) { * {@parentDoc}. */ public static function toString( Translations $translations, array $options = [] ) { - $result = ''; + $lines = static::$comments_before_headers + [ 'msgid ""', 'msgstr ""' ]; - if ( ! empty( static::$comments_before_headers ) ) { - $result = implode( "\n", static::$comments_before_headers ) . "\n"; + $plural_form = $translations->getPluralForms(); + $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : 1; + + foreach ( $translations->getHeaders() as $name => $value ) { + $lines[] = sprintf( '"%s: %s\\n"', $name, $value ); + } + + $lines[] = ''; + + foreach ( $translations as $translation ) { + /** @var \Gettext\Translation $translation */ + if ( $translation->hasComments() ) { + foreach ( $translation->getComments() as $comment ) { + $lines[] = '# ' . $comment; + } + } + + if ( $translation->hasExtractedComments() ) { + foreach ( $translation->getExtractedComments() as $comment ) { + $lines[] = '#. ' . $comment; + } + } + + foreach ( $translation->getReferences() as $reference ) { + $lines[] = '#: ' . $reference[0] . ( $reference[1] !== null ? ':' . $reference[1] : null ); + } + + if ( $translation->hasFlags() ) { + $lines[] = '#, ' . implode( ',', $translation->getFlags() ); + } + + $prefix = $translation->isDisabled() ? '#~ ' : ''; + + 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() ); + + for ( $i = 0; $i <= $plural_size; $i ++ ) { + self::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' ); + } + } else { + self::addLines( $lines, $prefix . 'msgstr', $translation->getTranslation() ); + } + + $lines[] = ''; } - return $result . parent::toString( $translations, $options ); + return implode("\n", $lines); + } + + /** + * Escapes and adds double quotes to a string. + * + * @param string $string Multiline string. + * + * @return string[] + */ + private static function multilineQuote($string) { + $lines = explode( "\n", $string ); + $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. + */ + private static function addLines( array &$lines, $name, $value ) { + $newlines = self::multilineQuote( $value ); + + if ( count( $newlines ) === 1 ) { + $lines[] = $name . ' ' . $newlines[0]; + } else { + $lines[] = $name . ' ""'; + + foreach ( $newlines as $line ) { + $lines[] = $line; + } + } } } diff --git a/tests/PotGeneratorTest.php b/tests/PotGeneratorTest.php new file mode 100644 index 00000000..c27eed89 --- /dev/null +++ b/tests/PotGeneratorTest.php @@ -0,0 +1,25 @@ +assertContains( 'msgid "%d cat"', $result ); + $this->assertContains( 'msgid_plural "%d cats"', $result ); + $this->assertContains( 'msgstr[0] ""', $result ); + $this->assertContains( 'msgstr[1] ""', $result ); + } +} From 2604669ce6a7720513c5c8d13c6eceed0a7632b5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 26 Dec 2018 17:18:38 +0100 Subject: [PATCH 2/3] Fix array concat --- src/PotGenerator.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PotGenerator.php b/src/PotGenerator.php index 9fe04949..496f5d85 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -35,7 +35,9 @@ public static function setCommentBeforeHeaders( $comment ) { * {@parentDoc}. */ public static function toString( Translations $translations, array $options = [] ) { - $lines = static::$comments_before_headers + [ 'msgid ""', 'msgstr ""' ]; + $lines = static::$comments_before_headers; + $lines[] = 'msgid ""'; + $lines[] = 'msgstr ""'; $plural_form = $translations->getPluralForms(); $plural_size = is_array( $plural_form ) ? ( $plural_form[0] - 1 ) : 1; From 3d9cb9568a76acf43d5759763a64184c4d88fad8 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 19 Feb 2019 19:17:58 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-Authored-By: swissspidy --- src/PotGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PotGenerator.php b/src/PotGenerator.php index 496f5d85..e7da8393 100644 --- a/src/PotGenerator.php +++ b/src/PotGenerator.php @@ -63,7 +63,7 @@ public static function toString( Translations $translations, array $options = [] } foreach ( $translation->getReferences() as $reference ) { - $lines[] = '#: ' . $reference[0] . ( $reference[1] !== null ? ':' . $reference[1] : null ); + $lines[] = '#: ' . $reference[0] . ( $reference[1] !== null ? ':' . $reference[1] : '' ); } if ( $translation->hasFlags() ) { @@ -91,7 +91,7 @@ public static function toString( Translations $translations, array $options = [] $lines[] = ''; } - return implode("\n", $lines); + return implode( "\n", $lines ); } /**