-
Notifications
You must be signed in to change notification settings - Fork 52
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 wp i18n make-php
command
#363
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9b2c119
Add make-php skeleton
swissspidy aefd457
Set certain headers
swissspidy 96e6a96
Fix tests
swissspidy 9e88ac7
No else
swissspidy d3f06fa
Merge branch 'main' into add/try-make-php
swissspidy 804c005
Update code & tests
swissspidy 48e08ae
Lint fixes
swissspidy b7c57fe
Merge branch 'main' into add/try-make-php
swissspidy f0ce86a
Merge branch 'main' into add/try-make-php
swissspidy 594a91c
Apply suggestions from code review
swissspidy f071aba
Add back dirname
swissspidy 28e85d9
Update file extension
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
"i18n make-pot", | ||
"i18n make-json", | ||
"i18n make-mo", | ||
"i18n make-php", | ||
"i18n update-po" | ||
] | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
Feature: Generate PHP files from PO files | ||
|
||
Background: | ||
Given an empty directory | ||
|
||
Scenario: Bail for invalid source directories | ||
When I try `wp i18n make-php foo` | ||
Then STDERR should contain: | ||
""" | ||
Error: Source file or directory does not exist! | ||
""" | ||
And the return code should be 1 | ||
|
||
Scenario: Uses source folder as destination by default | ||
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 <LL@li.org>\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 exist | ||
|
||
Scenario: Allows setting custom destination directory | ||
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 <LL@li.org>\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 result` | ||
Then STDOUT should contain: | ||
""" | ||
Success: Created 1 file. | ||
""" | ||
And the return code should be 0 | ||
And the result/foo-plugin-de_DE.php file should exist | ||
|
||
Scenario: Does include headers | ||
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 <LL@li.org>\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 STDERR should be empty | ||
And the foo-plugin/foo-plugin-de_DE.php file should contain: | ||
""" | ||
'language'=>'de_DE' | ||
""" | ||
And the foo-plugin/foo-plugin-de_DE.php file should contain: | ||
""" | ||
'domain'=>'foo-plugin' | ||
""" | ||
And the foo-plugin/foo-plugin-de_DE.php file should contain: | ||
""" | ||
'plural-forms'=>'nplurals=2; plural=(n != 1);' | ||
""" | ||
And the foo-plugin/foo-plugin-de_DE.php file should contain: | ||
""" | ||
'messages'=>[''=>['Foo Plugin'=>['Foo Plugin']]] | ||
""" | ||
|
||
Scenario: Does include translations | ||
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 <LL@li.org>\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 "Bar 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: | ||
""" | ||
'messages'=>[''=>['Foo Plugin'=>['Bar Plugin']]] | ||
""" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace WP_CLI\I18n; | ||
|
||
use DirectoryIterator; | ||
use Gettext\Translations; | ||
use IteratorIterator; | ||
use SplFileInfo; | ||
use WP_CLI; | ||
use WP_CLI\Utils; | ||
use WP_CLI_Command; | ||
|
||
class MakePhpCommand extends WP_CLI_Command { | ||
/** | ||
* Create PHP files from PO files. | ||
* | ||
* ## OPTIONS | ||
* | ||
* <source> | ||
* : Path to an existing PO file or a directory containing multiple PO files. | ||
* | ||
* [<destination>] | ||
* : Path to the destination directory for the resulting PHP files. Defaults to the source directory. | ||
* | ||
* ## EXAMPLES | ||
* | ||
* # Create PHP files for all PO files in the current directory. | ||
* $ wp i18n make-php . | ||
* | ||
* # Create a PHP file from a single PO file in a specific directory. | ||
* $ wp i18n make-php example-plugin-de_DE.po languages | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @when before_wp_load | ||
* | ||
* @throws WP_CLI\ExitException | ||
*/ | ||
public function __invoke( $args, $assoc_args ) { | ||
$source = realpath( $args[0] ); | ||
if ( ! $source || ( ! is_file( $source ) && ! is_dir( $source ) ) ) { | ||
WP_CLI::error( 'Source file or directory does not exist!' ); | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$destination = is_file( $source ) ? dirname( $source ) : $source; | ||
if ( isset( $args[1] ) ) { | ||
$destination = $args[1]; | ||
} | ||
|
||
// Two is_dir() checks in case of a race condition. | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( ! is_dir( $destination ) | ||
&& ! mkdir( $destination, 0777, true ) | ||
&& ! is_dir( $destination ) | ||
) { | ||
WP_CLI::error( 'Could not create destination directory!' ); | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if ( is_file( $source ) ) { | ||
$files = [ new SplFileInfo( $source ) ]; | ||
} else { | ||
$files = new IteratorIterator( new DirectoryIterator( $source ) ); | ||
} | ||
|
||
$result_count = 0; | ||
/** @var DirectoryIterator $file */ | ||
foreach ( $files as $file ) { | ||
if ( 'po' !== $file->getExtension() ) { | ||
continue; | ||
} | ||
|
||
if ( ! $file->isFile() || ! $file->isReadable() ) { | ||
WP_CLI::warning( sprintf( 'Could not read file %s', $file->getFilename() ) ); | ||
continue; | ||
} | ||
|
||
$file_basename = basename( $file->getFilename(), '.po' ); | ||
$destination_file = "{$destination}/{$file_basename}.php"; | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$translations = Translations::fromPoFile( $file->getPathname() ); | ||
if ( ! PhpArrayGenerator::toFile( $translations, $destination_file ) ) { | ||
WP_CLI::warning( sprintf( 'Could not create file %s', $destination_file ) ); | ||
continue; | ||
} | ||
|
||
++$result_count; | ||
} | ||
|
||
WP_CLI::success( sprintf( 'Created %d %s.', $result_count, Utils\pluralize( 'file', $result_count ) ) ); | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a test that verifies a valid PHP translation file is produced?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid as in syntax or as in translation gets loaded properly in WordPress?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the latter but we could check both, I suppose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably best done in a future PR because we don't have similar assertions for the other file formats, and the PHP format especially is not yet merged into core.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 👍