From b5f8fbaca7c26be173b12e65d049f0e93faf7dfa Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 1 Feb 2024 10:53:58 +0100 Subject: [PATCH 1/3] Fix file format in make-php Follow-up to #363. Requirement for WP 6.5 support. --- features/makephp.feature | 13 +++++++- src/PhpArrayGenerator.php | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/features/makephp.feature b/features/makephp.feature index 0fab416..6dbbefd 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -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` @@ -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']; """ diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 28c1c57..bdc7129 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -3,6 +3,7 @@ namespace WP_CLI\I18n; use Gettext\Generators\PhpArray; +use Gettext\Translation; use Gettext\Translations; /** @@ -39,6 +40,67 @@ public static function toString( Translations $translations, array $options = [] return ' [ 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. * From 7d387306664347a325e4021b7a187223b876083e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 1 Feb 2024 11:01:00 +0100 Subject: [PATCH 2/3] Fix alignment --- src/PhpArrayGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index bdc7129..544d522 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -64,7 +64,7 @@ public static function generate( Translations $translations, array $options = [] * @return array */ protected static function toArray( Translations $translations, $include_headers, $force_array = false ) { - $messages = []; + $messages = []; if ( $include_headers ) { $messages[''] = [ From b1801bc9e0565bd9f07159a4b082996c467b34ff Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 1 Feb 2024 11:17:01 +0100 Subject: [PATCH 3/3] Update other test --- features/makephp.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/makephp.feature b/features/makephp.feature index 6dbbefd..1afc9e6 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -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