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() ) { $unique_comments = array(); /** @var ParsedComment|string $comment */ foreach ( $translation->getExtractedComments() as $comment ) { $comment = ( $comment instanceof ParsedComment ? $comment->getComment() : $comment ); if ( ! in_array( $comment, $unique_comments, true ) ) { $lines[] = '#. ' . $comment; $unique_comments[] = $comment; } } } foreach ( $translation->getReferences() as $reference ) { $lines[] = '#: ' . $reference[0] . ( null !== $reference[1] ? ':' . $reference[1] : '' ); } 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 implode( "\n", $lines ); } /** * Escapes and adds double quotes to a string. * * @param string $string Multiline string. * * @return string[] */ protected 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. */ protected 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; } } } }