-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathJsStringFilterTrait.php
More file actions
46 lines (38 loc) · 1.16 KB
/
JsStringFilterTrait.php
File metadata and controls
46 lines (38 loc) · 1.16 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
<?php
namespace WP_CLI\I18n;
use Gettext\Translation;
use Gettext\Translations;
trait JsStringFilterTrait {
/**
* JavaScript file extensions to check for.
*
* @var string[]
*/
protected static $js_extensions = [ '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs' ];
/**
* Removes strings from translations that only occur in JavaScript files.
*
* @param Translations $translations The translations instance to filter.
*/
protected function remove_js_only_strings( Translations $translations ): void {
foreach ( $translations->getArrayCopy() as $translation ) {
/** @var Translation $translation */
if ( ! $translation->hasReferences() ) {
continue;
}
$has_non_js_reference = false;
foreach ( $translation->getReferences() as $reference ) {
$file = $reference[0];
$extension = '.' . strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
if ( ! in_array( $extension, static::$js_extensions, true ) ) {
$has_non_js_reference = true;
break;
}
}
// If all references are JS files, remove this translation.
if ( ! $has_non_js_reference ) {
unset( $translations[ $translation->getId() ] );
}
}
}
}