Skip to content

Commit

Permalink
Merge pull request #63 from drzraf/extract-wo-save
Browse files Browse the repository at this point in the history
separate translation extraction from Po file writing
  • Loading branch information
swissspidy authored Aug 6, 2018
2 parents 3fa39a1 + e0bea13 commit eb8d68f
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
use IteratorIterator;

class MakePotCommand extends WP_CLI_Command {
/**
* @var Translations
*/
protected $translations;

/**
* @var string
*/
Expand Down Expand Up @@ -111,10 +106,19 @@ class MakePotCommand extends WP_CLI_Command {
*/
public function __invoke( $args, $assoc_args ) {
$this->handle_arguments( $args, $assoc_args );
if ( ! $this->makepot() ) {
$translations = $this->extract_strings();
if ( ! $translations ) {
WP_CLI::warning( 'No translation found' );
}
$translations_count = count( $translations );
if ( 1 === $translations_count ) {
WP_CLI::debug( sprintf( 'Extracted %d string', $translations_count ), 'make-pot' );
} else {
WP_CLI::debug( sprintf( 'Extracted %d strings', $translations_count ), 'make-pot' );
}
if ( ! PotGenerator::toFile( $translations, $this->destination ) ) {
WP_CLI::error( 'Could not generate a POT file!' );
}

WP_CLI::success( 'POT file successfully generated!' );
}

Expand Down Expand Up @@ -312,30 +316,30 @@ protected function get_main_file_data() {
/**
* Creates a POT file and stores it on disk.
*
* @return bool True on success, false otherwise.
* @return Translations A Translation set.
*/
protected function makepot() {
$this->translations = new Translations();
protected function extract_strings() {
$translations = new Translations();

// Add existing strings first but don't keep headers.
if ( $this->merge ) {
WP_CLI::debug( sprintf( 'Merging with existing POT file: %s', $this->merge ), 'make-pot' );

$existing_translations = new Translations();
Po::fromFile( $this->merge, $existing_translations );
$this->translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE );
$translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE );
}

$meta = $this->get_meta_data();
PotGenerator::setCommentBeforeHeaders( $meta['comments'] );

$this->set_default_headers();
$this->set_default_headers( $translations );

// POT files have no Language header.
$this->translations->deleteHeader( Translations::HEADER_LANGUAGE );
$translations->deleteHeader( Translations::HEADER_LANGUAGE );

if ( $this->domain ) {
$this->translations->setDomain( $this->domain );
$translations->setDomain( $this->domain );
}

$file_data = $this->get_main_file_data();
Expand All @@ -356,11 +360,11 @@ protected function makepot() {
$translation->addExtractedComment( sprintf( '%s of the plugin', $header ) );
}

$this->translations[] = $translation;
$translations[] = $translation;
}

try {
PhpCodeExtractor::fromDirectory( $this->source, $this->translations, [
PhpCodeExtractor::fromDirectory( $this->source, $translations, [
// Extract 'Template Name' headers in theme files.
'wpExtractTemplates' => isset( $file_data['Theme Name'] ),
'exclude' => $this->exclude,
Expand All @@ -370,7 +374,7 @@ protected function makepot() {
if ( ! $this->skip_js ) {
JsCodeExtractor::fromDirectory(
$this->source,
$this->translations,
$translations,
[
'exclude' => $this->exclude,
'extensions' => [ 'js' ],
Expand All @@ -381,7 +385,7 @@ protected function makepot() {
WP_CLI::error( $e->getMessage() );
}

foreach( $this->translations as $translation ) {
foreach( $translations as $translation ) {
if ( ! $translation->hasExtractedComments() ) {
continue;
}
Expand All @@ -397,18 +401,7 @@ protected function makepot() {
) );
}
}

$result = PotGenerator::toFile( $this->translations, $this->destination );

$translations_count = count( $this->translations );

if ( 1 === $translations_count ) {
WP_CLI::debug( sprintf( 'Extracted %d string', $translations_count ), 'make-pot' );
} else {
WP_CLI::debug( sprintf( 'Extracted %d strings', $translations_count ), 'make-pot' );
}

return $result;
return $translations;
}

/**
Expand Down Expand Up @@ -456,17 +449,17 @@ protected function get_meta_data() {
/**
* Sets default POT file headers for the project.
*/
protected function set_default_headers() {
protected function set_default_headers( $translations ) {
$meta = $this->get_meta_data();

$this->translations->setHeader( 'Project-Id-Version', $meta['name'] . ' ' . $meta['version'] );
$this->translations->setHeader( 'Report-Msgid-Bugs-To', $meta['msgid-bugs-address'] );
$this->translations->setHeader( 'Last-Translator', 'FULL NAME <EMAIL@ADDRESS>' );
$this->translations->setHeader( 'Language-Team', 'LANGUAGE <[email protected]>' );
$this->translations->setHeader( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION );
$translations->setHeader( 'Project-Id-Version', $meta['name'] . ' ' . $meta['version'] );
$translations->setHeader( 'Report-Msgid-Bugs-To', $meta['msgid-bugs-address'] );
$translations->setHeader( 'Last-Translator', 'FULL NAME <EMAIL@ADDRESS>' );
$translations->setHeader( 'Language-Team', 'LANGUAGE <[email protected]>' );
$translations->setHeader( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION );

foreach( $this->headers as $key => $value ) {
$this->translations->setHeader( $key, $value );
$translations->setHeader( $key, $value );
}
}

Expand Down

0 comments on commit eb8d68f

Please sign in to comment.