/* * The `auto_detect_line_endings` setting has been deprecated in PHP 8.1, * but will continue to work until PHP 9.0. * For now, we're silencing the deprecation notice as there may still be * translation files around which haven't been updated in a long time and * which still use the old MacOS standalone `\r` as a line ending. * This fix should be revisited when PHP 9.0 is in alpha/beta. */ @ini_set( 'auto_detect_line_endings', 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
/** * Routines for working with PO files */ if ( ! class_exists( 'PO', false ) ) : class PO extends Gettext_Translations {
public $comments_before_headers = '';
/** * Exports headers to a PO entry * * @return string msgid/msgstr PO entry for this PO file headers, doesn't contain newline at the end */ public function export_headers() { $header_string = ''; foreach ( $this->headers as $header => $value ) { $header_string .= "$header: $value\n"; } $poified = PO::poify( $header_string ); if ( $this->comments_before_headers ) { $before_headers = $this->prepend_each_line( rtrim( $this->comments_before_headers ) . "\n", '# ' ); } else { $before_headers = ''; } return rtrim( "{$before_headers}msgid \"\"\nmsgstr $poified" ); }
/** * Exports all entries to PO format * * @return string sequence of msgid/msgstr PO strings, doesn't contain a newline at the end */ public function export_entries() { // TODO: Sorting. return implode( "\n\n", array_map( array( 'PO', 'export_entry' ), $this->entries ) ); }
/** * Exports the whole PO file as a string * * @param bool $include_headers whether to include the headers in the export * @return string ready for inclusion in PO file string for headers and all the entries */ public function export( $include_headers = true ) { $res = ''; if ( $include_headers ) { $res .= $this->export_headers(); $res .= "\n\n"; } $res .= $this->export_entries(); return $res; }
/** * Same as {@link export}, but writes the result to a file * * @param string $filename Where to write the PO string. * @param bool $include_headers Whether to include the headers in the export. * @return bool true on success, false on error */ public function export_to_file( $filename, $include_headers = true ) { $fh = fopen( $filename, 'w' ); if ( false === $fh ) { return false; } $export = $this->export( $include_headers ); $res = fwrite( $fh, $export ); if ( false === $res ) { return false; } return fclose( $fh ); }
/** * Text to include as a comment before the start of the PO contents * * Doesn't need to include # in the beginning of lines, these are added automatically * * @param string $text Text to include as a comment. */ public function set_comment_before_headers( $text ) { $this->comments_before_headers = $text; }
/** * Formats a string in PO-style * * @param string $input_string the string to format * @return string the poified string */ public static function poify( $input_string ) { $quote = '"'; $slash = '\\'; $newline = "\n";
// Standardize the line endings on imported content, technically PO files shouldn't contain \r. $unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified );
return $unpoified; }
/** * Inserts $with in the beginning of every new line of $input_string and * returns the modified string * * @param string $input_string prepend lines in this string * @param string $with prepend lines with this string */ public static function prepend_each_line( $input_string, $with ) { $lines = explode( "\n", $input_string ); $append = ''; if ( "\n" === substr( $input_string, -1 ) && '' === end( $lines ) ) { /* * Last line might be empty because $input_string was terminated * with a newline, remove it from the $lines array, * we'll restore state by re-terminating the string at the end. */ array_pop( $lines ); $append = "\n"; } foreach ( $lines as &$line ) { $line = $with . $line; } unset( $line ); return implode( "\n", $lines ) . $append; }
/** * Prepare a text as a comment -- wraps the lines and prepends # * and a special character to each line * * @access private * @param string $text the comment text * @param string $char character to denote a special PO comment, * like :, default is a space */ public static function comment_block( $text, $char = ' ' ) { $text = wordwrap( $text, PO_MAX_LINE_LEN - 3 ); return PO::prepend_each_line( $text, "#$char " ); }
/** * Builds a string from the entry for inclusion in PO file * * @param Translation_Entry $entry the entry to convert to po string. * @return string|false PO-style formatted string for the entry or * false if the entry is empty */ public static function export_entry( $entry ) { if ( null === $entry->singular || '' === $entry->singular ) { return false; } $po = array(); if ( ! empty( $entry->translator_comments ) ) { $po[] = PO::comment_block( $entry->translator_comments ); } if ( ! empty( $entry->extracted_comments ) ) { $po[] = PO::comment_block( $entry->extracted_comments, '.' ); } if ( ! empty( $entry->references ) ) { $po[] = PO::comment_block( implode( ' ', $entry->references ), ':' ); } if ( ! empty( $entry->flags ) ) { $po[] = PO::comment_block( implode( ', ', $entry->flags ), ',' ); } if ( $entry->context ) { $po[] = 'msgctxt ' . PO::poify( $entry->context ); } $po[] = 'msgid ' . PO::poify( $entry->singular ); if ( ! $entry->is_plural ) { $translation = empty( $entry->translations ) ? '' : $entry->translations[0]; $translation = PO::match_begin_and_end_newlines( $translation, $entry->singular ); $po[] = 'msgstr ' . PO::poify( $translation ); } else { $po[] = 'msgid_plural ' . PO::poify( $entry->plural ); $translations = empty( $entry->translations ) ? array( '', '' ) : $entry->translations; foreach ( $translations as $i => $translation ) { $translation = PO::match_begin_and_end_newlines( $translation, $entry->plural ); $po[] = "msgstr[$i] " . PO::poify( $translation ); } } return implode( "\n", $po ); }
public static function match_begin_and_end_newlines( $translation, $original ) { if ( '' === $translation ) { return $translation; }