Skip to content

Commit

Permalink
Merge pull request #379 from wp-cli/fix/php-format
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Feb 1, 2024
2 parents 1bc07c3 + b1801bc commit ca7a44a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
15 changes: 13 additions & 2 deletions features/makephp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Feature: Generate PHP files from PO files
"""
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
'messages'=>[''=>['Foo Plugin'=>['Foo Plugin']]]
'messages'=>['Foo Plugin'=>'Foo Plugin']
"""

Scenario: Does include translations
Expand All @@ -150,9 +150,20 @@ Feature: Generate PHP files from PO files
"X-Domain: foo-plugin\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: foo-plugin.js:15
msgctxt "Plugin Name"
msgid "Foo Plugin (EN)"
msgstr "Foo Plugin (DE)"
#: foo-plugin.js:15
msgid "Foo Plugin"
msgstr "Bar Plugin"
#: foo-plugin.php:60
msgid "You have %d new message"
msgid_plural "You have %d new messages"
msgstr[0] "Sie haben %d neue Nachricht"
msgstr[1] "Sie haben %d neue Nachrichten"
"""

When I run `wp i18n make-php foo-plugin`
Expand All @@ -163,5 +174,5 @@ Feature: Generate PHP files from PO files
And the return code should be 0
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
'messages'=>[''=>['Foo Plugin'=>['Bar Plugin']]]
return ['domain'=>'foo-plugin','plural-forms'=>'nplurals=2; plural=(n != 1);','messages'=>['Plugin NameFoo Plugin (EN)'=>'Foo Plugin (DE)','Foo Plugin'=>'Bar Plugin','You have %d new message'=>'Sie haben %d neue Nachricht' . "\0" . 'Sie haben %d neue Nachrichten'],'language'=>'de_DE'];
"""
62 changes: 62 additions & 0 deletions src/PhpArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WP_CLI\I18n;

use Gettext\Generators\PhpArray;
use Gettext\Translation;
use Gettext\Translations;

/**
Expand Down Expand Up @@ -39,6 +40,67 @@ public static function toString( Translations $translations, array $options = []
return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';';
}

/**
* Generates an array with the translations.
*
* @param Translations $translations
* @param array $options
*
* @return array
*/
public static function generate( Translations $translations, array $options = [] ) {
$options += static::$options;

return static::toArray( $translations, $options['includeHeaders'] );
}

/**
* Returns a flat array.
*
* @param Translations $translations
* @param bool $include_headers
* @param bool $force_array Unused.
*
* @return array
*/
protected static function toArray( Translations $translations, $include_headers, $force_array = false ) {
$messages = [];

if ( $include_headers ) {
$messages[''] = [
'' => [ static::generateHeaders( $translations ) ],
];
}

/**
* @var Translation $translation
*/
foreach ( $translations as $translation ) {
if ( $translation->isDisabled() ) {
continue;
}

$context = $translation->getContext();
$original = $translation->getOriginal();

$key = $context ? $context . "\4" . $original : $original;

if ( $translation->hasPluralTranslations() ) {
$msg_translations = $translation->getPluralTranslations();
array_unshift( $msg_translations, $translation->getTranslation() );
$messages[ $key ] = implode( "\0", $msg_translations );
} else {
$messages[ $key ] = $translation->getTranslation();
}
}

return [
'domain' => $translations->getDomain(),
'plural-forms' => $translations->getHeader( 'Plural-Forms' ),
'messages' => $messages,
];
}

/**
* Determines if the given array is a list.
*
Expand Down

0 comments on commit ca7a44a

Please sign in to comment.