Skip to content

Instantly share code, notes, and snippets.

@rmpel
Last active July 10, 2025 06:26
Show Gist options
  • Save rmpel/969508c652de6a9bf33e4ac0f89996a4 to your computer and use it in GitHub Desktop.
Save rmpel/969508c652de6a9bf33e4ac0f89996a4 to your computer and use it in GitHub Desktop.
Universal po mo php file script for WordPress plugins
#!/usr/bin/env bash
# https://gist.github.com/rmpel/969508c652de6a9bf33e4ac0f89996a4
cd "$(dirname "$0")" || exit 1
SCRIPTDIR="$(pwd)"
SCRIPTFILE="$(basename "$0")"
SCRIPTPATH="$(realpath "$SCRIPTDIR"/"$SCRIPTFILE")"
I18NDIR="$(basename "$SCRIPTDIR")"
cd ".."
i18n_help() {
echo "Usage: $SCRIPTFILE command"
echo "commands:"
echo "make-pot : re-generate POT file from source"
echo "update-po : update PO files from POT file"
echo "make-mo : generate MO and l10n.php files from PO files"
echo "make : run make-pot, update-po, and make-mo in sequence"
}
[ -z "$1" ] && i18n_help && exit 1
file=./style.css
if [ -f "$file" ]; then
# Probably a theme.
if grep -q "Text Domain:" "$file"; then
TEXTDOMAIN=$(grep -o "Text Domain:\s*\([^'\"]*\)" "$file" | sed "s/Text Domain:\s*\([^'\"]*\)/\1/")
TEXTDOMAIN=$(echo "$TEXTDOMAIN" | xargs)
fi
fi
if [ -z "$TEXTDOMAIN" ]; then
# Not a theme, so, try to find textdomain from ./*php
for file in $(find . -name '*.php'); do
if grep -q "Text Domain:" "$file"; then
TEXTDOMAIN=$(grep -o "Text Domain:\s*\([^'\"]*\)" "$file" | sed "s/Text Domain:\s*\([^'\"]*\)/\1/")
TEXTDOMAIN=$(echo "$TEXTDOMAIN" | xargs)
break
fi
done
fi
if [ -z "$TEXTDOMAIN" ]; then
echo "Text Domain not found in any PHP file, or style.css"
echo "Make sure the main PHP file (for plugin) or style.css (for theme) has a comment block with the Text Domain in it based on WordPress Documentation standards."
exit 1
fi
echo "Text Domain: $TEXTDOMAIN" >&2
echo "Translation files are in: $I18NDIR" >&2
# only define this function if the wp-command is not available
if ! command -v wp &> /dev/null; then
wp () {
if [ ! -f wp-cli.phar ]; then
curl --silent --insecure -L -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar 2>/dev/null
chmod 755 wp-cli.phar
fi
./wp-cli.phar --allow-root "$@"
}
fi
i18n_make_pot() {
wp i18n make-pot --no-location --domain="$TEXTDOMAIN" . "$I18NDIR"/"$TEXTDOMAIN".pot --exclude=languages --exclude=node_modules --exclude=vendor --exclude=tests --exclude=build --exclude=dist
}
i18n_update_po() {
wp i18n update-po "$I18NDIR"/"$TEXTDOMAIN".pot "$I18NDIR"/
}
i18n_make_files() {
wp i18n make-mo "$I18NDIR"/
wp i18n make-php "$I18NDIR"/
}
case "$1" in
"make-pot")
i18n_make_pot
;;
"update-po")
i18n_update_po
;;
"make-mo")
i18n_make_files
;;
"make")
i18n_make_pot
i18n_update_po
i18n_make_files
;;
*)
echo "Unknown command"
i18n_help
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment