/** * Class WP_Translation_File_MO. * * @since 6.5.0 */ class WP_Translation_File_MO extends WP_Translation_File { /** * Endian value. * * V for little endian, N for big endian, or false. * * Used for unpack(). * * @since 6.5.0 * @var false|'V'|'N' */ protected $uint32 = false;
/** * The magic number of the GNU message catalog format. * * @since 6.5.0 * @var int */ const MAGIC_MARKER = 0x950412de;
/** * Detects endian and validates file. * * @since 6.5.0 * * @param string $header File contents. * @return false|'V'|'N' V for little endian, N for big endian, or false on failure. */ protected function detect_endian_and_validate_file( string $header ) { $big = unpack( 'N', $header );
if ( false === $big ) { return false; }
$big = reset( $big );
if ( false === $big ) { return false; }
$little = unpack( 'V', $header );
if ( false === $little ) { return false; }
$little = reset( $little );
if ( false === $little ) { return false; }
// Force cast to an integer as it can be a float on x86 systems. See https://core.trac.wordpress.org/ticket/60678. if ( (int) self::MAGIC_MARKER === $big ) { return 'N'; }
// Force cast to an integer as it can be a float on x86 systems. See https://core.trac.wordpress.org/ticket/60678. if ( (int) self::MAGIC_MARKER === $little ) { return 'V'; }
$this->error = 'Magic marker does not exist'; return false; }
/** * Parses the file. * * @since 6.5.0 * * @return bool True on success, false otherwise. */ protected function parse_file(): bool { $this->parsed = true;
// Metadata about the MO file is stored in the first translation entry. if ( '' === $original ) { foreach ( explode( "\n", $translation ) as $meta_line ) { if ( '' === $meta_line || ! str_contains( $meta_line, ':' ) ) { continue; }
$this->headers[ strtolower( $name ) ] = $value; } } else { /* * In MO files, the key normally contains both singular and plural versions. * However, this just adds the singular string for lookup, * which caters for cases where both __( 'Product' ) and _n( 'Product', 'Products' ) * are used and the translation is expected to be the same for both. */ $parts = explode( "\0", (string) $original );
$this->entries[ $parts[0] ] = $translation; } }
return true; }
/** * Exports translation contents as a string. * * @since 6.5.0 * * @return string Translation file contents. */ public function export(): string { // Prefix the headers as the first key. $headers_string = ''; foreach ( $this->headers as $header => $value ) { $headers_string .= "{$header}: $value\n"; } $entries = array_merge( array( '' => $headers_string ), $this->entries ); $entry_count = count( $entries );
$file_header = pack( $this->uint32 . '*', // Force cast to an integer as it can be a float on x86 systems. See https://core.trac.wordpress.org/ticket/60678. (int) self::MAGIC_MARKER, 0, /* rev */ $entry_count, $originals_addr, $translations_addr, 0, /* hash_length */ $hash_addr );