-
Notifications
You must be signed in to change notification settings - Fork 52
/
BlockExtractor.php
72 lines (59 loc) · 1.65 KB
/
BlockExtractor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 );
}
}
}
}
}