Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pretty-printing PHP files in wp i18n make-php command #396

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ wp i18n make-php <source> [<destination>]
[<destination>]
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.
Expand Down
58 changes: 58 additions & 0 deletions features/makephp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,61 @@ 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 <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-plugin\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: foo-plugin.js:15
sovetski marked this conversation as resolved.
Show resolved Hide resolved
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`
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
Then STDOUT should contain:
"""
Success: Created 1 file.
"""
And the return code should be 0
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
<?php
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
return [
'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',
],
];
"""
7 changes: 6 additions & 1 deletion src/MakePhpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class MakePhpCommand extends WP_CLI_Command {
* [<destination>]
* : 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.
Expand Down Expand Up @@ -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() ) {
Expand All @@ -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;
}
Expand Down
22 changes: 16 additions & 6 deletions src/PhpArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class PhpArrayGenerator extends PhpArray {
public static $options = [
'includeHeaders' => false,
'prettyPrint' => false,
];

/**
Expand All @@ -22,7 +23,10 @@ class PhpArrayGenerator extends PhpArray {
public static function toString( Translations $translations, array $options = [] ) {
$array = static::generate( $translations, $options );

return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';';
$pretty_print = isset( $options['prettyPrint'] ) ? $options['prettyPrint'] : false;
$exported_array = static::var_export( $array, $pretty_print );

return '<?php' . PHP_EOL . 'return ' . $exported_array . ';';
}

/**
Expand Down Expand Up @@ -140,15 +144,17 @@ private static function array_is_list( array $arr ) {
/**
* Outputs or returns a parsable string representation of a variable.
*
* Like {@see var_export()} but "minified", using short array syntax
* and no newlines.
* Like {@see var_export()} but can be "minified" or "pretty-printed", using short array syntax.
* By default, it uses no newlines for a compact format ("minified"),
* but can be formatted with newlines and spaces for readability when the 'prettyPrint' option is set.
*
* @since 4.0.0
*
* @param mixed $value The variable you want to export.
* @param bool $pretty_print Whether to pretty-print the output.
* @return string The variable representation.
*/
private static function var_export( $value ) {
private static function var_export( $value, $pretty_print = false ) {
if ( ! is_array( $value ) ) {
return var_export( $value, true );
}
Expand All @@ -158,9 +164,13 @@ private static function var_export( $value ) {
$is_list = self::array_is_list( $value );

foreach ( $value as $key => $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 . ']';
}
}
Loading