Skip to content

Instantly share code, notes, and snippets.

@Rarst

Rarst/mustachepot.php

Last active Sep 29, 2016
Embed
What would you like to do?
MustachePOT extends MakePOT in WordPress tools to additionally scan for localized strings in Mustache templates.
<?php
error_reporting( E_ALL ^ E_STRICT ); // le sigh
require __DIR__ . '/wordpress/tools/i18n/makepot.php';
/**
* Additionally scans for localized strings in Mustache templates.
*/
class MustachePOT extends MakePOT {
/**
* @param string $dir
* @param string $output
* @param string $slug
*
* @return bool
*/
public function wp_plugin( $dir, $output, $slug = null ) {
$placeholders = array();
// guess plugin slug
if ( is_null( $slug ) ) {
$slug = $this->guess_plugin_slug( $dir );
}
$main_file = $dir . '/' . $slug . '.php';
$source = $this->get_first_lines( $main_file, $this->max_header_lines );
$placeholders['version'] = $this->get_addon_header( 'Version', $source );
$placeholders['author'] = $this->get_addon_header( 'Author', $source );
$placeholders['name'] = $this->get_addon_header( 'Plugin Name', $source );
$placeholders['slug'] = $slug;
$output = is_null( $output ) ? "$slug.pot" : $output;
$res = $this->xgettext( 'wp-plugin', $dir, $output, $placeholders );
if ( ! $res ) {
return false;
}
$potextmeta = new PotExtMeta;
$res = $potextmeta->append( $main_file, $output );
if ( ! $res ) {
return false;
}
$res = $this->append( $dir, $output );
if ( ! $res ) {
return false;
}
/* Adding non-gettexted strings can repeat some phrases */
$output_shell = escapeshellarg( $output );
system( "msguniq $output_shell -o $output_shell" );
return true;
}
/**
* @param string $ext_filename
* @param string $pot_filename
*
* @return bool
*/
function append( $ext_filename, $pot_filename ) {
if ( is_dir( $ext_filename ) ) {
// glob is tad hardcoded, not figuring out recursive right now
$pot = implode( '', array_map( array( $this, 'load_from_file' ), glob( "$ext_filename/mustache/*.mustache" ) ) );
} else {
$pot = $this->load_from_file( $ext_filename );
}
$potf = '-' == $pot_filename ? STDOUT : fopen( $pot_filename, 'a' );
if ( ! $potf ) {
return false;
}
fwrite( $potf, $pot );
if ( '-' != $pot_filename ) {
fclose( $potf );
}
return true;
}
/**
* @param string $ext_filename
*
* @return string
*/
function load_from_file( $ext_filename ) {
$pot = '';
$strings = array();
preg_match_all( '/{{#i18n}}(.*?){{\/i18n}}/', file_get_contents( $ext_filename ), $strings );
if ( ! empty( $strings[1] ) ) {
$strings = $strings[1];
} else {
return $pot;
}
foreach ( $strings as $string ) {
$entry = new Translation_Entry( array( 'singular' => $string ) );
$pot .= "\n" . PO::export_entry( $entry ) . "\n";
}
return $pot;
}
}
// run the CLI only if the file
// wasn't included
$included_files = get_included_files();
if ( $included_files[0] == __FILE__ ) {
$makepot = new MustachePOT();
if ( ( 3 == count( $argv ) || 4 == count( $argv ) ) && in_array( $method = str_replace( '-', '_', $argv[1] ), get_class_methods( $makepot ) ) ) {
$res = call_user_func( array( $makepot, $method ), realpath( $argv[2] ), isset( $argv[3] ) ? $argv[3] : null );
if ( false === $res ) {
fwrite( STDERR, "Couldn't generate POT file!\n" );
}
} else {
$usage = "Usage: php mustachepot.php PROJECT DIRECTORY [OUTPUT]\n\n";
$usage .= "Generate POT file from the files in DIRECTORY [OUTPUT]\n";
$usage .= 'Available projects: ' . implode( ', ', $makepot->projects ) . "\n";
fwrite( STDERR, $usage );
exit( 1 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.