WP_Translation_File_MO::export(): string

In this article

Exports translation contents as a string.

Return

string Translation file contents.

Source

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 );

	if ( false === $this->uint32 ) {
		$this->uint32 = 'V';
	}

	$bytes_for_entries = $entry_count * 4 * 2;
	// Pair of 32bit ints per entry.
	$originals_addr    = 28; /* header */
	$translations_addr = $originals_addr + $bytes_for_entries;
	$hash_addr         = $translations_addr + $bytes_for_entries;
	$entry_offsets     = $hash_addr;

	$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
	);

	$o_entries = '';
	$t_entries = '';
	$o_addr    = '';
	$t_addr    = '';

	foreach ( array_keys( $entries ) as $original ) {
		$o_addr        .= pack( $this->uint32 . '*', strlen( $original ), $entry_offsets );
		$entry_offsets += strlen( $original ) + 1;
		$o_entries     .= $original . "\0";
	}

	foreach ( $entries as $translations ) {
		$t_addr        .= pack( $this->uint32 . '*', strlen( $translations ), $entry_offsets );
		$entry_offsets += strlen( $translations ) + 1;
		$t_entries     .= $translations . "\0";
	}

	return $file_header . $o_addr . $t_addr . $o_entries . $t_entries;
}

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.