Generally speaking, task runners like Gulp or Grunt are best for generating translation files on a continuous basis, but what if you need to just generate a quick translation file once? You can easily do so with WP-CLI.
Generating POT Translation Files
To generate a POT translation file for your WordPress plugin or theme, you can use this command:
pagely-wp i18n make-pot wp-content/plugins/my-plugin-dir
Defining a Destination
By default, WP-CLI will place the resulting POT file into the languages/plugin-name.pot. If you want to place it inside a different location, you can define it as an additional option after the source directory like this:
pagely-wp i18n make-pot wp-content/plugins/my-plugin-dir destination/plugin-name.pot
Including Specific Files
What if you want to generate translation strings for only a specific file or set of files? By using the --include option, only those files will be used for generating your POT file.
pagely-wp i18n make-pot . --include"only/scan/this/dir,and/this/file.php"
Simple glob patterns can also be used like in the following example:
pagely-wp i18n make-pot . --include"included-*.php"
Excluding Files
If you want to exclude certain files from being scanned for translation strings, you can provide them by using the --exclude option like this:
pagely-wp i18n make-pot . --exclude="ignored-dir,ignored-file.php"
The --exclude option also has the ability to use simple glob patterns like this:
pagely-wp i18n make-pot . --exclude="ignored-*.php"
The following files are always excluded, unless otherwise included:
- node_modules
- vendor
- .git
- .svn
- .CVS
- .hg
- *.min.js
Ignoring JavaScript
WP-CLI is able so search through both PHP and JavaScript files for translation strings when generating POT files. This is a great feature, but can sometimes get in the way if your JavaScript is already being processed through a build script.
To ignore JavaScript files when generating POT files, simply use the --skip-js option like this:
pagely-wp i18n make-pot wp-content/plugins/my-plugin-dir --skip-js