Skip to content

Commit 234783f

Browse files
Copilotswissspidy
andcommitted
Add --format argument support to core install command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 662ee1f commit 234783f

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎features/language-core.feature‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,50 @@ Feature: Manage core translation files for a WordPress install
476476
| en_US | active |
477477
| nl_NL | installed |
478478
And STDERR should be empty
479+
480+
@require-wp-4.0
481+
Scenario: Install languages with different output formats
482+
Given a WP install
483+
And an empty cache
484+
485+
When I run `wp language core install de_DE nl_NL --format=csv`
486+
Then the return code should be 0
487+
And STDOUT should be:
488+
"""
489+
locale,status
490+
de_DE,installed
491+
nl_NL,installed
492+
"""
493+
And STDERR should be empty
494+
495+
When I run `wp language core install de_DE nl_NL --format=summary`
496+
Then the return code should be 0
497+
And STDOUT should contain:
498+
"""
499+
Success: Installed 0 of 2 languages (2 skipped).
500+
"""
501+
And STDERR should be empty
502+
503+
When I run `wp language core install de_DE nl_NL invalid_lang --format=json`
504+
Then the return code should be 0
505+
And STDOUT should be JSON containing:
506+
"""
507+
[{"locale":"de_DE","status":"already installed"},{"locale":"nl_NL","status":"already installed"},{"locale":"invalid_lang","status":"not available"}]
508+
"""
509+
And STDERR should be empty
510+
511+
When I run `wp language core install fr_FR --format=table`
512+
Then the return code should be 0
513+
And STDOUT should contain:
514+
"""
515+
locale
516+
"""
517+
And STDOUT should contain:
518+
"""
519+
fr_FR
520+
"""
521+
And STDOUT should contain:
522+
"""
523+
installed
524+
"""
525+
And STDERR should be empty

‎src/Core_Language_Command.php‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ public function is_installed( $args ) {
183183
* [--activate]
184184
* : If set, the language will be activated immediately after install.
185185
*
186+
* [--format=<format>]
187+
* : Render output in a particular format. Used when installing multiple languages.
188+
* ---
189+
* default: table
190+
* options:
191+
* - table
192+
* - csv
193+
* - json
194+
* - summary
195+
* ---
196+
*
186197
* ## EXAMPLES
187198
*
188199
* # Install the Brazilian Portuguese language.
@@ -198,7 +209,7 @@ public function is_installed( $args ) {
198209
* @subcommand install
199210
*
200211
* @param string[] $args Positional arguments.
201-
* @param array{activate?: bool} $assoc_args Associative arguments.
212+
* @param array{activate?: bool, format: string} $assoc_args Associative arguments.
202213
*/
203214
public function install( $args, $assoc_args ) {
204215
$language_codes = (array) $args;
@@ -208,15 +219,30 @@ public function install( $args, $assoc_args ) {
208219
WP_CLI::error( 'Only a single language can be active.' );
209220
}
210221

222+
if ( empty( $assoc_args['format'] ) ) {
223+
$assoc_args['format'] = 'table';
224+
}
225+
226+
if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) {
227+
$logger = new \WP_CLI\Loggers\Quiet();
228+
\WP_CLI::set_logger( $logger );
229+
}
230+
211231
$available = $this->get_installed_languages();
212232

233+
$results = array();
234+
213235
$successes = 0;
214236
$errors = 0;
215237
$skips = 0;
216238
foreach ( $language_codes as $language_code ) {
239+
$result = [
240+
'locale' => $language_code,
241+
];
217242

218243
if ( in_array( $language_code, $available, true ) ) {
219244
\WP_CLI::log( "Language '{$language_code}' already installed." );
245+
$result['status'] = 'already installed';
220246
++$skips;
221247
} else {
222248
$response = $this->download_language_pack( $language_code );
@@ -227,19 +253,28 @@ public function install( $args, $assoc_args ) {
227253

228254
// Skip if translation is not yet available.
229255
if ( 'not_found' === $response->get_error_code() ) {
256+
$result['status'] = 'not available';
230257
++$skips;
231258
} else {
259+
$result['status'] = 'not installed';
232260
++$errors;
233261
}
234262
} else {
235263
\WP_CLI::log( "Language '{$language_code}' installed." );
264+
$result['status'] = 'installed';
236265
++$successes;
237266
}
238267
}
239268

240269
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'activate' ) ) {
241270
$this->activate_language( $language_code );
242271
}
272+
273+
$results[] = (object) $result;
274+
}
275+
276+
if ( 'summary' !== $assoc_args['format'] ) {
277+
\WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'locale', 'status' ) );
243278
}
244279

245280
\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );

0 commit comments

Comments
 (0)