-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathPhpFunctionsScanner.php
83 lines (64 loc) · 2.2 KB
/
PhpFunctionsScanner.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
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace WP_CLI\I18n;
use Gettext\Utils\PhpFunctionsScanner as GettextPhpFunctionsScanner;
class PhpFunctionsScanner extends GettextPhpFunctionsScanner {
/**
* {@inheritdoc}
*/
public function saveGettextFunctions( $translations, array $options ) {
// Ignore multiple translations for now.
// @todo Add proper support for multiple translations.
if ( is_array( $translations ) ) {
$translations = $translations[0];
}
$functions = $options['functions'];
$file = $options['file'];
foreach ( $this->getFunctions( $options['constants'] ) as $function ) {
list( $name, $line, $args ) = $function;
if ( ! isset( $functions[ $name ] ) ) {
continue;
}
$original = null;
$domain = null;
$context = null;
$plural = null;
switch ( $functions[ $name ] ) {
case 'text_domain':
case 'gettext':
list( $original, $domain ) = array_pad( $args, 2, null );
break;
case 'text_context_domain':
list( $original, $context, $domain ) = array_pad( $args, 3, null );
break;
case 'single_plural_number_domain':
list( $original, $plural, $number, $domain ) = array_pad( $args, 4, null );
break;
case 'single_plural_number_context_domain':
list( $original, $plural, $number, $context, $domain ) = array_pad( $args, 5, null );
break;
case 'single_plural_domain':
list( $original, $plural, $domain ) = array_pad( $args, 3, null );
break;
case 'single_plural_context_domain':
list( $original, $plural, $context, $domain ) = array_pad( $args, 4, null );
break;
default:
// Should never happen.
\WP_CLI::error( sprintf( "Internal error: unknown function map '%s' for '%s'.", $functions[ $name ], $name ) );
}
if ( '' === (string) $original ) {
continue;
}
if ( $domain !== $translations->getDomain() && null !== $translations->getDomain() ) {
continue;
}
$translation = $translations->insert( $context, $original, $plural );
$translation = $translation->addReference( $file, $line );
if ( isset( $function[3] ) ) {
foreach ( $function[3] as $extracted_comment ) {
$translation = $translation->addExtractedComment( $extracted_comment );
}
}
}
}
}