Skip to content
Timi-Artturi Mäkelä edited this page May 16, 2019 · 10 revisions

A PHP helper script to scan a DustPress theme for translatable strings with POedit. Put it in a subdirectory under the root directory of you theme (for example /utils) and run the script which must have writing permission to its directory (thus it is preferable to run the script via command line).

The script then writes a file called strings.php containing all strings defined with {@s} helper in a format that can be scanned with POedit.

<?php
$path = realpath( __DIR__ ."/../partials" );

if ( ! $path ) {
	die("Path ../partials was not found.");
}

$return = [];

foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::SKIP_DOTS ) ) as $file ) {
    if ( is_readable( $file ) && '.dust' === substr( $file, -5, 5 ) ) {
        $return = array_merge( $return, parse( file_get_contents( $file ) ) );
    }
}

$strings = [];

foreach ( array_unique( $return, SORT_REGULAR ) as $s ) {

    // Escape quotes
    $s['s'] = addslashes( $s['s'] );

    if ( isset( $s['td'] ) ) {
        $strings[] = "__( '". $s['s'] ."', '". $s['td'] ."' );";
    }
    else {
        $strings[] = "__( '". $s['s'] ."' );";
    }
}

$contents = "<?php\n" . implode( "\n", $strings ). "\n";

file_put_contents( "strings.php", $contents );

function parse( $partial ) {
	$return = [];

	preg_match_all( '/\{@s\s+?(.+)\s+?\/}/', $partial, $ss );

	$contents = $ss[1];

	foreach ( $contents as $content ) {
		preg_match_all( '/(\w+)="(.+?([^\\\]))"/', $content, $parameters );

		$params = [];

		foreach ( $parameters[1] as $index => $param ) {
			$params[ $param ] = $parameters[2][ $index ];
		}

		ksort( $params );

		$return[] = $params;
	}

	return $return;
}
Clone this wiki locally