Skip to content

Commit

Permalink
Merge pull request #313 from wp-cli/fix/197-theme-domain
Browse files Browse the repository at this point in the history
Prefix JSON files with text domain if needed
  • Loading branch information
schlessera authored Jul 4, 2022
2 parents 483662a + f363648 commit 45bc2b4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
33 changes: 33 additions & 0 deletions features/makejson.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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"
"""

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

22 changes: 13 additions & 9 deletions src/MakeJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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 ) ) {
Expand Down Expand Up @@ -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' ) {
Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 45bc2b4

Please sign in to comment.