-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathJsCodeExtractor.php
More file actions
93 lines (83 loc) · 2.59 KB
/
JsCodeExtractor.php
File metadata and controls
93 lines (83 loc) · 2.59 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace WP_CLI\I18n;
use Exception;
use Gettext\Extractors\JsCode;
use Gettext\Translations;
use Peast\Syntax\Exception as PeastException;
use WP_CLI;
final class JsCodeExtractor extends JsCode {
use IterableCodeExtractor;
/**
* @var array<mixed>
*/
public static $options = [
'extractComments' => [ 'translators', 'Translators' ],
'constants' => [],
'functions' => [
'__' => 'text_domain',
'_x' => 'text_context_domain',
'_n' => 'single_plural_number_domain',
'_nx' => 'single_plural_number_context_domain',
],
];
/**
* @var string
*/
protected static $functionsScannerClass = 'WP_CLI\I18n\JsFunctionsScanner';
/**
* {@inheritdoc}
*
* @param string $text The text to extract strings from.
* @param Translations $translations Translations instance.
* @param array<mixed> $options Extraction options.
* @return void
*/
public static function fromString( $text, Translations $translations, array $options = [] ) {
$file = isset( $options['file'] ) && is_scalar( $options['file'] ) ? (string) $options['file'] : '';
WP_CLI::debug( "Parsing file {$file}", 'make-pot' );
try {
self::fromStringMultiple( $text, [ $translations ], $options );
} catch ( PeastException $exception ) {
WP_CLI::debug(
sprintf(
'Could not parse file %1$s: %2$s (line %3$d, column %4$d)',
$file,
$exception->getMessage(),
$exception->getPosition()->getLine(),
$exception->getPosition()->getColumn()
),
'make-pot'
);
} catch ( Exception $exception ) {
WP_CLI::debug(
sprintf(
'Could not parse file %1$s: %2$s',
$file,
$exception->getMessage()
),
'make-pot'
);
}
}
/**
* {@inheritdoc}
*
* @param string $text The text to extract strings from.
* @param array<\Gettext\Translations> $translations Translations instances.
* @param array<mixed> $options Extraction options.
* @return void
*/
public static function fromStringMultiple( $text, array $translations, array $options = [] ) {
$options += self::$options;
/** @var JsFunctionsScanner $functions */
$functions = new self::$functionsScannerClass( $text );
if ( isset( $options['extractComments'] ) && ( is_string( $options['extractComments'] ) || is_array( $options['extractComments'] ) ) ) {
/** @var array<string>|string $extract_comments */
$extract_comments = $options['extractComments'];
$functions->enableCommentsExtraction( $extract_comments );
}
if ( ! empty( $translations ) ) {
$functions->saveGettextFunctions( $translations[0], $options );
}
}
}