From d9660015fc0bb90976f1df054ea650a384b0a3d0 Mon Sep 17 00:00:00 2001 From: Ahliman HUSEYNOV Date: Tue, 9 Apr 2024 21:07:31 +0200 Subject: [PATCH 1/5] Add support for pretty-printing PHP files in wp i18n make-php command --- README.md | 3 +++ features/makephp.feature | 45 +++++++++++++++++++++++++++++++++++++++ src/MakePhpCommand.php | 7 +++++- src/PhpArrayGenerator.php | 22 +++++++++++++------ 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 10adbe2..79c2457 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,9 @@ wp i18n make-php [] [] Path to the destination directory for the resulting PHP files. Defaults to the source directory. + [--pretty-print] + Pretty-print resulting PHP files. + **EXAMPLES** # Create PHP files for all PO files in the current directory. diff --git a/features/makephp.feature b/features/makephp.feature index 15972e2..3407093 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -259,3 +259,48 @@ Feature: Generate PHP files from PO files """ new message """ + + Scenario: Should create pretty-printed PHP files + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin-de_DE.po file: + """ + # Copyright (C) 2018 Foo Plugin + # 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-plugin\n" + "Plural-Forms: nplurals=2; plural=(n != 1);\n" + + #: foo-plugin.js:15 + msgid "Foo Plugin" + msgstr "Foo Plugin" + """ + + When I run `wp i18n make-php foo-plugin` + Then STDOUT should contain: + """ + Success: Created 1 file. + """ + And the return code should be 0 + And the foo-plugin/foo-plugin-de_DE.php file should contain: + """ + 'Foo Plugin', + 'report-msgid-bugs-to' => 'https://wordpress.org/support/plugin/foo-plugin', + 'messages' => + [ + 'Foo Plugin' => 'Foo Plugin', + ], + ]; + """ \ No newline at end of file diff --git a/src/MakePhpCommand.php b/src/MakePhpCommand.php index 3c3b559..825929a 100644 --- a/src/MakePhpCommand.php +++ b/src/MakePhpCommand.php @@ -22,6 +22,9 @@ class MakePhpCommand extends WP_CLI_Command { * [] * : Path to the destination directory for the resulting PHP files. Defaults to the source directory. * + * [--pretty-print] + * : Pretty-print resulting PHP files. + * * ## EXAMPLES * * # Create PHP files for all PO files in the current directory. @@ -58,6 +61,8 @@ public function __invoke( $args, $assoc_args ) { } $result_count = 0; + $pretty_print = Utils\get_flag_value( $assoc_args, 'pretty-print', false ); + /** @var DirectoryIterator $file */ foreach ( $files as $file ) { if ( 'po' !== $file->getExtension() ) { @@ -73,7 +78,7 @@ public function __invoke( $args, $assoc_args ) { $destination_file = "{$destination}/{$file_basename}.l10n.php"; $translations = Translations::fromPoFile( $file->getPathname() ); - if ( ! PhpArrayGenerator::toFile( $translations, $destination_file ) ) { + if ( ! PhpArrayGenerator::toFile( $translations, $destination_file, [ 'prettyPrint' => $pretty_print ] ) ) { WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) ); continue; } diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index d76efc5..399a07b 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -14,6 +14,7 @@ class PhpArrayGenerator extends PhpArray { public static $options = [ 'includeHeaders' => false, + 'prettyPrint' => false, ]; /** @@ -22,7 +23,10 @@ class PhpArrayGenerator extends PhpArray { public static function toString( Translations $translations, array $options = [] ) { $array = static::generate( $translations, $options ); - return ' $val ) { - $entries[] = $is_list ? self::var_export( $val ) : var_export( $key, true ) . '=>' . self::var_export( $val ); + $entries[] = $is_list ? self::var_export( $val, $pretty_print ) : var_export( $key, true ) . '=>' . self::var_export( $val, $pretty_print ); } - return '[' . implode( ',', $entries ) . ']'; + $glue = $pretty_print ? ', ' . PHP_EOL : ','; + $prefix = $pretty_print ? PHP_EOL : ''; + $suffix = $pretty_print ? PHP_EOL : ''; + + return '[' . $prefix . implode( $glue, $entries ) . $suffix . ']'; } } From 1edfaa04789eda966c74027393454e15bd2248e9 Mon Sep 17 00:00:00 2001 From: Ahliman HUSEYNOV Date: Wed, 10 Apr 2024 11:36:19 +0200 Subject: [PATCH 2/5] Update makephp.feature to fix file extension, add missing newline at the end of file and improve test with more entries to test plural one --- features/makephp.feature | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/features/makephp.feature b/features/makephp.feature index 3407093..3fda7ed 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -284,6 +284,16 @@ Feature: Generate PHP files from PO files #: foo-plugin.js:15 msgid "Foo Plugin" msgstr "Foo Plugin" + + #: foo-plugin.js:16 + msgid "Hello" + msgstr "Hallo" + + #: foo-plugin.js:17 + msgid "You have %d new message" + msgid_plural "You have %d new messages" + msgstr[0] "Du hast %d neue Nachricht" + msgstr[1] "Du hast %d neue Nachrichten" """ When I run `wp i18n make-php foo-plugin` @@ -292,7 +302,7 @@ Feature: Generate PHP files from PO files Success: Created 1 file. """ And the return code should be 0 - And the foo-plugin/foo-plugin-de_DE.php file should contain: + And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: """ [ 'Foo Plugin' => 'Foo Plugin', + 'Hello' => 'Hallo', + 'You have %d new message' => 'Du hast %d neue Nachricht', + 'You have %d new messages' => 'Du hast %d neue Nachrichten', ], ]; - """ \ No newline at end of file + """ From e2872d2ea1684ef8a5716c96a9bcc704657ea5a9 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 12 Apr 2024 14:12:23 +0200 Subject: [PATCH 3/5] Actually use `pretty-print` in test Props @ernilambar --- features/makephp.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/makephp.feature b/features/makephp.feature index 3fda7ed..54d5ced 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -296,7 +296,7 @@ Feature: Generate PHP files from PO files msgstr[1] "Du hast %d neue Nachrichten" """ - When I run `wp i18n make-php foo-plugin` + When I run `wp i18n make-php foo-plugin --pretty-print` Then STDOUT should contain: """ Success: Created 1 file. From 9c898863dc14a55984fe86aee4853d9beea94399 Mon Sep 17 00:00:00 2001 From: Ahliman HUSEYNOV Date: Fri, 12 Apr 2024 14:44:09 +0200 Subject: [PATCH 4/5] Fix pretty-printing of PHP files in wp i18n make-php command --- features/makephp.feature | 22 ++++++++------- src/PhpArrayGenerator.php | 57 ++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/features/makephp.feature b/features/makephp.feature index 54d5ced..f750dae 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -305,15 +305,17 @@ Feature: Generate PHP files from PO files And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: """ 'foo-plugin', + 'plural-forms' => 'nplurals=2; plural=(n != 1);', + 'language' => 'de_DE', 'project-id-version' => 'Foo Plugin', - 'report-msgid-bugs-to' => 'https://wordpress.org/support/plugin/foo-plugin', - 'messages' => - [ - 'Foo Plugin' => 'Foo Plugin', - 'Hello' => 'Hallo', - 'You have %d new message' => 'Du hast %d neue Nachricht', - 'You have %d new messages' => 'Du hast %d neue Nachrichten', - ], - ]; + 'pot-creation-date' => '2018-05-02T22:06:24+00:00', + 'po-revision-date' => '2018-05-02T22:06:24+00:00', + 'messages' => [ + 'Foo Plugin' => 'Foo Plugin', + 'Hello' => 'Hallo', + 'You have %d new message' => 'Du hast %d neue Nachricht' . "\0" . 'Du hast %d neue Nachrichten', + ], + ]; """ diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 399a07b..8c765e1 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -23,10 +23,15 @@ class PhpArrayGenerator extends PhpArray { public static function toString( Translations $translations, array $options = [] ) { $array = static::generate( $translations, $options ); - $pretty_print = isset( $options['prettyPrint'] ) ? $options['prettyPrint'] : false; - $exported_array = static::var_export( $array, $pretty_print ); + $pretty_print = isset( $options['prettyPrint'] ) ? $options['prettyPrint'] : false; - return ' $val ) { - $entries[] = $is_list ? self::var_export( $val, $pretty_print ) : var_export( $key, true ) . '=>' . self::var_export( $val, $pretty_print ); + $entries[] = $is_list ? self::var_export( $val ) : var_export( $key, true ) . '=>' . self::var_export( $val ); } - $glue = $pretty_print ? ', ' . PHP_EOL : ','; - $prefix = $pretty_print ? PHP_EOL : ''; - $suffix = $pretty_print ? PHP_EOL : ''; + return '[' . implode( ',', $entries ) . ']'; + } + + /** + * Outputs or returns a parsable string representation of a variable. + * @since 4.0.0 + * + * @param mixed $value The variable you want to export. + * @param int $level The current indentation level. + * @param int $indentation The number of spaces for indentation. Default is 4. + * @return string The variable representation. + */ + private static function pretty_export( $values, $indentation = 2 ) { + $result = '[' . PHP_EOL; + $indent = str_repeat( ' ', $indentation ); + + foreach ( $values as $key => $value ) { + $result .= $indent . str_repeat( ' ', $indentation ) . "'$key' => "; + + if ( is_array( $value ) ) { + $result .= self::pretty_export( $value, $indentation + $indentation ); + } elseif ( strpos( $value, "\0" ) !== false ) { + $parts = explode( "\0", $value ); + $result .= "'" . implode( "' . \"\\0\" . '", array_map( 'addslashes', $parts ) ) . "'"; + } else { + $result .= "'$value'"; + } + + $result .= ',' . PHP_EOL; + } - return '[' . $prefix . implode( $glue, $entries ) . $suffix . ']'; + $result .= $indent . ']'; + return $result; } } From 3899f6b8567370040516064a7949d2ea1a30141e Mon Sep 17 00:00:00 2001 From: Ahliman HUSEYNOV Date: Fri, 12 Apr 2024 17:18:09 +0200 Subject: [PATCH 5/5] Fix indentation in PhpArrayGenerator.php --- features/makephp.feature | 4 ++-- src/PhpArrayGenerator.php | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/features/makephp.feature b/features/makephp.feature index f750dae..23dd2ba 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -305,7 +305,7 @@ Feature: Generate PHP files from PO files And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: """ 'foo-plugin', 'plural-forms' => 'nplurals=2; plural=(n != 1);', 'language' => 'de_DE', @@ -317,5 +317,5 @@ Feature: Generate PHP files from PO files 'Hello' => 'Hallo', 'You have %d new message' => 'Du hast %d neue Nachricht' . "\0" . 'Du hast %d neue Nachrichten', ], - ]; + ]; """ diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 8c765e1..7946c2d 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -31,7 +31,7 @@ public static function toString( Translations $translations, array $options = [] $exported_array = static::var_export( $array ); } - return ' "; if ( is_array( $value ) ) { - $result .= self::pretty_export( $value, $indentation + $indentation ); + $result .= self::pretty_export( $value, $indentation + $indentation, false ); } elseif ( strpos( $value, "\0" ) !== false ) { $parts = explode( "\0", $value ); $result .= "'" . implode( "' . \"\\0\" . '", array_map( 'addslashes', $parts ) ) . "'"; @@ -201,7 +200,7 @@ private static function pretty_export( $values, $indentation = 2 ) { $result .= ',' . PHP_EOL; } - $result .= $indent . ']'; + $result .= $is_top_level ? ']' : $indent . ']'; return $result; } }