Skip to content

Instantly share code, notes, and snippets.

@Rarst
Last active September 29, 2016 13:47
Show Gist options
  • Select an option

  • Save Rarst/9178471 to your computer and use it in GitHub Desktop.

Select an option

Save Rarst/9178471 to your computer and use it in GitHub Desktop.

Revisions

  1. Rarst revised this gist Feb 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mustachepot.php
    Original file line number Diff line number Diff line change
    @@ -133,7 +133,7 @@ function load_from_file( $ext_filename ) {
    }
    } else {

    $usage = "Usage: php makepot.php PROJECT DIRECTORY [OUTPUT]\n\n";
    $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 );
  2. Rarst created this gist Feb 23, 2014.
    143 changes: 143 additions & 0 deletions mustachepot.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,143 @@
    <?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 makepot.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 );
    }
    }