From f1758c4dbb739e2441ce711d697c15703fdebba9 Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Mon, 21 May 2018 11:32:34 +0100 Subject: [PATCH] experimental js & twig support --- composer.json | 2 +- composer.lock | 4 +- inc/class-command.php | 28 ++++++--- inc/class-wpjscode-extractor.php | 29 +++++++++ inc/class-wpjsfunctionsscanner.php | 97 ++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 13 deletions(-) create mode 100644 inc/class-wpjscode-extractor.php create mode 100644 inc/class-wpjsfunctionsscanner.php diff --git a/composer.json b/composer.json index c3f5004..744e763 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "homepage": "https://github.com/roborourke/wp-l10n-gen", "require": { "gettext/gettext": "dev-master", - "php": ">=5.4" + "php": ">=7.0" }, "license": "GPL-3.0", "authors": [ diff --git a/composer.lock b/composer.lock index e48f0c3..746aa21 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c103dc252ffd5e0f2b6aec312cc0924e", + "content-hash": "674b401c419fd90b51b1b2e205e35c95", "packages": [ { "name": "gettext/gettext", @@ -139,7 +139,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=5.4" + "php": ">=7.0" }, "platform-dev": [] } diff --git a/inc/class-command.php b/inc/class-command.php index 7570838..3eac614 100644 --- a/inc/class-command.php +++ b/inc/class-command.php @@ -49,7 +49,7 @@ class Command extends WP_CLI_Command { * --- * * [--domain=] - * : The text domain to extract strings for. Prepended to translation files. + * : The text domain to extract strings for. Prepended to translation file names. * --- * default: 'default' * --- @@ -98,18 +98,26 @@ public function generate( $args, $assoc_args = [] ) { WP_CLI::log( 'Preparing to parse files...' ); foreach ( $paths as $path ) { - $directory = new \RecursiveDirectoryIterator( $path ); - $iterator = new \RecursiveIteratorIterator( $directory ); - $php_files = new \RegexIterator( $iterator, '/^.+\.php\d?$/i', \RecursiveRegexIterator::GET_MATCH ); + $directory = new \RecursiveDirectoryIterator( $path ); + $iterator = new \RecursiveIteratorIterator( $directory ); + $code_files = new \RegexIterator( $iterator, '/^.+(\.php\d|\.jsx?|\.twig)?$/i', \RecursiveRegexIterator::GET_MATCH ); // Show progress. - $progress = make_progress_bar( sprintf( 'Extracting strings from %s', $path ), iterator_count( $php_files ) ); + $progress = make_progress_bar( sprintf( 'Extracting strings from %s', $path ), iterator_count( $code_files ) ); - foreach ( $php_files as $file_path => $file_info ) { + foreach ( $code_files as $file_path => $file_info ) { if ( isset( $assoc_args['verbose'] ) ) { WP_CLI::log( sprintf( 'Extracting strings from %s', $file_path ) ); } - $translations->addFromWPCodeFile( $file_path ); + switch ( pathinfo( $file_path, PATHINFO_EXTENSION ) ) { + case 'js': + case 'jsx': + $translations->addFromWPJsCodeFile( $file_path ); + break; + default: + $translations->addFromWPCodeFile( $file_path ); + break; + } $progress->tick(); } @@ -203,7 +211,7 @@ public function convert( $args, $assoc_args = [] ) { } // Normalise file extension for regex. - $file_extension = str_replace( [ 'dict', 'yaml' ], [ '', 'yml' ], $input_type ); + $file_extension = str_replace( [ 'dict', 'yaml', 'jed' ], [ '', 'yml', 'json' ], $input_type ); // Files iterator. try { @@ -269,7 +277,7 @@ public function po2mo( $args, $assoc_args ) { * @param string $type * @return false|Translations */ - protected function from( string $file, string $type = '' ) { + protected function from( string $file, string $type ) { $translations = false; // Determine type from extension. @@ -357,7 +365,7 @@ protected function to( Translations $translations, string $type, string $file, $ $type = 'json'; break; case 'jed': - $translations->toJedFile( "{$file}.jed", $file_args ); + $translations->toJedFile( "{$file}.json", $file_args ); break; case 'xliff': $translations->toXliffFile( "{$file}.xliff", $file_args ); diff --git a/inc/class-wpjscode-extractor.php b/inc/class-wpjscode-extractor.php new file mode 100644 index 0000000..bffec61 --- /dev/null +++ b/inc/class-wpjscode-extractor.php @@ -0,0 +1,29 @@ +enableCommentsExtraction( $options['extractComments'] ); + } + + $functions->saveGettextFunctions( $translations, $options ); + } + +} diff --git a/inc/class-wpjsfunctionsscanner.php b/inc/class-wpjsfunctionsscanner.php new file mode 100644 index 0000000..33caa7f --- /dev/null +++ b/inc/class-wpjsfunctionsscanner.php @@ -0,0 +1,97 @@ +getFunctions( $options['constants'] ) as $function ) { + list( $name, $line, $args ) = $function; + + if ( ! isset( $functions[ $name ] ) ) { + continue; + } + + $context = $original = $plural = null; + $domain = 'default'; + + switch ( $functions[ $name ] ) { + case 'noop': + if ( ! isset( $args[0] ) ) { + continue 2; + } + + $original = $args[0]; + break; + + case 'nnoop': + if ( ! isset( $args[1] ) ) { + continue 2; + } + + list( $original, $plural ) = $args; + break; + + case 'dgettext': + if ( ! isset( $args[1] ) ) { + continue 2; + } + + list( $original, $domain ) = $args; + break; + + case 'dpgettext': + if ( ! isset( $args[2] ) ) { + continue 2; + } + + list( $original, $context, $domain ) = $args; + break; + + case 'dnpgettext': + if ( ! isset( $args[3] ) ) { + continue 2; + } + + list( $original, $plural, $context, $domain ) = $args; + break; + + case 'dngettext': + if ( ! isset( $args[2] ) ) { + continue 2; + } + + list( $original, $plural, $domain ) = $args; + break; + + default: + throw new Exception( sprintf( 'Not valid function %s', $functions[ $name ] ) ); + } + + if ( (string) $original !== '' && ( $domain === null || $domain === $translations->getDomain() ) ) { + $translation = $translations->insert( $context, $original, $plural ); + $translation->addReference( $file, $line ); + + if ( isset( $function[3] ) ) { + foreach ( $function[3] as $extractedComment ) { + $translation->addExtractedComment( $extractedComment ); + } + } + } + } + } + +}