Skip to content

Commit ece9bd0

Browse files
claudeswissspidy
andcommitted
Use the header, flag and reference order of gettext/gettext v5
Drop the classes that kept headers, flags and references in the order they were added. Headers and flags are now sorted alphabetically and references are grouped by file, as the library does by default. The keys in generated PHP files keep their order. Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Claude-Session: https://claude.ai/code/session_01UWTd5qmztfFv37ybV4xsjV
1 parent b0857ff commit ece9bd0

12 files changed

Lines changed: 90 additions & 239 deletions

‎features/makepot.feature‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ Feature: Generate a POT file of a WordPress project
189189
And I run `wp i18n make-pot wp-content/plugins/hello-world wp-content/plugins/hello-world/languages/hello-world.pot`
190190
Then the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
191191
"""
192-
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
193192
"Language-Team: LANGUAGE <LL@li.org>\n"
193+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
194194
"""
195195

196196
Scenario: Sets the generator header
@@ -1765,17 +1765,23 @@ Feature: Generate a POT file of a WordPress project
17651765
# This file is distributed under the same license as the Hello World plugin.
17661766
msgid ""
17671767
msgstr ""
1768-
"Project-Id-Version: Hello World 0.1.0\n"
1769-
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n"
1770-
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1768+
"Content-Transfer-Encoding: 8bit\n"
1769+
"Content-Type: text/plain; charset=UTF-8\n"
17711770
"Language-Team: LANGUAGE <LL@li.org>\n"
1771+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
17721772
"MIME-Version: 1.0\n"
1773-
"Content-Type: text/plain; charset=UTF-8\n"
1774-
"Content-Transfer-Encoding: 8bit\n"
1773+
"""
1774+
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
1775+
"""
1776+
"Project-Id-Version: Hello World 0.1.0\n"
1777+
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/hello-world\n"
17751778
"""
17761779
And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain:
17771780
"""
17781781
"POT-Creation-Date: 2018-05-02T22:06:24+00:00\n"
1782+
"""
1783+
And the wp-content/plugins/hello-world/languages/hello-world.pot file should not contain:
1784+
"""
17791785
"PO-Revision-Date: 2018-05-02T22:06:24+00:00\n"
17801786
"""
17811787
And the wp-content/plugins/hello-world/languages/hello-world.pot file should contain:
@@ -4460,7 +4466,7 @@ Feature: Generate a POT file of a WordPress project
44604466
#: foo-plugin.php:6
44614467
"""
44624468

4463-
Scenario: Keeps flags and references in the order they were found
4469+
Scenario: Groups references by file and sorts flags
44644470
Given an empty foo-plugin directory
44654471
And a foo-plugin/foo-plugin.php file:
44664472
"""
@@ -4506,9 +4512,9 @@ Feature: Generate a POT file of a WordPress project
45064512
#: admin.php:3
45074513
#: foo-plugin.php:7
45084514
#: build/index.js:2
4509-
#: build/view.js:2
45104515
#: build/index.js:4
4511-
#, php-format,js-format
4516+
#: build/view.js:2
4517+
#, js-format,php-format
45124518
msgid "Hello %s"
45134519
msgstr ""
45144520
"""

‎features/updatepo.feature‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ Feature: Update existing PO files from a POT file
934934
msgstr[1] "0"
935935
"""
936936

937-
Scenario: Keeps the order of headers, flags and references
937+
Scenario: Sorts headers and flags and groups references by file
938938
Given an empty foo-plugin directory
939939
And a foo-plugin/foo-plugin.pot file:
940940
"""
@@ -975,16 +975,17 @@ Feature: Update existing PO files from a POT file
975975
And STDERR should be empty
976976
And the foo-plugin/foo-plugin-de_DE.po file should contain:
977977
"""
978-
"Language: de_DE\n"
979-
"X-Poedit-Basepath: ..\n"
980978
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
981-
"X-Generator: Poedit 3.0\n"
979+
"Project-Id-Version: Foo Plugin\n"
980+
"Report-Msgid-Bugs-To: \n"
982981
"X-Domain: foo-plugin\n"
982+
"X-Generator: Poedit 3.0\n"
983+
"X-Poedit-Basepath: ..\n"
983984
984985
#: foo-plugin.php:20
985-
#: build/index.js:1
986986
#: foo-plugin.php:10
987-
#, php-format,fuzzy,js-format
987+
#: build/index.js:1
988+
#, fuzzy,js-format,php-format
988989
msgid "Hello %s"
989990
msgstr "Hallo %s"
990991
"""

‎src/OrderedFlags.php‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎src/OrderedHeaders.php‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎src/OrderedReferences.php‎

Lines changed: 0 additions & 79 deletions
This file was deleted.

‎src/PhpArrayGenerator.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,17 @@ protected static function toArray( Translations $translations, $include_headers,
7272
}
7373

7474
$headers_allowlist = [
75+
'Project-Id-Version' => 'project-id-version',
7576
'POT-Creation-Date' => 'pot-creation-date',
7677
'PO-Revision-Date' => 'po-revision-date',
77-
'Project-Id-Version' => 'project-id-version',
7878
'X-Generator' => 'x-generator',
7979
];
8080

81-
foreach ( $translations->getHeaders()->toArray() as $name => $value ) {
82-
if ( isset( $headers_allowlist[ $name ] ) ) {
83-
$result[ $headers_allowlist[ $name ] ] = $value;
81+
foreach ( $headers_allowlist as $name => $key ) {
82+
$value = $translations->getHeaders()->get( $name );
83+
84+
if ( null !== $value ) {
85+
$result[ $key ] = $value;
8486
}
8587
}
8688

‎src/PoGenerator.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected static function addMetaLines( array &$lines, Translation $translation
7979
$lines[] = '#. ' . $comment;
8080
}
8181

82-
foreach ( OrderedReferences::entries( $translation->getReferences() ) as $reference ) {
82+
foreach ( \WP_CLI\I18n\Translation::flattenReferences( $translation->getReferences() ) as $reference ) {
8383
$lines[] = '#: ' . $reference[0] . ( null !== $reference[1] ? ':' . $reference[1] : '' );
8484
}
8585

‎src/Translation.php‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@
33
namespace WP_CLI\I18n;
44

55
use Gettext\Merge;
6+
use Gettext\References;
67
use Gettext\Translation as GettextTranslation;
78

89
/**
910
* A single translation entry.
10-
*
11-
* Keeps flags and references in the order they were added.
1211
*/
1312
class Translation extends GettextTranslation {
1413

15-
protected function __construct( string $id ) {
16-
parent::__construct( $id );
17-
18-
$this->references = new OrderedReferences();
19-
$this->flags = new OrderedFlags();
20-
}
21-
2214
/**
2315
* {@inheritdoc}
2416
*
@@ -55,12 +47,35 @@ public function hasPluralTranslations( bool $check_content = false ): bool {
5547
}
5648

5749
/**
58-
* Returns the references in the order they were added.
50+
* Returns the references as a flat list.
5951
*
6052
* @return array<int, array{0: string, 1: int|null}> List of file name and line number pairs.
6153
*/
6254
public function getReferenceEntries(): array {
63-
return OrderedReferences::entries( $this->references );
55+
return self::flattenReferences( $this->getReferences() );
56+
}
57+
58+
/**
59+
* Turns references grouped by file name into a flat list.
60+
*
61+
* @param References $references References instance.
62+
* @return array<int, array{0: string, 1: int|null}> List of file name and line number pairs.
63+
*/
64+
public static function flattenReferences( References $references ): array {
65+
$entries = [];
66+
67+
foreach ( $references->toArray() as $filename => $lines ) {
68+
if ( empty( $lines ) ) {
69+
$entries[] = [ (string) $filename, null ];
70+
continue;
71+
}
72+
73+
foreach ( $lines as $line ) {
74+
$entries[] = [ (string) $filename, $line ];
75+
}
76+
}
77+
78+
return $entries;
6479
}
6580

6681
/**

‎src/Translations.php‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,29 @@
77
/**
88
* A set of translations.
99
*
10-
* Comes with a set of default headers and keeps headers in the order they were set.
10+
* Comes with a set of default headers.
1111
*/
1212
class Translations extends GettextTranslations {
1313

1414
protected function __construct() {
1515
parent::__construct();
1616

17-
$this->headers = new OrderedHeaders(
18-
[
19-
'Project-Id-Version' => '',
20-
'Report-Msgid-Bugs-To' => '',
21-
'Last-Translator' => '',
22-
'Language-Team' => '',
23-
'MIME-Version' => '1.0',
24-
'Content-Type' => 'text/plain; charset=UTF-8',
25-
'Content-Transfer-Encoding' => '8bit',
26-
'POT-Creation-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
27-
'PO-Revision-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
28-
'Language' => '',
29-
]
30-
);
17+
$defaults = [
18+
'Project-Id-Version' => '',
19+
'Report-Msgid-Bugs-To' => '',
20+
'Last-Translator' => '',
21+
'Language-Team' => '',
22+
'MIME-Version' => '1.0',
23+
'Content-Type' => 'text/plain; charset=UTF-8',
24+
'Content-Transfer-Encoding' => '8bit',
25+
'POT-Creation-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
26+
'PO-Revision-Date' => date( 'c' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
27+
'Language' => '',
28+
];
29+
30+
foreach ( $defaults as $name => $value ) {
31+
$this->headers->set( $name, $value );
32+
}
3133
}
3234

3335
/**

‎src/UpdatePoCommand.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ private function translations_differ( Translations $original, Translations $upda
298298
}
299299

300300
// Compare references (source code locations).
301-
$original_refs = OrderedReferences::entries( $translation->getReferences() );
302-
$updated_refs = OrderedReferences::entries( $updated_translation->getReferences() );
301+
$original_refs = Translation::flattenReferences( $translation->getReferences() );
302+
$updated_refs = Translation::flattenReferences( $updated_translation->getReferences() );
303303

304304
$original_refs_sorted = $original_refs;
305305
$updated_refs_sorted = $updated_refs;

0 commit comments

Comments
 (0)