Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Script to surface WordPress i18n functions from Gutenberg blocks (Javascript)
<?php
/*
Script for building a php file containing the i18n strings, properly formatted for inclusion in POT file
*/
if ( $argc < 4 ) {
echo "\nError: You must pass a domain name, an output file and at least one filename.\n";
echo "Usage: php -f [text-domain] [OUTPUT FILENAME] [INPUT filename(s)]\n\n";
exit;
} else {
// Domain string name for plugin
$domain = trim( $argv[1] );
}
$pot_strings = '<?php
/* THIS IS A GENERATED FILE. DO NOT EDIT DIRECTLY. */
/* THIS FILE EXPOSES STRINGS FROM JAVASCRIPT FILES FOR TRANSLATION */
$generated_i18n_strings = array(' . "\n";
$filenames = array_slice( $argv, 3 );
foreach ( $filenames as $filename ) {
$gutenberg_block = file_get_contents( $filename );
// matcher regex for possible string translation functions
preg_match_all( '/_[_]?[n]?[x]?[\(][\s]?[\'"].*?[\'"][\s]?,[\s]?[\'"]' . $domain . '[\'"][\s]?[\)]/', $gutenberg_block, $matches );
foreach ( $matches[0] as $string ) {
$pot_strings .= "\t$string,\n";
}
}
$pot_strings .= ");\n";
$file = fopen( $argv[2], 'w+' );
fwrite( $file, $pot_strings );
fclose( $file );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment