Skip to content

Commit

Permalink
WIP: php-gettext 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Droz authored and schlessera committed Jan 6, 2022
1 parent e6e2000 commit 0f124f2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 105 deletions.
34 changes: 34 additions & 0 deletions src/IterableCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,38 @@ static function ( $file, $key, $iterator ) use ( $include, $exclude, $extensions
private static function trim_leading_slash( $path ) {
return ltrim( $path, '/' );
}

/**
* Formerly part php-gettext 4.x.
*/
protected static function getFiles($file)
{
if (empty($file)) {
throw new InvalidArgumentException('There is not any file defined');
}

if (is_string($file)) {
if (!is_file($file)) {
throw new InvalidArgumentException("'$file' is not a valid file");
}

if (!is_readable($file)) {
throw new InvalidArgumentException("'$file' is not a readable file");
}

return [$file];
}

if (is_array($file)) {
$files = [];

foreach ($file as $f) {
$files = array_merge($files, self::getFiles($f));
}

return $files;
}

throw new InvalidArgumentException('The first argument must be string or array');
}
}
31 changes: 17 additions & 14 deletions src/MakePotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gettext\Merge;
use Gettext\Translation;
use Gettext\Translations;
use Gettext\Headers;
use Gettext\Utils\ParsedComment;
use WP_CLI;
use WP_CLI_Command;
Expand Down Expand Up @@ -402,6 +403,8 @@ function ( $file ) {
}
}

$this->exceptions = Translations::create();

if ( isset( $assoc_args['subtract'] ) ) {
$this->subtract_and_merge = Utils\get_flag_value( $assoc_args, 'subtract-and-merge', false );

Expand All @@ -415,7 +418,7 @@ function ( $file ) {

WP_CLI::debug( sprintf( 'Ignoring any string already existing in: %s', $file ), 'make-pot' );

$this->exceptions[ $file ] = new Translations();
$this->exceptions[ $file ] = Translations::create();
Po::fromFile( $file, $this->exceptions[ $file ] );
}
}
Expand Down Expand Up @@ -554,11 +557,11 @@ protected function get_file_headers( $type ) {
* @return Translations A Translation set.
*/
protected function extract_strings() {
$translations = new Translations();
$translations = Translations::create();

// Add existing strings first but don't keep headers.
if ( ! empty( $this->merge ) ) {
$existing_translations = new Translations();
$existing_translations = Translations::create();
Po::fromFile( $this->merge, $existing_translations );
$translations->mergeWith( $existing_translations, Merge::ADD | Merge::REMOVE );
}
Expand All @@ -568,10 +571,10 @@ protected function extract_strings() {
$this->set_default_headers( $translations );

// POT files have no Language header.
$translations->deleteHeader( Translations::HEADER_LANGUAGE );
$translations->getHeaders()->delete( Headers::HEADER_LANGUAGE );

// Only relevant for PO files, not POT files.
$translations->setHeader( 'PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE' );
$translations->getHeaders()->set('PO-Revision-Date', 'YEAR-MO-DA HO:MI+ZONE');

if ( $this->domain ) {
$translations->setDomain( $this->domain );
Expand All @@ -585,15 +588,15 @@ protected function extract_strings() {
continue;
}

$translation = new Translation( '', $data );
$translation = Translation::create( '', $data );

if ( isset( $this->main_file_data['Theme Name'] ) ) {
$translation->addExtractedComment( sprintf( '%s of the theme', $header ) );
$translation->getComments()->add( sprintf( '%s of the theme', $header ) );
} else {
$translation->addExtractedComment( sprintf( '%s of the plugin', $header ) );
$translation->getComments()->add( sprintf( '%s of the plugin', $header ) );
}

$translations[] = $translation;
$translations->add($translation);
}

try {
Expand Down Expand Up @@ -908,16 +911,16 @@ protected function set_default_headers( $translations ) {
}

if ( null !== $name ) {
$translations->setHeader( 'Project-Id-Version', $name . ( $version ? ' ' . $version : '' ) );
$translations->getHeaders()->set('Project-Id-Version', $name . ( $version ? ' ' . $version : '' ));
}

if ( null !== $bugs_address ) {
$translations->setHeader( 'Report-Msgid-Bugs-To', $bugs_address );
$translations->getHeaders()->set('Report-Msgid-Bugs-To', $bugs_address);
}

$translations->setHeader( 'Last-Translator', 'FULL NAME <EMAIL@ADDRESS>' );
$translations->setHeader( 'Language-Team', 'LANGUAGE <[email protected]>' );
$translations->setHeader( 'X-Generator', 'WP-CLI ' . WP_CLI_VERSION );
$translations->getHeaders()->set('Last-Translator', 'FULL NAME <EMAIL@ADDRESS>');
$translations->getHeaders()->set('Language-Team', 'LANGUAGE <[email protected]>');
$translations->getHeaders()->set('X-Generator', 'WP-CLI ' . WP_CLI_VERSION);

foreach ( $this->headers as $key => $value ) {
$translations->setHeader( $key, $value );
Expand Down
11 changes: 6 additions & 5 deletions src/PhpCodeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace WP_CLI\I18n;

use Exception;
use Gettext\Extractors\PhpCode;
use Gettext\Scanner\PhpScanner;
use Gettext\Translations;
use WP_CLI;

final class PhpCodeExtractor extends PhpCode {
class PhpCodeExtractor {
use IterableCodeExtractor;

public static $options = [
Expand Down Expand Up @@ -43,16 +43,17 @@ final class PhpCodeExtractor extends PhpCode {
],
];

protected static $functionsScannerClass = 'WP_CLI\I18n\PhpFunctionsScanner';

/**
* {@inheritdoc}
*/
public static function fromString( $string, Translations $translations, array $options = [] ) {
WP_CLI::debug( "Parsing file {$options['file']}", 'make-pot' );

try {
static::fromStringMultiple( $string, [ $translations ], $options );
$scanner = new PhpScanner( $translations );
$scanner->setFunctions( self::$options['functions'] );
$scanner->extractCommentsStartingWith($options['extractComments']);
$scanner->scanString( $string, $options[ 'file' ] );
} catch ( Exception $exception ) {
WP_CLI::debug(
sprintf(
Expand Down
86 changes: 0 additions & 86 deletions src/PhpFunctionsScanner.php

This file was deleted.

0 comments on commit 0f124f2

Please sign in to comment.