Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions features/makejson.feature
Original file line number Diff line number Diff line change
Expand Up @@ -984,3 +984,137 @@ Feature: Split PO files into JSON files.
"""
And the return code should be 0
And the foo-theme/my-custom-domain-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist

Scenario: Should only process js/min.js extensions by default
Given an empty foo-theme directory
And a foo-theme/de_DE.po file:
"""
# Copyright (C) 2018 Foo Theme
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-theme\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-theme.js:15
msgid "Foo Theme"
msgstr "Foo Theme"

#: bar.min.minified.min.js:15
msgid "Foo Theme"
msgstr "Foo Theme"

#: foo-theme.ts:15
msgid "Foo Theme"
msgstr "Foo Theme"

#: foo-theme.tag:15
msgid "Foo Theme"
msgstr "Foo Theme"
"""

When I run `wp i18n make-json foo-theme`
Then STDOUT should contain:
"""
Success: Created 2 files.
"""
And the return code should be 0
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
And the foo-theme/foo-theme-de_DE-a9c6627f5fe96185c0a0d0ddd8fa0216.json file should exist

Scenario: Allows processing custom file extensions
Given an empty foo-theme directory
And a foo-theme/de_DE.po file:
"""
# Copyright (C) 2018 Foo Theme
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-theme\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-theme.js:15
msgid "Foo Theme"
msgstr "Foo Theme"

#: foo-theme.ts:15
msgid "Foo Theme"
msgstr "Foo Theme"

#: foo-theme.tag:15
msgid "Foo Theme"
msgstr "Foo Theme"
"""

When I run `wp i18n make-json foo-theme --extensions=ts,tag`
Then STDOUT should contain:
"""
Success: Created 3 files.
"""
And the return code should be 0
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
And the foo-theme/foo-theme-de_DE-a4e9f6529ffa4750907c140158b834b9.json file should exist
And the foo-theme/foo-theme-de_DE-df975addfaa8d6579df3d3133999691a.json file should exist

Scenario: Should use extensions, strip spaces and . from notation
Given an empty foo-theme directory
And a foo-theme/de_DE.po file:
"""
# Copyright (C) 2018 Foo Theme
# This file is distributed under the same license as the Foo Plugin package.
msgid ""
msgstr ""
"Project-Id-Version: Foo Plugin\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
"X-Domain: foo-theme\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-theme.js:15
msgid "Foo Theme"
msgstr "Foo Theme"

#: foo-theme.ts:15
msgid "Foo Theme"
msgstr "Foo Theme"

#: foo-theme.tag:15
msgid "Foo Theme"
msgstr "Foo Theme"
"""

When I run `wp i18n make-json foo-theme --extensions=".ts, .tsx"`
Then STDOUT should contain:
"""
Success: Created 2 files.
"""
And the return code should be 0
And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist
And the foo-theme/foo-theme-de_DE-a4e9f6529ffa4750907c140158b834b9.json file should exist
32 changes: 19 additions & 13 deletions src/MakeJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
* [--domain=<domain>]
* : Text domain to use for the JSON file name. Overrides the default one extracted from the PO file.
*
* [--extensions=<extensions>]
* : Additional custom JS extensions, comma separated list. By default searches for .min.js and .js extensions.
*
* [--purge]
* : Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal.
*
Expand Down Expand Up @@ -82,6 +85,12 @@
$update_mo_files = Utils\get_flag_value( $assoc_args, 'update-mo-files', true );
$map_paths = Utils\get_flag_value( $assoc_args, 'use-map', false );
$domain = Utils\get_flag_value( $assoc_args, 'domain', '' );
$extensions = array_map(
function ( $extension ) {
return trim( $extension, ' .' );
},
explode( ',', Utils\get_flag_value( $assoc_args, 'extensions', '' ) )
);

Check warning on line 93 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L88-L93

Added lines #L88 - L93 were not covered by tests

if ( Utils\get_flag_value( $assoc_args, 'pretty-print', false ) ) {
$this->json_options |= JSON_PRETTY_PRINT;
Expand Down Expand Up @@ -119,7 +128,7 @@
/** @var DirectoryIterator $file */
foreach ( $files as $file ) {
if ( $file->isFile() && $file->isReadable() && 'po' === $file->getExtension() ) {
$result = $this->make_json( $file->getRealPath(), $destination, $map, $domain );
$result = $this->make_json( $file->getRealPath(), $destination, $map, $domain, $extensions );

Check warning on line 131 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L131

Added line #L131 was not covered by tests
$result_count += count( $result );

if ( $purge ) {
Expand Down Expand Up @@ -228,13 +237,15 @@
* @param string $destination Path to the destination directory.
* @param array|null $map Source to build file mapping.
* @param string $domain Override text domain to use.
* @param array $extensions Additional extensions.
* @return array List of created JSON files.
*/
protected function make_json( $source_file, $destination, $map, $domain ) {
protected function make_json( $source_file, $destination, $map, $domain, $extensions ) {

Check warning on line 243 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L243

Added line #L243 was not covered by tests
/** @var Translations[] $mapping */
$mapping = [];
$translations = new Translations();
$result = [];
$extensions = array_merge( [ 'js' ], $extensions );

Check warning on line 248 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L248

Added line #L248 was not covered by tests

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

Expand All @@ -251,18 +262,13 @@

// Find all unique sources this translation originates from.
$sources = array_map(
static function ( $reference ) {
$file = $reference[0];

if ( substr( $file, - 7 ) === '.min.js' ) {
return substr( $file, 0, - 7 ) . '.js';
}

if ( substr( $file, - 3 ) === '.js' ) {
return $file;
}
static function ( $reference ) use ( $extensions ) {
$file = $reference[0];
$extension = pathinfo( $file, PATHINFO_EXTENSION );

Check warning on line 267 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L265-L267

Added lines #L265 - L267 were not covered by tests

return null;
return in_array( $extension, $extensions, true )
? preg_replace( "/.min.{$extension}$/", ".{$extension}", $file )
: null;

Check warning on line 271 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L269-L271

Added lines #L269 - L271 were not covered by tests
},
$this->reference_map( $translation->getReferences(), $map )
);
Expand Down