Auto translate WordPress theme and plugin strings with WP CLI

How to use?

  • Create fresh WordPress setup.
  • Clone the plugin wp-i18n into your /wp-content/plugins/ directory.
  • Import the database latest.sql with WP CLI commnd as below:
    wp db import latest.sql
  • Now use below commands to translate any plugin/theme from wp.org.

To translate the plugin use 2 WP CLI commands:

wp wpi18n download_plugin_po {slug} --lang={mr}
wp wpi18n generate_plugin_po {slug} --lang={mr}

Here,

E.g. To translate the Contact Form 7 plugin we can use below commands:

wp wpi18n download_plugin_po contact-form-7  --lang=mr
wp wpi18n generate_plugin_po contact-form-7  --lang=mr

First command download the .po file of plugin.
And second command generate the translated strings .po file.


And, To translate the theme use below WP CLI command.

wp wpi18n generate_theme_po {slug} --lang={mr}
wp wpi18n generate_theme_po  {slug} --lang={mr}

Example 2: In below example I’m translating ShortcodeHub plugin.

E.g. To download the plugin .po files Im using command wp wpi18n download_plugin_po shortcodehub --lang=mr

D:\xampp\htdocs\wpi18n\wp-content\plugins\wp-i18n (master)
λ wp wpi18n download_plugin_po shortcodehub --lang=mr
shortcodehub | CREATED - po-files/plugins/shortcodehub/mr/wp-plugins-shortcodehub-dev-mr.po | https://translate.wordpress.org/projects/wp-plugins/shortcodehub/dev/mr/default/export-translations
shortcodehub | CREATED - po-files/plugins/shortcodehub/mr/wp-plugins-shortcodehub-dev-readme-mr.po | https://translate.wordpress.org/projects/wp-plugins/shortcodehub/dev-readme/mr/default/export-translations
shortcodehub | CREATED - po-files/plugins/shortcodehub/mr/wp-plugins-shortcodehub-stable-mr.po | https://translate.wordpress.org/projects/wp-plugins/shortcodehub/stable/mr/default/export-translations
shortcodehub | CREATED - po-files/plugins/shortcodehub/mr/wp-plugins-shortcodehub-stable-readme-mr.po | https://translate.wordpress.org/projects/wp-plugins/shortcodehub/stable-readme/mr/default/export-translations

Now, Im using command wp wpi18n generate_plugin_po shortcodehub --lang=mr to auto generate the .po files.

E.g. It looks like below:

D:\xampp\htdocs\wpi18n\wp-content\plugins\wp-i18n (master)
λ wp wpi18n generate_plugin_po shortcodehub --lang=mr
shortcodehub | 4 | 178 | 4508 UPDATED Documentation | WITH डोक्युमेंटशन
shortcodehub | 4 | 177 | 0 NOT EXIST Support
...
...
shortcodehub | 1 | 2 | 0 NOT EXIST Fix: Updated wrong 5 star rating link from dashboard page
shortcodehub | 1 | 1 | 0 NOT EXIST Initial release
shortcodehub | 1 | COMPLETE - TRANSLATED 36 STRINGS
shortcodehub | 1 | po-files/plugins/shortcodehub/mr/wp-plugins-shortcodehub-stable-readme-mr-translated.po
shortcodehub | TOTAL TRANSLATED 36
ALL TRANSLATED STRINGS COUNT: 2901

Screenshot for reference:

After executing both commands we have 36 auto translated strings. Also we have 2901 available strings in Marathi language.

All translated strings are stored in directory:
/wp-i18n/po-files/plugins/shortcodehub/mr/

Now file wp-plugins-shortcodehub-dev-mr.po is original file and wp-plugins-shortcodehub-dev-mr-translated.po is translated file.

We can use this auto translated file and import into Glotpress.

To confirm the translated strings the log file is generated which show all the translated strings. E.g.
/wp-i18n/po-files/plugins/shortcodehub/mr/log.txt

This file show the content like below:

1 | Documentation | डोक्युमेंटशन
2 | Version %s | आवृत्ती %s
...
...
36 | Features | वैशिष्ट्ये

How it works?

In WordPress there are lot of strings are 100% translated. See https://translate.wordpress.org/stats/

I notice that there are lot of common strings which are already translated in WordPress core releases.

E.g. Strings Leave a comment, Comment Content, Comment Date, Completed are some of the common strings examples which already translated into WordPress core releases.

In this article I’m giving an example of Marathi (मराठी) (mr) which have now 160 contributors.

In Marathi language there are 9+ core release:

  • dev
  • 4.8.x
  • 4.7.x
  • 4.6.x
  • 4.5.x
  • 4.4.x
  • 4.3.x
  • 4.3.x
  • 4.1.x

Each release have 4 sub projects:

So, For dev project core release have 4 sub projects such as:

https://translate.wordpress.org/projects/wp/dev/mr/default/
https://translate.wordpress.org/projects/wp/dev/cc/mr/default
https://translate.wordpress.org/projects/wp/dev/admin/mr/default
https://translate.wordpress.org/projects/wp/dev/admin/network/mr/default

And, Core version 4.8.x have 4 sub projects such as:

https://translate.wordpress.org/projects/wp/4.8.x/ru/default
https://translate.wordpress.org/projects/wp/4.8.x/cc/ru/default
https://translate.wordpress.org/projects/wp/4.8.x/admin/ru/default
https://translate.wordpress.org/projects/wp/4.8.x/admin/network/ru/default

And so on.


For each sub project we have a Export option.

With this option we can export the translated project translated strings in below formats:

  • Android XML (.xml)
  • Portable Object Message Catalog (.po/.pot)
  • Machine Object Message Catalog (.mo)
  • .NET Resource (.resx)
  • Mac OS X / iOS Strings File (.strings)
  • Java Properties File (.properties)
  • JSON (.json)
  • Jed 1.x (.json)
  • NGX-Translate (.json)

Development Background

I have create a fresh WordPress setup and created a plugin wpi18n.

In this plugin I have register a post type which store the original string as a post title.

And its translated language version in post meta.

E.g.

Now,

  • I have downloaded the .po files of all the core projects from 4.1.x to dev with the help of WP CLI command wp wpi18n download_all_core_files

We have all the translated files of each core release of each language. To process all languages it takes too much time. So, I have added the individual language support for importing the translated strings.

The CLI command wp wpi18n import_translated_strings mr download the original & translated string in Marathi language.

After importing strings, I notice that there are lot of strings which are translated as different in different core projects.

E.g. The string Leave a comment is translated as below:

  • टिपणी करा (4-1-x)
  • प्रतिक्रिया लिहा (4-2-x)
  • टिपणी करा (4-3-x)
  • टिपणी करा (4-4-x)
  • टिपणी करा (4-5-x)
  • टिपणी करा (4-6-x)
  • टिपणी करा (4-7-x)
  • टिपणी करा (4-8-x)
  • टिपणी करा (4-9-x-development)

So, To keep the consistency I have used most repetitive strings as a translated strings. E.g.

  • (8 times) टिपणी करा
  • (1 times) प्रतिक्रिया लिहा

So, I have used string टिपणी करा to auto translate the string Leave a comment in theme and plugin.

Some other examples:

Example 2: String – Password: have 3 variations in Marathi (mr) language as below:

(4 times) कूटशब्द:
(2 times) पासवर्डः
(10 times) पासवर्ड:
--------- FROM PROJECTS ---------
(4-1-x) कूटशब्द:
(4-2-x) कूटशब्द:
(4-3-x) पासवर्डः
(4-5-x) पासवर्ड:
(4-6-x) पासवर्ड:
(4-7-x) पासवर्ड:
(4-8-x) पासवर्ड:
(4-9-x-development) पासवर्ड:

Example 3: String – Sites have 3 variations in Russian (ru) language as below:

(1 times) сайтов
(3 times) Сайты
--------- FROM PROJECTS ---------
(4-1-x-administration-network-admin) сайтов
(4-1-x) Сайты
(4-2-x) Сайты
(4-3-x) Сайты

To detect all the repeated strings I have created the command which generate the file of all repeated strings.


To discuss with other teammates and confirm it works for all languages I have created a log file which show the original string and its log of repetitive strings with most repetitive string.

Below are the some language files:

To translate the string into any language I have create a command which set the most repeated string into the meta box.

E.g. String Sites is most 3 time repeated with Сайты So, I have set this most repeated string and used it while translating the string with CLI.

I have created some commands to auto generate the .po file of any theme/plugin.

// Commands to download the .po files of theme/plugin.
wp wpi18n download_plugin_po {plugin-slug} --lang={language}
wp wpi18n download_theme_po {theme-slug} --lang={language}
// Commands to generate the .po files of theme/plugin.
wp wpi18n generate_theme_po {theme-slug} --lang={language}
wp wpi18n generate_plugin_po {plugin-slug} --lang={language}
// Other commands.
wp wpi18n translate_all_plugin_files --lang={language}
wp wpi18n generate_blank_lang_po {language}
wp wpi18n generate_filled_lang_po {language}
wp wpi18n import_translated_strings {language}
wp wpi18n set_most_repeated_string_as_top {language}
wp wpi18n detect_all_repeated_string {language}

To confirm strings, I have first used my own native language and I found that its 100% works. After that I have tried another language.

After translating plugins and themes I found that the Import Translation feature just import any .po file and detect the string into it.

Finally, I have create each language .po file which set the most repeated string as a translation string. And imported it into the theme/plugin. And it works as expected.


Known Issues

After discuss with yui, I found that the repeated strings are depends on plural/singular, male, female or neutral or sometimes its participle highly depends on context.

The string Sites is translated as in (4-1-x-administration-network-admin) as сайтов because it has the context.

And in (4-1-x) its Сайты because there is no context.

For reference see 4-1-x-administration.

While translating strings It does not check its context. I’ll discuss with community and maybe change this or maybe implement another way to do this same thing.


GlotPress Background

WordPress use the GlotPress to translate the strings from WordPress Core, Themes, Plugins etc.

In GlotPress Roadmap there is a target Translation Memory which is scheduled for version x+5. It is the almost same as reusing strings from existing approved strings.

I have read about this from below reference links:


Whats next?

I’ll discuss this with the Polyglot team on slack channel. You can also try this for your theme/plugin. Its super easy. Just you wan’t to setup the WP CLI on your local environment.

If you don’t have setup the WP CLI then read How to setup WP CLI (WordPress Command Line Interface).

Setup WP CLI (WordPress Command Line Interface) on Windows Operating System

Step 1

Create folder wp-cli in C drive.

Step 2

Download wp-cli.phar from https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Step 3

Move downloaded wp-cli.phar file in C:\wp-cli\

Step 4

Create wp.bat file in C:\wp-cli\ and write below code in it.

@ECHO OFF
php "C:\wp-cli\wp-cli.phar" %*

Or download file from gist https://gist.github.com/maheshwaghmare/848b5a9f21f27239600cf08693a0bedb

Step 5

Open system environment and set C:\wp-cli as environment path.

Done!


Open Command Prompt and type command wp and press Enter. It’ll show you the like the below screenshot.


Not work on Git Bash?

Create another file wp without any extension into the C:\wp-cli\ directory and paste below code:

#!/usr/bin/env sh

dir=$(d=${0%[/\\]*}; cd "$d"; pwd)

# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
   # Cygwin paths start with /cygdrive/ which will break windows PHP,
   # so we need to translate the dir path to windows format. However
   # we could be using cygwin PHP which does not require this, so we
   # test if the path to PHP starts with /cygdrive/ rather than /usr/bin
   if [[ $(which php) == /cygdrive/* ]]; then
       dir=$(cygpath -m $dir);
   fi
fi

dir=$(echo $dir | sed 's/ /\ /g')
"${dir}/wp-cli.phar" "$@"

Great!

Now open git bash and type command wp Eg.

Source https://deluxeblogtips.com/install-wp-cli-windows/

Thanks for the suggestions Uttam Sharma!

Keyboard shortcut/aliases for the WP CLI, Git, Grunt & PHPCS commands for windows

In development, We spend a lot of time on the command line. We type a lot of commands on regularly.

I have created a shortcut for the most of the commands which I use on a daily basis some of them are below:

WP CLI

Command Keyboard Shortcut/Aliases
wp theme list wpthl
wp plugin list wppll

Check all the available WP CLI commands etc. (all WP CLI commands list)

Example 1. Using shortcut wpthl instead of wp theme list

Example 2. Using shortcut wppll instead of wp plugin list

Some more commands with keyboard shortcuts/aliases are below:

Grunt

Command Keyboard Shortcut/Aliases
grunt release grr
grunt minify grm

PHPCS

Command Keyboard Shortcut/Aliases
phpcs pb
phpcbf pbf

Git

Command Keyboard Shortcut/Aliases
git add . gaa
git commit –message gcm
git checkout gco
git pull origin gpu
git push origin gpp
git status gs

To create keyboard shortcuts using aliases is very simple. We can create any command shortcut/aliases.

Follow below simple steps:

  1. Open the file .bashrc which is found in location  C:\Users\USERNAME\.bashrc and
  2. Add below sample commands of Git & Grunt.

I found the article Terminal/Bash Command-Line Shortcuts with Aliases written by @jonsuh to create keyboard shortcuts for the Terminal/Git Bash.

I have added some more commands of Grunt, WP CLI & PHPCS on my local environment.

WP CLI Commands Cheat Sheet (Complete List of WP CLI Commands)

WP-CLI is the command-line interface for WordPress. You can update plugins, configure multisite installs and much more, without using a web browser.

Below is the complete list of WP CLI commands generated with WP CLI Commands Cheat Sheet (Complete List of WP CLI Commands)

Cache
wp cache add Docs | Git
wp cache decr Docs | Git
wp cache delete Docs | Git
wp cache flush Docs | Git
wp cache get Docs | Git
wp cache incr Docs | Git
wp cache replace Docs | Git
wp cache set Docs | Git
wp cache type Docs | Git
Cap
wp cap add Docs | Git
wp cap list Docs | Git
wp cap remove Docs | Git
Cli
wp cli alias Docs | Git
wp cli check-update Docs | Git
wp cli cmd-dump Docs | Git
wp cli completions Docs | Git
wp cli has-command Docs | Git
wp cli info Docs | Git
wp cli param-dump Docs | Git
wp cli update Docs | Git
wp cli version Docs | Git
Comment
wp comment approve Docs | Git
wp comment count Docs | Git
wp comment create Docs | Git
wp comment delete Docs | Git
wp comment exists Docs | Git
wp comment generate Docs | Git
wp comment get Docs | Git
wp comment list Docs | Git
wp comment meta Docs | Git
wp comment recount Docs | Git
wp comment spam Docs | Git
wp comment status Docs | Git
wp comment trash Docs | Git
wp comment unapprove Docs | Git
wp comment unspam Docs | Git
wp comment untrash Docs | Git
wp comment update Docs | Git
wp comment meta add Docs | Git
wp comment meta delete Docs | Git
wp comment meta get Docs | Git
wp comment meta list Docs | Git
wp comment meta patch Docs | Git
wp comment meta pluck Docs | Git
wp comment meta update Docs | Git
Config
wp config create Docs | Git
wp config delete Docs | Git
wp config get Docs | Git
wp config has Docs | Git
wp config list Docs | Git
wp config path Docs | Git
wp config set Docs | Git
Core
wp core check-update Docs | Git
wp core download Docs | Git
wp core install Docs | Git
wp core is-installed Docs | Git
wp core multisite-convert Docs | Git
wp core multisite-install Docs | Git
wp core update-db Docs | Git
wp core update Docs | Git
wp core verify-checksums Docs | Git
wp core version Docs | Git
Cron
wp cron event Docs | Git
wp cron schedule Docs | Git
wp cron test Docs | Git
wp cron event delete Docs | Git
wp cron event list Docs | Git
wp cron event run Docs | Git
wp cron event schedule Docs | Git
wp cron schedule list Docs | Git
Db
wp db check Docs | Git
wp db cli Docs | Git
wp db create Docs | Git
wp db drop Docs | Git
wp db export Docs | Git
wp db import Docs | Git
wp db optimize Docs | Git
wp db prefix Docs | Git
wp db query Docs | Git
wp db repair Docs | Git
wp db reset Docs | Git
wp db search Docs | Git
wp db size Docs | Git
wp db tables Docs | Git
Embed
wp embed cache Docs | Git
wp embed fetch Docs | Git
wp embed handler Docs | Git
wp embed provider Docs | Git
wp embed cache clear Docs | Git
wp embed cache find Docs | Git
wp embed cache trigger Docs | Git
wp embed handler list Docs | Git
wp embed provider list Docs | Git
wp embed provider match Docs | Git
Language
wp language core Docs | Git
wp language core activate Docs | Git
wp language core install Docs | Git
wp language core list Docs | Git
wp language core uninstall Docs | Git
wp language core update Docs | Git
Media
wp media image-size Docs | Git
wp media import Docs | Git
wp media regenerate Docs | Git
Menu
wp menu create Docs | Git
wp menu delete Docs | Git
wp menu item Docs | Git
wp menu list Docs | Git
wp menu location Docs | Git
wp menu item add-custom Docs | Git
wp menu item add-post Docs | Git
wp menu item add-term Docs | Git
wp menu item delete Docs | Git
wp menu item list Docs | Git
wp menu item update Docs | Git
wp menu location assign Docs | Git
wp menu location list Docs | Git
wp menu location remove Docs | Git
Network
wp network meta Docs | Git
wp network meta add Docs | Git
wp network meta delete Docs | Git
wp network meta get Docs | Git
wp network meta list Docs | Git
wp network meta patch Docs | Git
wp network meta pluck Docs | Git
wp network meta update Docs | Git
Option
wp option add Docs | Git
wp option delete Docs | Git
wp option get Docs | Git
wp option list Docs | Git
wp option patch Docs | Git
wp option pluck Docs | Git
wp option update Docs | Git
Package
wp package browse Docs | Git
wp package install Docs | Git
wp package list Docs | Git
wp package path Docs | Git
wp package uninstall Docs | Git
wp package update Docs | Git
Plugin
wp plugin activate Docs | Git
wp plugin deactivate Docs | Git
wp plugin delete Docs | Git
wp plugin get Docs | Git
wp plugin install Docs | Git
wp plugin is-installed Docs | Git
wp plugin list Docs | Git
wp plugin path Docs | Git
wp plugin search Docs | Git
wp plugin status Docs | Git
wp plugin toggle Docs | Git
wp plugin uninstall Docs | Git
wp plugin update Docs | Git
wp plugin verify-checksums Docs | Git
Post-type
wp post-type get Docs | Git
wp post-type list Docs | Git
Post
wp post create Docs | Git
wp post delete Docs | Git
wp post edit Docs | Git
wp post generate Docs | Git
wp post get Docs | Git
wp post list Docs | Git
wp post meta Docs | Git
wp post term Docs | Git
wp post update Docs | Git
wp post meta add Docs | Git
wp post meta delete Docs | Git
wp post meta get Docs | Git
wp post meta list Docs | Git
wp post meta patch Docs | Git
wp post meta pluck Docs | Git
wp post meta update Docs | Git
wp post term add Docs | Git
wp post term list Docs | Git
wp post term remove Docs | Git
wp post term set Docs | Git
Profile
wp profile eval-file Docs | Git
wp profile eval Docs | Git
wp profile hook Docs | Git
wp profile stage Docs | Git
Rewrite
wp rewrite flush Docs | Git
wp rewrite list Docs | Git
wp rewrite structure Docs | Git
Role
wp role create Docs | Git
wp role delete Docs | Git
wp role exists Docs | Git
wp role list Docs | Git
wp role reset Docs | Git
Scaffold
wp scaffold _s Docs | Git
wp scaffold block Docs | Git
wp scaffold child-theme Docs | Git
wp scaffold plugin-tests Docs | Git
wp scaffold plugin Docs | Git
wp scaffold post-type Docs | Git
wp scaffold taxonomy Docs | Git
wp scaffold theme-tests Docs | Git
Sidebar
wp sidebar list Docs | Git
Site
wp site activate Docs | Git
wp site archive Docs | Git
wp site create Docs | Git
wp site deactivate Docs | Git
wp site delete Docs | Git
wp site empty Docs | Git
wp site list Docs | Git
wp site mature Docs | Git
wp site option Docs | Git
wp site private Docs | Git
wp site public Docs | Git
wp site spam Docs | Git
wp site unarchive Docs | Git
wp site unmature Docs | Git
wp site unspam Docs | Git
wp site option add Docs | Git
wp site option delete Docs | Git
wp site option get Docs | Git
wp site option list Docs | Git
wp site option patch Docs | Git
wp site option pluck Docs | Git
wp site option update Docs | Git
Super-admin
wp super-admin add Docs | Git
wp super-admin list Docs | Git
wp super-admin remove Docs | Git
Taxonomy
wp taxonomy get Docs | Git
wp taxonomy list Docs | Git
Term
wp term create Docs | Git
wp term delete Docs | Git
wp term generate Docs | Git
wp term get Docs | Git
wp term list Docs | Git
wp term meta Docs | Git
wp term recount Docs | Git
wp term update Docs | Git
wp term meta add Docs | Git
wp term meta delete Docs | Git
wp term meta get Docs | Git
wp term meta list Docs | Git
wp term meta patch Docs | Git
wp term meta pluck Docs | Git
wp term meta update Docs | Git
Theme
wp theme activate Docs | Git
wp theme delete Docs | Git
wp theme disable Docs | Git
wp theme enable Docs | Git
wp theme get Docs | Git
wp theme install Docs | Git
wp theme is-installed Docs | Git
wp theme list Docs | Git
wp theme mod Docs | Git
wp theme path Docs | Git
wp theme search Docs | Git
wp theme status Docs | Git
wp theme update Docs | Git
wp theme mod get Docs | Git
wp theme mod remove Docs | Git
wp theme mod set Docs | Git
Transient
wp transient delete Docs | Git
wp transient get Docs | Git
wp transient set Docs | Git
wp transient type Docs | Git
User
wp user add-cap Docs | Git
wp user add-role Docs | Git
wp user create Docs | Git
wp user delete Docs | Git
wp user generate Docs | Git
wp user get Docs | Git
wp user import-csv Docs | Git
wp user list-caps Docs | Git
wp user list Docs | Git
wp user meta Docs | Git
wp user remove-cap Docs | Git
wp user remove-role Docs | Git
wp user reset-password Docs | Git
wp user session Docs | Git
wp user set-role Docs | Git
wp user spam Docs | Git
wp user term Docs | Git
wp user unspam Docs | Git
wp user update Docs | Git
wp user meta add Docs | Git
wp user meta delete Docs | Git
wp user meta get Docs | Git
wp user meta list Docs | Git
wp user meta patch Docs | Git
wp user meta pluck Docs | Git
wp user meta update Docs | Git
wp user session destroy Docs | Git
wp user session list Docs | Git
wp user term add Docs | Git
wp user term list Docs | Git
wp user term remove Docs | Git
wp user term set Docs | Git
Widget
wp widget add Docs | Git
wp widget deactivate Docs | Git
wp widget delete Docs | Git
wp widget list Docs | Git
wp widget move Docs | Git
wp widget reset Docs | Git
wp widget update Docs | Git