Skip to content
47 changes: 47 additions & 0 deletions features/language-core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 49 additions & 1 deletion src/Core_Language_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public function is_installed( $args ) {
* [--activate]
* : If set, the language will be activated immediately after install.
*
* [--format=<format>]
* : Render output in a particular format (for one or more languages).
* ---
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* ## EXAMPLES
*
* # Install the Brazilian Portuguese language.
Expand All @@ -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;
Expand All @@ -208,15 +218,34 @@ 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 ) {
if ( $has_format_flag ) {
$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 );
Expand All @@ -227,19 +256,38 @@ 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;
}
}

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 );
Expand Down
Loading