Skip to content

Instantly share code, notes, and snippets.

@rmpel
Created November 22, 2024 08:37
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
cd "$(dirname $0)/.."
[ -z "$1" ] && echo "Usage: $0 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" && exit 1
# 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
if [ -z "$TEXTDOMAIN" ]; then
echo "Text Domain not found in any PHP file"
echo "Make sure the main PHP file has a comment block with the Text Domain in it based on WordPress standards."
exit 1
fi
echo "Text Domain: $TEXTDOMAIN" >&2
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 "$@"
}
fi
case "$1" in
"make-pot")
wp i18n make-pot --domain=$TEXTDOMAIN . languages/$TEXTDOMAIN.pot --exclude=languages
;;
"update-po")
wp i18n update-po languages/$TEXTDOMAIN.pot languages/
;;
"make-mo")
wp i18n make-mo languages/
wp i18n make-php languages/
;;
*)
echo "Unknown command"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment