WP-CLI uses the same locale as the WordPress installation, so if you deal with multiple locales, it might be tricky to follow the output. Luckily, the official documentation shows how to set a specific locale. Here’s a quick summary, plus an important caveat I found:

1. Create a file called force-locale.php and set your desired output locale.

<?php
WP_CLI::add_wp_hook( 'pre_option_WPLANG', function() {
    return 'en_US';
});
force-locale.php

2. Add this file to the beginning of your WP-CLI commands. No need to keep it in the same folder as your WordPress site.

wp --require=$HOME/workspace/wp-tools/force-locale.php
Bash

Here’s an example of the output for a WordPress site with the pt_BR locale, both with and without the parameter:

The standard output is a mix of English and localized messages.

$ wp plugin install wordpress-seo; wp plugin delete wordpress-seo

Installing Yoast SEO (25.0)
Baixando pacote de instalação a partir de https://downloads.wordpress.org/plugin/wordpress-seo.25.0.zip...
Using cached file '/home/kossmann/.wp-cli/cache/plugin/wordpress-seo-25.0.zip'...
Descompactando o pacote...
Instalando o plugin...
Plugin instalado com sucesso.
Success: Installed 1 of 1 plugins.
Deleted 'wordpress-seo' plugin.
Success: Deleted 1 of 1 plugins.
Bash

With force-locale.php, the entire output is in the desired locale (en_US).

$ wp --require=$HOME/workspace/wp-tools/force-locale.php plugin install wordpress-seo; wp --require=$HOME/workspace/wp-tools/force-locale.php plugin delete wordpress-seo

Installing Yoast SEO (25.0)
Downloading installation package from https://downloads.wordpress.org/plugin/wordpress-seo.25.0.zip...
Using cached file '/home/kossmann/.wp-cli/cache/plugin/wordpress-seo-25.0.zip'...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.
Deleted 'wordpress-seo' plugin.
Success: Deleted 1 of 1 plugins.
Bash

Make sure to test this parameter before adding it to all your WP-CLI commands. I had some issues before when I added it to every command in my WordPress reset script, and it broke. Tested again now and it worked, so I’m not sure if it was a bug or if some commands just don’t support the parameter.

Since I use this now and then, I added force-locale.php to my wp-tools repo.

🤖 Clarification on the use of Artificial Intelligence in this content

  • 📝 AI used for: text correction;
  • 🧠 Model: ChatGPT 4.1;
  • 🧐 Human Reviewed: Yes.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *