diff --git a/features/language-core.feature b/features/language-core.feature index 4fe8e115..b3d9b56a 100644 --- a/features/language-core.feature +++ b/features/language-core.feature @@ -476,3 +476,50 @@ Feature: Manage core translation files for a WordPress install | en_US | active | | nl_NL | installed | And STDERR should be empty + + @require-wp-4.0 + Scenario: Install languages with different output formats + Given a WP install + And an empty cache + + When I run `wp language core install de_DE nl_NL --format=csv` + Then the return code should be 0 + And STDOUT should be: + """ + locale,status + de_DE,installed + nl_NL,installed + """ + And STDERR should be empty + + When I run `wp language core install de_DE nl_NL --format=summary` + Then the return code should be 0 + And STDOUT should contain: + """ + Success: Installed 0 of 2 languages (2 skipped). + """ + And STDERR should be empty + + When I run `wp language core install de_DE nl_NL invalid_lang --format=json` + Then the return code should be 0 + And STDOUT should be JSON containing: + """ + [{"locale":"de_DE","status":"already installed"},{"locale":"nl_NL","status":"already installed"},{"locale":"invalid_lang","status":"not available"}] + """ + And STDERR should be empty + + When I run `wp language core install fr_FR --format=table` + Then the return code should be 0 + And STDOUT should contain: + """ + locale + """ + And STDOUT should contain: + """ + fr_FR + """ + And STDOUT should contain: + """ + installed + """ + And STDERR should be empty diff --git a/src/Core_Language_Command.php b/src/Core_Language_Command.php index c1b01af1..7f004678 100644 --- a/src/Core_Language_Command.php +++ b/src/Core_Language_Command.php @@ -183,6 +183,16 @@ public function is_installed( $args ) { * [--activate] * : If set, the language will be activated immediately after install. * + * [--format=] + * : Render output in a particular format. Used when installing multiple languages. + * --- + * options: + * - table + * - csv + * - json + * - summary + * --- + * * ## EXAMPLES * * # Install the Brazilian Portuguese language. @@ -198,7 +208,7 @@ public function is_installed( $args ) { * @subcommand install * * @param string[] $args Positional arguments. - * @param array{activate?: bool} $assoc_args Associative arguments. + * @param array{activate?: bool, format?: string} $assoc_args Associative arguments. */ public function install( $args, $assoc_args ) { $language_codes = (array) $args; @@ -208,15 +218,32 @@ public function install( $args, $assoc_args ) { WP_CLI::error( 'Only a single language can be active.' ); } + $has_format_flag = isset( $assoc_args['format'] ); + + if ( $has_format_flag ) { + if ( in_array( $assoc_args['format'], array( 'json', 'csv' ), true ) ) { + $logger = new \WP_CLI\Loggers\Quiet(); + \WP_CLI::set_logger( $logger ); + } + } + $available = $this->get_installed_languages(); + $results = array(); + $successes = 0; $errors = 0; $skips = 0; foreach ( $language_codes as $language_code ) { + $result = [ + 'locale' => $language_code, + ]; if ( in_array( $language_code, $available, true ) ) { \WP_CLI::log( "Language '{$language_code}' already installed." ); + if ( $has_format_flag ) { + $result['status'] = 'already installed'; + } ++$skips; } else { $response = $this->download_language_pack( $language_code ); @@ -227,12 +254,21 @@ public function install( $args, $assoc_args ) { // Skip if translation is not yet available. if ( 'not_found' === $response->get_error_code() ) { + if ( $has_format_flag ) { + $result['status'] = 'not available'; + } ++$skips; } else { + if ( $has_format_flag ) { + $result['status'] = 'not installed'; + } ++$errors; } } else { \WP_CLI::log( "Language '{$language_code}' installed." ); + if ( $has_format_flag ) { + $result['status'] = 'installed'; + } ++$successes; } } @@ -240,6 +276,16 @@ public function install( $args, $assoc_args ) { if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'activate' ) ) { $this->activate_language( $language_code ); } + + if ( $has_format_flag ) { + $results[] = (object) $result; + } + } + + if ( $has_format_flag ) { + if ( 'summary' !== $assoc_args['format'] ) { + \WP_CLI\Utils\format_items( $assoc_args['format'], $results, array( 'locale', 'status' ) ); + } } \WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );