Force WP-CLI output to be in English

I need to manage several insensate of WordPress installations, some of them are in Hebrew and one in Arabic, and when using `wp-cli` to manage the installation, e.g when running `plugin update`, `wp-cli` will use WordPress translation strings with the current WP installed language. So for example a Hebrew WP installation will look like this:

WP-CLI terminal output in Hebrew

Using WP-CLI with a terminal not supporting BiDi

One way to over come this, is to use a terminal which support BiDi (e.g mlterm or Konsole)

WP-CLI with BiDi suported terminal

Using WP-CLI with a terminal (like mlterm) supporting BiDi

That’s make is a bit better, but still I prefer all my terminal work to be in English.

I tried to add a snippet of code to `wp-config.php` to detect if the WP installation is running via `WP-CLI` and force the `WPLANG` to be `en_US`but that didn’t work either. So I googled a bit more and didn’t found any way to resolve this my self, so I asked for some help in the `wp-cli` slack channel and they gave me a work around which seems to work, and they also open an issue (which you are welcome to support to push this forward) in github about defaulting to English for `wp-cli` in terminal.

TL;DR

Any way, the short answer is to use the `–require` option of `wp-cli` to load a file which uses a hook to set `WPLANG` before any one set it.

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

And you can run all your commands with:

wp --require=force-locale.php {...}

or you can go one step forward, and set it as default for all wp commands by setting it in the global `~/.wp-cli/config.yml` file

require:
  - /home/wp/.wp-cli/force-locale.php

 

You may also like...

2 Responses

  1. Hatul says:

    Thanks but if I setting the WPLANG to english then the translation not update.

    wp --require=force-locale.php core language update
    Translations updates are not needed for the 'English (US)' locale.
    

Leave a Reply to Rabin Cancel reply

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