Skip to content

Commit 46e9483

Browse files
authored
Merge pull request #388 from wp-cli/fix/make-php-header-format
2 parents c251a9c + 591b3a1 commit 46e9483

2 files changed

Lines changed: 82 additions & 54 deletions

File tree

features/makephp.feature

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,65 @@ Feature: Generate PHP files from PO files
8383
Given an empty foo-plugin directory
8484
And a foo-plugin/foo-plugin-de_DE.po file:
8585
"""
86-
# Copyright (C) 2018 Foo Plugin
87-
# This file is distributed under the same license as the Foo Plugin package.
86+
# Translation of 5.2.x in German
87+
# This file is distributed under the same license as the 5.2.x package.
8888
msgid ""
8989
msgstr ""
90-
"Project-Id-Version: Foo Plugin\n"
91-
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/foo-plugin\n"
92-
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
93-
"Language-Team: LANGUAGE <LL@li.org>\n"
94-
"Language: de_DE\n"
90+
"PO-Revision-Date: 2019-03-28 19:42+0300\n"
9591
"MIME-Version: 1.0\n"
9692
"Content-Type: text/plain; charset=UTF-8\n"
9793
"Content-Transfer-Encoding: 8bit\n"
98-
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
99-
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
100-
"X-Domain: foo-plugin\n"
101-
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
102-
103-
#: foo-plugin.js:15
104-
msgid "Foo Plugin"
105-
msgstr "Foo Plugin"
94+
"Plural-Forms: nplurals=2; plural=n != 1;\n"
95+
"X-Generator: Poedit 2.2.1\n"
96+
"Project-Id-Version: Development (5.2.x)\n"
97+
"Language: de_DE\n"
98+
"POT-Creation-Date: \n"
99+
"Last-Translator: \n"
100+
"Language-Team: \n"
101+
102+
#. translators: Translate this to the correct language tag for your locale, see
103+
#. https://www.w3.org/International/articles/language-tags/ for reference. Do
104+
#. not translate into your own language.
105+
#: wp-includes/general-template.php:716
106+
msgid "html_lang_attribute"
107+
msgstr "de-DE"
108+
109+
#. translators: 'rtl' or 'ltr'. This sets the text direction for WordPress.
110+
#: wp-includes/class-wp-locale.php:223
111+
msgctxt "text direction"
112+
msgid "ltr"
113+
msgstr "ltr"
114+
115+
#. translators: $dec_point argument for https://secure.php.net/number_format,
116+
#. default is .
117+
#: wp-includes/class-wp-locale.php:215
118+
msgid "number_format_decimal_point"
119+
msgstr ","
120+
121+
#. translators: $thousands_sep argument for
122+
#. https://secure.php.net/number_format, default is ,
123+
#: wp-includes/class-wp-locale.php:202
124+
msgid "number_format_thousands_sep"
125+
msgstr "."
126+
127+
#. translators: %s: Plugin name and version
128+
#: wp-includes/script-loader.php:620
129+
msgid "Update %s now"
130+
msgstr "Jetzt %s aktualisieren"
131+
132+
#. translators: Privacy data request subject. 1: Site name, 2: Name of the action
133+
#: wp-includes/user.php:3445
134+
msgid "[%1$s] Confirm Action: %2$s"
135+
msgstr "[%1$s] Aktion bestätigen: %2$s"
136+
137+
#. translators: %s: Site name.
138+
#: wp-includes/user.php:3175
139+
msgid "[%s] Erasure Request Fulfilled"
140+
msgstr "[%s] Löschauftrag ausgeführt"
141+
142+
#: wp-admin/includes/file.php:2415
143+
msgid "[%s] Personal Data Export"
144+
msgstr "[%s] Export personenbezogener Daten"
106145
"""
107146

108147
When I run `wp i18n make-php foo-plugin`
@@ -114,19 +153,8 @@ Feature: Generate PHP files from PO files
114153
And STDERR should be empty
115154
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
116155
"""
117-
'language'=>'de_DE'
118-
"""
119-
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
120-
"""
121-
'domain'=>'foo-plugin'
122-
"""
123-
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
124-
"""
125-
'plural-forms'=>'nplurals=2; plural=(n != 1);'
126-
"""
127-
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
128-
"""
129-
'messages'=>['Foo Plugin'=>'Foo Plugin']
156+
<?php
157+
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']];
130158
"""
131159

132160
Scenario: Does include translations
@@ -174,5 +202,5 @@ Feature: Generate PHP files from PO files
174202
And the return code should be 0
175203
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
176204
"""
177-
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'];
205+
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']];
178206
"""

src/PhpArrayGenerator.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@ class PhpArrayGenerator extends PhpArray {
2222
public static function toString( Translations $translations, array $options = [] ) {
2323
$array = static::generate( $translations, $options );
2424

25-
$language = $translations->getLanguage();
26-
if ( null !== $language ) {
27-
$array['language'] = $language;
28-
}
29-
30-
$headers = [
31-
'X-Generator' => 'x-generator',
32-
];
33-
34-
foreach ( $translations->getHeaders() as $name => $value ) {
35-
if ( isset( $headers[ $name ] ) ) {
36-
$array[ $headers[ $name ] ] = $value;
37-
}
38-
}
39-
4025
return '<?php' . PHP_EOL . 'return ' . static::var_export( $array ) . ';';
4126
}
4227

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

5742
/**
58-
* Returns a flat array.
43+
* Returns an array containing headers and translations.
5944
*
6045
* @param Translations $translations
6146
* @param bool $include_headers
@@ -66,10 +51,27 @@ public static function generate( Translations $translations, array $options = []
6651
protected static function toArray( Translations $translations, $include_headers, $force_array = false ) {
6752
$messages = [];
6853

69-
if ( $include_headers ) {
70-
$messages[''] = [
71-
'' => [ static::generateHeaders( $translations ) ],
72-
];
54+
$result = [
55+
'domain' => $translations->getDomain(),
56+
'plural-forms' => $translations->getHeader( 'Plural-Forms' ),
57+
];
58+
59+
$language = $translations->getLanguage();
60+
if ( null !== $language ) {
61+
$result['language'] = $language;
62+
}
63+
64+
$headers_allowlist = [
65+
'POT-Creation-Date' => 'pot-creation-date',
66+
'PO-Revision-Date' => 'po-revision-date',
67+
'Project-Id-Version' => 'project-id-version',
68+
'X-Generator' => 'x-generator',
69+
];
70+
71+
foreach ( $translations->getHeaders() as $name => $value ) {
72+
if ( isset( $headers_allowlist[ $name ] ) ) {
73+
$result[ $headers_allowlist[ $name ] ] = $value;
74+
}
7375
}
7476

7577
/**
@@ -94,11 +96,9 @@ protected static function toArray( Translations $translations, $include_headers,
9496
}
9597
}
9698

97-
return [
98-
'domain' => $translations->getDomain(),
99-
'plural-forms' => $translations->getHeader( 'Plural-Forms' ),
100-
'messages' => $messages,
101-
];
99+
$result['messages'] = $messages;
100+
101+
return $result;
102102
}
103103

104104
/**

0 commit comments

Comments
 (0)