From 591b3a149144da8a9f8c4ab0268c89b7abda8da3 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 16 Feb 2024 14:49:33 +0100 Subject: [PATCH] Add more headers to php translation files --- features/makephp.feature | 86 ++++++++++++++++++++++++++------------- src/PhpArrayGenerator.php | 50 +++++++++++------------ 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/features/makephp.feature b/features/makephp.feature index 1afc9e6..0e8c7ba 100644 --- a/features/makephp.feature +++ b/features/makephp.feature @@ -83,26 +83,65 @@ Feature: Generate PHP files from PO files Given an empty foo-plugin directory And a foo-plugin/foo-plugin-de_DE.po file: """ - # Copyright (C) 2018 Foo Plugin - # This file is distributed under the same license as the Foo Plugin package. + # Translation of 5.2.x in German + # This file is distributed under the same license as the 5.2.x package. msgid "" msgstr "" - "Project-Id-Version: Foo Plugin\n" - "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n" - "Last-Translator: FULL NAME \n" - "Language-Team: LANGUAGE \n" - "Language: de_DE\n" + "PO-Revision-Date: 2019-03-28 19:42+0300\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" - "POT-Creation-Date: 2018-05-02T22:06:24+00:00\n" - "PO-Revision-Date: 2018-05-02T22:06:24+00:00\n" - "X-Domain: foo-plugin\n" - "Plural-Forms: nplurals=2; plural=(n != 1);\n" - - #: foo-plugin.js:15 - msgid "Foo Plugin" - msgstr "Foo Plugin" + "Plural-Forms: nplurals=2; plural=n != 1;\n" + "X-Generator: Poedit 2.2.1\n" + "Project-Id-Version: Development (5.2.x)\n" + "Language: de_DE\n" + "POT-Creation-Date: \n" + "Last-Translator: \n" + "Language-Team: \n" + + #. translators: Translate this to the correct language tag for your locale, see + #. https://www.w3.org/International/articles/language-tags/ for reference. Do + #. not translate into your own language. + #: wp-includes/general-template.php:716 + msgid "html_lang_attribute" + msgstr "de-DE" + + #. translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. + #: wp-includes/class-wp-locale.php:223 + msgctxt "text direction" + msgid "ltr" + msgstr "ltr" + + #. translators: $dec_point argument for https://secure.php.net/number_format, + #. default is . + #: wp-includes/class-wp-locale.php:215 + msgid "number_format_decimal_point" + msgstr "," + + #. translators: $thousands_sep argument for + #. https://secure.php.net/number_format, default is , + #: wp-includes/class-wp-locale.php:202 + msgid "number_format_thousands_sep" + msgstr "." + + #. translators: %s: Plugin name and version + #: wp-includes/script-loader.php:620 + msgid "Update %s now" + msgstr "Jetzt %s aktualisieren" + + #. translators: Privacy data request subject. 1: Site name, 2: Name of the action + #: wp-includes/user.php:3445 + msgid "[%1$s] Confirm Action: %2$s" + msgstr "[%1$s] Aktion bestätigen: %2$s" + + #. translators: %s: Site name. + #: wp-includes/user.php:3175 + msgid "[%s] Erasure Request Fulfilled" + msgstr "[%s] Löschauftrag ausgeführt" + + #: wp-admin/includes/file.php:2415 + msgid "[%s] Personal Data Export" + msgstr "[%s] Export personenbezogener Daten" """ When I run `wp i18n make-php foo-plugin` @@ -114,19 +153,8 @@ Feature: Generate PHP files from PO files And STDERR should be empty And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: """ - 'language'=>'de_DE' - """ - And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: - """ - 'domain'=>'foo-plugin' - """ - And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: - """ - 'plural-forms'=>'nplurals=2; plural=(n != 1);' - """ - And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain: - """ - 'messages'=>['Foo Plugin'=>'Foo Plugin'] + NULL,'plural-forms'=>'nplurals=2; plural=n != 1;','language'=>'de_DE','project-id-version'=>'Development (5.2.x)','pot-creation-date'=>'','po-revision-date'=>'2019-03-28 19:42+0300','x-generator'=>'Poedit 2.2.1','messages'=>['html_lang_attribute'=>'de-DE','text directionltr'=>'ltr','number_format_decimal_point'=>',','number_format_thousands_sep'=>'.','Update %s now'=>'Jetzt %s aktualisieren','[%1$s] Confirm Action: %2$s'=>'[%1$s] Aktion bestätigen: %2$s','[%s] Erasure Request Fulfilled'=>'[%s] Löschauftrag ausgeführt','[%s] Personal Data Export'=>'[%s] Export personenbezogener Daten']]; """ Scenario: Does include translations @@ -174,5 +202,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: """ - 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']; + return ['domain'=>'foo-plugin','plural-forms'=>'nplurals=2; plural=(n != 1);','language'=>'de_DE','project-id-version'=>'Foo Plugin','pot-creation-date'=>'2018-05-02T22:06:24+00:00','po-revision-date'=>'2018-05-02T22:06:24+00:00','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']]; """ diff --git a/src/PhpArrayGenerator.php b/src/PhpArrayGenerator.php index 544d522..5f72df7 100644 --- a/src/PhpArrayGenerator.php +++ b/src/PhpArrayGenerator.php @@ -22,21 +22,6 @@ class PhpArrayGenerator extends PhpArray { public static function toString( Translations $translations, array $options = [] ) { $array = static::generate( $translations, $options ); - $language = $translations->getLanguage(); - if ( null !== $language ) { - $array['language'] = $language; - } - - $headers = [ - 'X-Generator' => 'x-generator', - ]; - - foreach ( $translations->getHeaders() as $name => $value ) { - if ( isset( $headers[ $name ] ) ) { - $array[ $headers[ $name ] ] = $value; - } - } - return ' [ static::generateHeaders( $translations ) ], - ]; + $result = [ + 'domain' => $translations->getDomain(), + 'plural-forms' => $translations->getHeader( 'Plural-Forms' ), + ]; + + $language = $translations->getLanguage(); + if ( null !== $language ) { + $result['language'] = $language; + } + + $headers_allowlist = [ + 'POT-Creation-Date' => 'pot-creation-date', + 'PO-Revision-Date' => 'po-revision-date', + 'Project-Id-Version' => 'project-id-version', + 'X-Generator' => 'x-generator', + ]; + + foreach ( $translations->getHeaders() as $name => $value ) { + if ( isset( $headers_allowlist[ $name ] ) ) { + $result[ $headers_allowlist[ $name ] ] = $value; + } } /** @@ -94,11 +96,9 @@ protected static function toArray( Translations $translations, $include_headers, } } - return [ - 'domain' => $translations->getDomain(), - 'plural-forms' => $translations->getHeader( 'Plural-Forms' ), - 'messages' => $messages, - ]; + $result['messages'] = $messages; + + return $result; } /**