Skip to content

Commit 5d79390

Browse files
authored
Merge pull request #439 from michaelw85/#231_add_custom_extension_support
2 parents 845cdb0 + 952f493 commit 5d79390

2 files changed

Lines changed: 153 additions & 13 deletions

File tree

features/makejson.feature

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,3 +984,137 @@ Feature: Split PO files into JSON files.
984984
"""
985985
And the return code should be 0
986986
And the foo-theme/my-custom-domain-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
987+
988+
Scenario: Should only process js/min.js extensions by default
989+
Given an empty foo-theme directory
990+
And a foo-theme/de_DE.po file:
991+
"""
992+
# Copyright (C) 2018 Foo Theme
993+
# This file is distributed under the same license as the Foo Plugin package.
994+
msgid ""
995+
msgstr ""
996+
"Project-Id-Version: Foo Plugin\n"
997+
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
998+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
999+
"Language-Team: LANGUAGE <LL@li.org>\n"
1000+
"Language: de_DE\n"
1001+
"MIME-Version: 1.0\n"
1002+
"Content-Type: text/plain; charset=UTF-8\n"
1003+
"Content-Transfer-Encoding: 8bit\n"
1004+
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
1005+
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
1006+
"X-Domain: foo-theme\n"
1007+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
1008+
1009+
#: foo-theme.js:15
1010+
msgid "Foo Theme"
1011+
msgstr "Foo Theme"
1012+
1013+
#: bar.min.minified.min.js:15
1014+
msgid "Foo Theme"
1015+
msgstr "Foo Theme"
1016+
1017+
#: foo-theme.ts:15
1018+
msgid "Foo Theme"
1019+
msgstr "Foo Theme"
1020+
1021+
#: foo-theme.tag:15
1022+
msgid "Foo Theme"
1023+
msgstr "Foo Theme"
1024+
"""
1025+
1026+
When I run `wp i18n make-json foo-theme`
1027+
Then STDOUT should contain:
1028+
"""
1029+
Success: Created 2 files.
1030+
"""
1031+
And the return code should be 0
1032+
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
1033+
And the foo-theme/foo-theme-de_DE-a9c6627f5fe96185c0a0d0ddd8fa0216.json file should exist
1034+
1035+
Scenario: Allows processing custom file extensions
1036+
Given an empty foo-theme directory
1037+
And a foo-theme/de_DE.po file:
1038+
"""
1039+
# Copyright (C) 2018 Foo Theme
1040+
# This file is distributed under the same license as the Foo Plugin package.
1041+
msgid ""
1042+
msgstr ""
1043+
"Project-Id-Version: Foo Plugin\n"
1044+
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
1045+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1046+
"Language-Team: LANGUAGE <LL@li.org>\n"
1047+
"Language: de_DE\n"
1048+
"MIME-Version: 1.0\n"
1049+
"Content-Type: text/plain; charset=UTF-8\n"
1050+
"Content-Transfer-Encoding: 8bit\n"
1051+
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
1052+
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
1053+
"X-Domain: foo-theme\n"
1054+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
1055+
1056+
#: foo-theme.js:15
1057+
msgid "Foo Theme"
1058+
msgstr "Foo Theme"
1059+
1060+
#: foo-theme.ts:15
1061+
msgid "Foo Theme"
1062+
msgstr "Foo Theme"
1063+
1064+
#: foo-theme.tag:15
1065+
msgid "Foo Theme"
1066+
msgstr "Foo Theme"
1067+
"""
1068+
1069+
When I run `wp i18n make-json foo-theme --extensions=ts,tag`
1070+
Then STDOUT should contain:
1071+
"""
1072+
Success: Created 3 files.
1073+
"""
1074+
And the return code should be 0
1075+
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
1076+
And the foo-theme/foo-theme-de_DE-a4e9f6529ffa4750907c140158b834b9.json file should exist
1077+
And the foo-theme/foo-theme-de_DE-df975addfaa8d6579df3d3133999691a.json file should exist
1078+
1079+
Scenario: Should use extensions, strip spaces and . from notation
1080+
Given an empty foo-theme directory
1081+
And a foo-theme/de_DE.po file:
1082+
"""
1083+
# Copyright (C) 2018 Foo Theme
1084+
# This file is distributed under the same license as the Foo Plugin package.
1085+
msgid ""
1086+
msgstr ""
1087+
"Project-Id-Version: Foo Plugin\n"
1088+
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
1089+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1090+
"Language-Team: LANGUAGE <LL@li.org>\n"
1091+
"Language: de_DE\n"
1092+
"MIME-Version: 1.0\n"
1093+
"Content-Type: text/plain; charset=UTF-8\n"
1094+
"Content-Transfer-Encoding: 8bit\n"
1095+
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
1096+
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
1097+
"X-Domain: foo-theme\n"
1098+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
1099+
1100+
#: foo-theme.js:15
1101+
msgid "Foo Theme"
1102+
msgstr "Foo Theme"
1103+
1104+
#: foo-theme.ts:15
1105+
msgid "Foo Theme"
1106+
msgstr "Foo Theme"
1107+
1108+
#: foo-theme.tag:15
1109+
msgid "Foo Theme"
1110+
msgstr "Foo Theme"
1111+
"""
1112+
1113+
When I run `wp i18n make-json foo-theme --extensions=".ts, .tsx"`
1114+
Then STDOUT should contain:
1115+
"""
1116+
Success: Created 2 files.
1117+
"""
1118+
And the return code should be 0
1119+
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
1120+
And the foo-theme/foo-theme-de_DE-a4e9f6529ffa4750907c140158b834b9.json file should exist

src/MakeJsonCommand.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class MakeJsonCommand extends WP_CLI_Command {
4141
* [--domain=<domain>]
4242
* : Text domain to use for the JSON file name. Overrides the default one extracted from the PO file.
4343
*
44+
* [--extensions=<extensions>]
45+
* : Additional custom JS extensions, comma separated list. By default searches for .min.js and .js extensions.
46+
*
4447
* [--purge]
4548
* : Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal.
4649
*
@@ -82,6 +85,12 @@ public function __invoke( $args, $assoc_args ) {
8285
$update_mo_files = Utils\get_flag_value( $assoc_args, 'update-mo-files', true );
8386
$map_paths = Utils\get_flag_value( $assoc_args, 'use-map', false );
8487
$domain = Utils\get_flag_value( $assoc_args, 'domain', '' );
88+
$extensions = array_map(
89+
function ( $extension ) {
90+
return trim( $extension, ' .' );
91+
},
92+
explode( ',', Utils\get_flag_value( $assoc_args, 'extensions', '' ) )
93+
);
8594

8695
if ( Utils\get_flag_value( $assoc_args, 'pretty-print', false ) ) {
8796
$this->json_options |= JSON_PRETTY_PRINT;
@@ -119,7 +128,7 @@ public function __invoke( $args, $assoc_args ) {
119128
/** @var DirectoryIterator $file */
120129
foreach ( $files as $file ) {
121130
if ( $file->isFile() && $file->isReadable() && 'po' === $file->getExtension() ) {
122-
$result = $this->make_json( $file->getRealPath(), $destination, $map, $domain );
131+
$result = $this->make_json( $file->getRealPath(), $destination, $map, $domain, $extensions );
123132
$result_count += count( $result );
124133

125134
if ( $purge ) {
@@ -228,13 +237,15 @@ static function ( $value ) {
228237
* @param string $destination Path to the destination directory.
229238
* @param array|null $map Source to build file mapping.
230239
* @param string $domain Override text domain to use.
240+
* @param array $extensions Additional extensions.
231241
* @return array List of created JSON files.
232242
*/
233-
protected function make_json( $source_file, $destination, $map, $domain ) {
243+
protected function make_json( $source_file, $destination, $map, $domain, $extensions ) {
234244
/** @var Translations[] $mapping */
235245
$mapping = [];
236246
$translations = new Translations();
237247
$result = [];
248+
$extensions = array_merge( [ 'js' ], $extensions );
238249

239250
PoExtractor::fromFile( $source_file, $translations );
240251

@@ -251,18 +262,13 @@ protected function make_json( $source_file, $destination, $map, $domain ) {
251262

252263
// Find all unique sources this translation originates from.
253264
$sources = array_map(
254-
static function ( $reference ) {
255-
$file = $reference[0];
256-
257-
if ( substr( $file, - 7 ) === '.min.js' ) {
258-
return substr( $file, 0, - 7 ) . '.js';
259-
}
260-
261-
if ( substr( $file, - 3 ) === '.js' ) {
262-
return $file;
263-
}
265+
static function ( $reference ) use ( $extensions ) {
266+
$file = $reference[0];
267+
$extension = pathinfo( $file, PATHINFO_EXTENSION );
264268

265-
return null;
269+
return in_array( $extension, $extensions, true )
270+
? preg_replace( "/.min.{$extension}$/", ".{$extension}", $file )
271+
: null;
266272
},
267273
$this->reference_map( $translation->getReferences(), $map )
268274
);

0 commit comments

Comments
 (0)