-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from wp-cli/add/blockjson
- Loading branch information
Showing
7 changed files
with
426 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace WP_CLI\I18n; | ||
|
||
use Gettext\Extractors\Extractor; | ||
use Gettext\Extractors\ExtractorInterface; | ||
use Gettext\Translations; | ||
use WP_CLI; | ||
|
||
final class BlockExtractor extends Extractor implements ExtractorInterface { | ||
use IterableCodeExtractor; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function fromString( $string, Translations $translations, array $options = [] ) { | ||
$file = $options['file']; | ||
WP_CLI::debug( "Parsing file {$file}", 'make-pot' ); | ||
|
||
$file_data = json_decode( $string, true ); | ||
|
||
if ( null === $file_data ) { | ||
WP_CLI::debug( | ||
sprintf( | ||
'Could not parse file %1$s: error code %2$s', | ||
$file, | ||
json_last_error() | ||
), | ||
'make-pot' | ||
); | ||
|
||
return; | ||
} | ||
|
||
$domain = isset( $file_data['textdomain'] ) ? $file_data['textdomain'] : null; | ||
|
||
// Allow missing domain, but skip if they don't match. | ||
if ( null !== $domain && $domain !== $translations->getDomain() ) { | ||
return; | ||
} | ||
|
||
foreach ( $file_data as $key => $original ) { | ||
switch ( $key ) { | ||
case 'title': | ||
case 'description': | ||
$translation = $translations->insert( sprintf( 'block %s', $key ), $original ); | ||
$translation->addReference( $file ); | ||
break; | ||
case 'keywords': | ||
if ( ! is_array( $original ) ) { | ||
continue 2; | ||
} | ||
|
||
foreach ( $original as $msg ) { | ||
$translation = $translations->insert( 'block keyword', $msg ); | ||
$translation->addReference( $file ); | ||
} | ||
|
||
break; | ||
case 'styles': | ||
if ( ! is_array( $original ) ) { | ||
continue 2; | ||
} | ||
|
||
foreach ( $original as $msg ) { | ||
$translation = $translations->insert( 'block style label', $msg['label'] ); | ||
$translation->addReference( $file ); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.