Skip to content

Commit af5d303

Browse files
authored
feat: sort extracted messages by key (#41)
1 parent f7884b7 commit af5d303

17 files changed

+101
-82
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414

1515
### Changed
1616

17-
- Nothing.
17+
- Sort extracted messages descending by key, using a natural, case-insensitive sorting algorithm.
1818

1919
### Deprecated
2020

src/Format/Writer/ChromeWriter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
use FormatPHP\Format\WriterInterface;
2828
use FormatPHP\Format\WriterOptions;
2929

30+
use function ksort;
31+
32+
use const SORT_FLAG_CASE;
33+
use const SORT_NATURAL;
34+
3035
/**
3136
* Chrome formatter for FormatPHP
3237
*
@@ -65,6 +70,8 @@ public function __invoke(DescriptorCollection $collection, WriterOptions $option
6570
$format[(string) $item->getId()] = $message;
6671
}
6772

73+
ksort($format, SORT_NATURAL | SORT_FLAG_CASE);
74+
6875
return $format;
6976
}
7077
}

src/Format/Writer/FormatPHPWriter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
use function array_merge;
3232
use function ksort;
3333

34+
use const SORT_FLAG_CASE;
35+
use const SORT_NATURAL;
36+
3437
/**
3538
* Default formatter for FormatPHP
3639
*
@@ -96,6 +99,8 @@ public function __invoke(DescriptorCollection $collection, WriterOptions $option
9699
$format[(string) $item->getId()] = $message;
97100
}
98101

102+
ksort($format, SORT_NATURAL | SORT_FLAG_CASE);
103+
99104
return $format;
100105
}
101106
}

src/Format/Writer/SimpleWriter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
use FormatPHP\Format\WriterInterface;
2828
use FormatPHP\Format\WriterOptions;
2929

30+
use function ksort;
31+
32+
use const SORT_FLAG_CASE;
33+
use const SORT_NATURAL;
34+
3035
/**
3136
* A simple formatter for FormatPHP, producing message key-value pairs
3237
*
@@ -52,6 +57,8 @@ public function __invoke(DescriptorCollection $collection, WriterOptions $option
5257
$simple[(string) $item->getId()] = $item->getDefaultMessage();
5358
}
5459

60+
ksort($simple, SORT_NATURAL | SORT_FLAG_CASE);
61+
5562
return $simple;
5663
}
5764
}

tests/Console/Command/__snapshots__/ExtractCommandTest__testExecuteWithValidationHasNoErrors__1.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"defaultMessage": "You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }",
88
"description": "A description with multiple lines and extra whitespace."
99
},
10+
"Q+U0TW": {
11+
"defaultMessage": "Welcome!"
12+
},
1013
"Soex4s": {
1114
"defaultMessage": "This is a default message",
1215
"description": "A simple description of a fixture for testing purposes."
1316
},
1417
"xgMWoP": {
1518
"defaultMessage": "This is a default message"
16-
},
17-
"Q+U0TW": {
18-
"defaultMessage": "Welcome!"
1919
}
2020
}

tests/Console/Command/__snapshots__/PseudoLocaleCommandTest__testExecute__1.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"how.many.pets": {
66
"defaultMessage": "Ļâśṭ ṭíṁè Ḭ ćḫèćǩèḋ, {gender, select, male{<italicized>ḫè</italicized> ḫâḋ} female{<italicized>śḫè</italicized> ḫâḋ} other{<italicized>ṭḫèẏ</italicized> ḫâḋ}} {petCount, plural, =0{<bold>ńŏ</bold> ṗèṭś} =1{<bold>â</bold> ṗèṭ} other{<bold>#</bold> ṗèṭś}}."
77
},
8-
"start.with.tag": {
9-
"defaultMessage": "<foo>{argument}</foo>"
10-
},
118
"start.with.argument": {
129
"defaultMessage": "{argument}"
1310
},
11+
"start.with.tag": {
12+
"defaultMessage": "<foo>{argument}</foo>"
13+
},
1414
"value.with.non-ascii.characters": {
1515
"defaultMessage": "Ẅè’ḋ ńèèḋ ṭŏ ṭëśṭ śøṁè ńŏń-áśćíí ćḫâŕâćṭèŕś, ṭŏŏ…"
1616
}

tests/Extractor/MessageExtractorTest.php

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function testProcessBasic(): void
7979
'defaultMessage' => 'This is a default message',
8080
'description' => 'A simple description of a fixture for testing purposes.',
8181
],
82+
'goodbye' => [
83+
'defaultMessage' => 'Goodbye!',
84+
],
8285
'OpKKos' => [
8386
'defaultMessage' => 'Hello!',
8487
],
@@ -87,22 +90,19 @@ public function testProcessBasic(): void
8790
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
8891
'description' => 'A description with multiple lines and extra whitespace.',
8992
],
90-
'welcome' => [
93+
'Q+U0TW' => [
9194
'defaultMessage' => 'Welcome!',
9295
],
93-
'goodbye' => [
94-
'defaultMessage' => 'Goodbye!',
95-
],
9696
'Soex4s' => [
9797
'defaultMessage' => 'This is a default message',
9898
'description' => 'A simple description of a fixture for testing purposes.',
9999
],
100+
'welcome' => [
101+
'defaultMessage' => 'Welcome!',
102+
],
100103
'xgMWoP' => [
101104
'defaultMessage' => 'This is a default message',
102105
],
103-
'Q+U0TW' => [
104-
'defaultMessage' => 'Welcome!',
105-
],
106106
],
107107
$messages,
108108
);
@@ -140,16 +140,16 @@ public function testProcessWithFormatPhpFormatterName(): void
140140
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
141141
'description' => 'A description with multiple lines and extra whitespace.',
142142
],
143+
'Q+U0TW' => [
144+
'defaultMessage' => 'Welcome!',
145+
],
143146
'Soex4s' => [
144147
'defaultMessage' => 'This is a default message',
145148
'description' => 'A simple description of a fixture for testing purposes.',
146149
],
147150
'xgMWoP' => [
148151
'defaultMessage' => 'This is a default message',
149152
],
150-
'Q+U0TW' => [
151-
'defaultMessage' => 'Welcome!',
152-
],
153153
],
154154
$messages,
155155
);
@@ -180,9 +180,9 @@ public function testProcessWithSimpleFormatterName(): void
180180
[
181181
'aTestId' => 'This is a default message',
182182
'photos.count' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
183+
'Q+U0TW' => 'Welcome!',
183184
'Soex4s' => 'This is a default message',
184185
'xgMWoP' => 'This is a default message',
185-
'Q+U0TW' => 'Welcome!',
186186
],
187187
$messages,
188188
);
@@ -230,16 +230,16 @@ public function testProcessWithSmartlingFormatterName(): void
230230
'description' => 'A description with multiple lines and extra whitespace.',
231231
'message' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
232232
],
233+
'Q+U0TW' => [
234+
'message' => 'Welcome!',
235+
],
233236
'Soex4s' => [
234237
'description' => 'A simple description of a fixture for testing purposes.',
235238
'message' => 'This is a default message',
236239
],
237240
'xgMWoP' => [
238241
'message' => 'This is a default message',
239242
],
240-
'Q+U0TW' => [
241-
'message' => 'Welcome!',
242-
],
243243
],
244244
$messages,
245245
);
@@ -276,16 +276,16 @@ public function testProcessWithCrowdinFormatterName(): void
276276
'description' => 'A description with multiple lines and extra whitespace.',
277277
'message' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
278278
],
279+
'Q+U0TW' => [
280+
'message' => 'Welcome!',
281+
],
279282
'Soex4s' => [
280283
'description' => 'A simple description of a fixture for testing purposes.',
281284
'message' => 'This is a default message',
282285
],
283286
'xgMWoP' => [
284287
'message' => 'This is a default message',
285288
],
286-
'Q+U0TW' => [
287-
'message' => 'Welcome!',
288-
],
289289
],
290290
$messages,
291291
);
@@ -322,16 +322,16 @@ public function testProcessWithChromeFormatterName(): void
322322
'description' => 'A description with multiple lines and extra whitespace.',
323323
'message' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
324324
],
325+
'Q+U0TW' => [
326+
'message' => 'Welcome!',
327+
],
325328
'Soex4s' => [
326329
'description' => 'A simple description of a fixture for testing purposes.',
327330
'message' => 'This is a default message',
328331
],
329332
'xgMWoP' => [
330333
'message' => 'This is a default message',
331334
],
332-
'Q+U0TW' => [
333-
'message' => 'Welcome!',
334-
],
335335
],
336336
$messages,
337337
);
@@ -623,32 +623,32 @@ public function testProcessWithCustomParser(): void
623623
'defaultMessage' => 'This is a default message',
624624
'description' => 'A simple description of a fixture for testing purposes.',
625625
],
626+
'customGoodbye' => [
627+
'defaultMessage' => 'Custom Goodbye!',
628+
],
629+
'customWelcome' => [
630+
'defaultMessage' => 'Custom Welcome!',
631+
],
632+
'goodbye' => [
633+
'defaultMessage' => 'Goodbye!',
634+
],
626635
'photos.count' => [
627636
'defaultMessage' =>
628637
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
629638
'description' => 'A description with multiple lines and extra whitespace.',
630639
],
631-
'welcome' => [
640+
'Q+U0TW' => [
632641
'defaultMessage' => 'Welcome!',
633642
],
634-
'goodbye' => [
635-
'defaultMessage' => 'Goodbye!',
636-
],
637643
'Soex4s' => [
638644
'defaultMessage' => 'This is a default message',
639645
'description' => 'A simple description of a fixture for testing purposes.',
640646
],
641-
'xgMWoP' => [
642-
'defaultMessage' => 'This is a default message',
643-
],
644-
'Q+U0TW' => [
647+
'welcome' => [
645648
'defaultMessage' => 'Welcome!',
646649
],
647-
'customWelcome' => [
648-
'defaultMessage' => 'Custom Welcome!',
649-
],
650-
'customGoodbye' => [
651-
'defaultMessage' => 'Custom Goodbye!',
650+
'xgMWoP' => [
651+
'defaultMessage' => 'This is a default message',
652652
],
653653
],
654654
$messages,
@@ -715,32 +715,32 @@ public function testProcessWithCustomParserAsClosure(): void
715715
'defaultMessage' => 'This is a default message',
716716
'description' => 'A simple description of a fixture for testing purposes.',
717717
],
718+
'customGoodbye' => [
719+
'defaultMessage' => 'Custom Goodbye!',
720+
],
721+
'customWelcome' => [
722+
'defaultMessage' => 'Custom Welcome!',
723+
],
724+
'goodbye' => [
725+
'defaultMessage' => 'Goodbye!',
726+
],
718727
'photos.count' => [
719728
'defaultMessage' =>
720729
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
721730
'description' => 'A description with multiple lines and extra whitespace.',
722731
],
723-
'welcome' => [
732+
'Q+U0TW' => [
724733
'defaultMessage' => 'Welcome!',
725734
],
726-
'goodbye' => [
727-
'defaultMessage' => 'Goodbye!',
728-
],
729735
'Soex4s' => [
730736
'defaultMessage' => 'This is a default message',
731737
'description' => 'A simple description of a fixture for testing purposes.',
732738
],
733-
'xgMWoP' => [
734-
'defaultMessage' => 'This is a default message',
735-
],
736-
'Q+U0TW' => [
739+
'welcome' => [
737740
'defaultMessage' => 'Welcome!',
738741
],
739-
'customWelcome' => [
740-
'defaultMessage' => 'Custom Welcome!',
741-
],
742-
'customGoodbye' => [
743-
'defaultMessage' => 'Custom Goodbye!',
742+
'xgMWoP' => [
743+
'defaultMessage' => 'This is a default message',
744744
],
745745
],
746746
$messages,
@@ -810,6 +810,9 @@ public function testProcessFlatten(): void
810810
'defaultMessage' => 'This is a default message',
811811
'description' => 'A simple description of a fixture for testing purposes.',
812812
],
813+
'goodbye' => [
814+
'defaultMessage' => 'Goodbye!',
815+
],
813816
'OpKKos' => [
814817
'defaultMessage' => 'Hello!',
815818
],
@@ -818,22 +821,19 @@ public function testProcessFlatten(): void
818821
. '=1{You have one photo.} other{You have # photos.}}',
819822
'description' => 'A description with multiple lines and extra whitespace.',
820823
],
821-
'welcome' => [
824+
'Q+U0TW' => [
822825
'defaultMessage' => 'Welcome!',
823826
],
824-
'goodbye' => [
825-
'defaultMessage' => 'Goodbye!',
826-
],
827827
'Soex4s' => [
828828
'defaultMessage' => 'This is a default message',
829829
'description' => 'A simple description of a fixture for testing purposes.',
830830
],
831+
'welcome' => [
832+
'defaultMessage' => 'Welcome!',
833+
],
831834
'xgMWoP' => [
832835
'defaultMessage' => 'This is a default message',
833836
],
834-
'Q+U0TW' => [
835-
'defaultMessage' => 'Welcome!',
836-
],
837837
],
838838
$messages,
839839
);

tests/Format/Writer/ChromeWriterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function testFormatter(): void
2525

2626
$this->assertSame(
2727
[
28-
'foo' => ['message' => ''],
2928
'bar' => ['message' => 'some message'],
3029
'baz' => ['description' => 'a description', 'message' => 'another message'],
30+
'foo' => ['message' => ''],
3131
],
3232
$formatter($collection, $options),
3333
);

tests/Format/Writer/CrowdinWriterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function testFormatter(): void
2525

2626
$this->assertSame(
2727
[
28-
'foo' => ['message' => ''],
2928
'bar' => ['message' => 'some message'],
3029
'baz' => ['description' => 'a description', 'message' => 'another message'],
30+
'foo' => ['message' => ''],
3131
],
3232
$formatter($collection, $options),
3333
);

tests/Format/Writer/FormatPHPWriterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function testFormatterBasic(): void
2525

2626
$this->assertSame(
2727
[
28-
'foo' => ['defaultMessage' => ''],
2928
'bar' => ['defaultMessage' => 'some message'],
3029
'baz' => ['defaultMessage' => 'another message', 'description' => 'a description'],
30+
'foo' => ['defaultMessage' => ''],
3131
],
3232
$formatter($collection, $options),
3333
);

0 commit comments

Comments
 (0)