Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 47 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. Used when installing multiple 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,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 );
Expand All @@ -227,19 +254,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