diff --git a/features/makejson.feature b/features/makejson.feature index b442d35..0fde9ce 100644 --- a/features/makejson.feature +++ b/features/makejson.feature @@ -917,4 +917,37 @@ Feature: Split PO files into JSON files. "source":"build\/other.js" """ + Scenario: Prefixes the destination file name with the text domain if missing + 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 \n" + "Language-Team: LANGUAGE \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" + """ + + When I run `wp i18n make-json foo-theme` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And the foo-theme/foo-theme-de_DE-557240f2080a0894dbd39f5c2f559bf8.json file should exist diff --git a/src/MakeJsonCommand.php b/src/MakeJsonCommand.php index e80d83d..2f5cacd 100644 --- a/src/MakeJsonCommand.php +++ b/src/MakeJsonCommand.php @@ -137,7 +137,6 @@ public function __invoke( $args, $assoc_args ) { $translations = Translations::fromPoFile( $file->getPathname() ); if ( ! $translations->toMoFile( $destination_file ) ) { WP_CLI::warning( "Could not create file {$destination_file}" ); - continue; } } } @@ -171,7 +170,7 @@ protected function build_map( $paths_or_maps ) { WP_CLI::debug( sprintf( 'Dropping %d invalid values from map argument', count( $paths_or_maps ) - count( $paths ) - count( $maps ) ), 'make-json' ); $to_transform = array_map( - function ( $value, $index ) { + static function ( $value, $index ) { return [ $value, sprintf( 'inline object %d', $index ) ]; }, $maps, @@ -198,7 +197,7 @@ function ( $value, $index ) { $key_num = count( $json ); // normalize contents to string[] $json = array_map( - function ( $value ) { + static function ( $value ) { if ( is_array( $value ) ) { $value = array_values( array_filter( $value, 'is_string' ) ); if ( ! empty( $value ) ) { @@ -240,12 +239,18 @@ protected function make_json( $source_file, $destination, $map ) { $base_file_name = basename( $source_file, '.po' ); - foreach ( $translations as $index => $translation ) { + $domain = $translations->getDomain(); + + if ( $domain && 0 !== strpos( $base_file_name, $domain ) ) { + $base_file_name = "{$domain}-{$base_file_name}"; + } + + foreach ( $translations as $translation ) { /** @var Translation $translation */ // Find all unique sources this translation originates from. $sources = array_map( - function ( $reference ) { + static function ( $reference ) { $file = $reference[0]; if ( substr( $file, - 7 ) === '.min.js' ) { @@ -305,7 +310,7 @@ protected function reference_map( $references, $map ) { // translate using map $temp = array_map( - function ( $reference ) use ( &$map ) { + static function ( $reference ) use ( &$map ) { $file = $reference[0]; if ( array_key_exists( $file, $map ) ) { @@ -325,13 +330,12 @@ function ( $reference ) use ( &$map ) { array_push( $references, ...$sources ); } // and wrap to array - $references = array_map( - function ( $value ) { + return array_map( + static function ( $value ) { return [ $value ]; }, $references ); - return $references; } /**