-
Notifications
You must be signed in to change notification settings - Fork 59
Add support for pretty-printing PHP files in wp i18n make-php command #396
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
Changes from 4 commits
d966001
1edfaa0
e2872d2
9c89886
3899f6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,3 +259,63 @@ 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 | ||
| 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 --pretty-print` | ||
| 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 [ | ||
| 'domain' => 'foo-plugin', | ||
| 'plural-forms' => 'nplurals=2; plural=(n != 1);', | ||
| 'language' => 'de_DE', | ||
| 'project-id-version' => 'Foo Plugin', | ||
| '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', | ||
| ], | ||
| ]; | ||
| """ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| class PhpArrayGenerator extends PhpArray { | ||
| public static $options = [ | ||
| 'includeHeaders' => false, | ||
| 'prettyPrint' => false, | ||
| ]; | ||
|
|
||
| /** | ||
|
|
@@ -22,7 +23,15 @@ 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; | ||
|
|
||
| if ( true === $pretty_print ) { | ||
| $exported_array = static::pretty_export( $array ); | ||
| } else { | ||
| $exported_array = static::var_export( $array ); | ||
| } | ||
|
|
||
| return '<?php' . PHP_EOL . ' return ' . $exported_array . ';'; | ||
sovetski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -163,4 +172,36 @@ private static function var_export( $value ) { | |
|
|
||
| return '[' . implode( ',', $entries ) . ']'; | ||
| } | ||
|
|
||
| /** | ||
| * Outputs or returns a parsable string representation of a variable. | ||
| * @since 4.0.0 | ||
sovetski marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @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; | ||
| } | ||
|
|
||
| $result .= $indent . ']'; | ||
| return $result; | ||
| } | ||
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.