Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental js & twig support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions inc/class-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Command extends WP_CLI_Command {
* ---
*
* [--domain=<string>]
* : 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'
* ---
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down
29 changes: 29 additions & 0 deletions inc/class-wpjscode-extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Gettext\Extractors;

use Gettext\Translations;
use Gettext\Utils\WPJsFunctionsScanner;

/**
* Class to get gettext strings from php files returning arrays.
*/
class WPJsCode extends WPCode {

/**
* {@inheritdoc}
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {

$options += static::$options;

$functions = new WPJsFunctionsScanner( $string );

if ( $options['extractComments'] !== false ) {
$functions->enableCommentsExtraction( $options['extractComments'] );
}

$functions->saveGettextFunctions( $translations, $options );
}

}
97 changes: 97 additions & 0 deletions inc/class-wpjsfunctionsscanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Gettext\Utils;

use Exception;
use Gettext\Translations;

class WPJsFunctionsScanner extends JsFunctionsScanner {

/**
* Search for specific functions and create translations.
*
* @param Translations $translations The translations instance where save the values
* @param array $options The extractor options
* @throws Exception
*/
public function saveGettextFunctions( Translations $translations, array $options ) {
$functions = $options['functions'];
$file = $options['file'];

foreach ( $this->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 );
}
}
}
}
}

}