Skip to content

Commit

Permalink
Merge pull request #388 from wp-cli/fix/make-php-header-format
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Feb 26, 2024
2 parents c251a9c + 591b3a1 commit 46e9483
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 54 deletions.
86 changes: 57 additions & 29 deletions features/makephp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\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`
Expand All @@ -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']
<?php
return ['domain'=>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
Expand Down Expand Up @@ -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']];
"""
50 changes: 25 additions & 25 deletions src/PhpArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';';
}

Expand All @@ -55,7 +40,7 @@ public static function generate( Translations $translations, array $options = []
}

/**
* Returns a flat array.
* Returns an array containing headers and translations.
*
* @param Translations $translations
* @param bool $include_headers
Expand All @@ -66,10 +51,27 @@ public static function generate( Translations $translations, array $options = []
protected static function toArray( Translations $translations, $include_headers, $force_array = false ) {
$messages = [];

if ( $include_headers ) {
$messages[''] = [
'' => [ 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;
}
}

/**
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 46e9483

Please sign in to comment.