Skip to content

Commit

Permalink
feat: sort extracted messages by key (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey authored Feb 1, 2022
1 parent f7884b7 commit af5d303
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 82 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

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

### Deprecated

Expand Down
7 changes: 7 additions & 0 deletions src/Format/Writer/ChromeWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
use FormatPHP\Format\WriterInterface;
use FormatPHP\Format\WriterOptions;

use function ksort;

use const SORT_FLAG_CASE;
use const SORT_NATURAL;

/**
* Chrome formatter for FormatPHP
*
Expand Down Expand Up @@ -65,6 +70,8 @@ public function __invoke(DescriptorCollection $collection, WriterOptions $option
$format[(string) $item->getId()] = $message;
}

ksort($format, SORT_NATURAL | SORT_FLAG_CASE);

return $format;
}
}
5 changes: 5 additions & 0 deletions src/Format/Writer/FormatPHPWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
use function array_merge;
use function ksort;

use const SORT_FLAG_CASE;
use const SORT_NATURAL;

/**
* Default formatter for FormatPHP
*
Expand Down Expand Up @@ -96,6 +99,8 @@ public function __invoke(DescriptorCollection $collection, WriterOptions $option
$format[(string) $item->getId()] = $message;
}

ksort($format, SORT_NATURAL | SORT_FLAG_CASE);

return $format;
}
}
7 changes: 7 additions & 0 deletions src/Format/Writer/SimpleWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
use FormatPHP\Format\WriterInterface;
use FormatPHP\Format\WriterOptions;

use function ksort;

use const SORT_FLAG_CASE;
use const SORT_NATURAL;

/**
* A simple formatter for FormatPHP, producing message key-value pairs
*
Expand All @@ -52,6 +57,8 @@ public function __invoke(DescriptorCollection $collection, WriterOptions $option
$simple[(string) $item->getId()] = $item->getDefaultMessage();
}

ksort($simple, SORT_NATURAL | SORT_FLAG_CASE);

return $simple;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"defaultMessage": "You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }",
"description": "A description with multiple lines and extra whitespace."
},
"Q+U0TW": {
"defaultMessage": "Welcome!"
},
"Soex4s": {
"defaultMessage": "This is a default message",
"description": "A simple description of a fixture for testing purposes."
},
"xgMWoP": {
"defaultMessage": "This is a default message"
},
"Q+U0TW": {
"defaultMessage": "Welcome!"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"how.many.pets": {
"defaultMessage": "Ļâśṭ ṭíṁè Ḭ ćḫèćǩèḋ, {gender, select, male{<italicized>ḫè</italicized> ḫâḋ} female{<italicized>śḫè</italicized> ḫâḋ} other{<italicized>ṭḫèẏ</italicized> ḫâḋ}} {petCount, plural, =0{<bold>ńŏ</bold> ṗèṭś} =1{<bold>â</bold> ṗèṭ} other{<bold>#</bold> ṗèṭś}}."
},
"start.with.tag": {
"defaultMessage": "<foo>{argument}</foo>"
},
"start.with.argument": {
"defaultMessage": "{argument}"
},
"start.with.tag": {
"defaultMessage": "<foo>{argument}</foo>"
},
"value.with.non-ascii.characters": {
"defaultMessage": "Ẅè’ḋ ńèèḋ ṭŏ ṭëśṭ śøṁè ńŏń-áśćíí ćḫâŕâćṭèŕś, ṭŏŏ…"
}
Expand Down
106 changes: 53 additions & 53 deletions tests/Extractor/MessageExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public function testProcessBasic(): void
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'OpKKos' => [
'defaultMessage' => 'Hello!',
],
Expand All @@ -87,22 +90,19 @@ public function testProcessBasic(): void
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
'description' => 'A description with multiple lines and extra whitespace.',
],
'welcome' => [
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'Soex4s' => [
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'welcome' => [
'defaultMessage' => 'Welcome!',
],
'xgMWoP' => [
'defaultMessage' => 'This is a default message',
],
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
],
$messages,
);
Expand Down Expand Up @@ -140,16 +140,16 @@ public function testProcessWithFormatPhpFormatterName(): void
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
'description' => 'A description with multiple lines and extra whitespace.',
],
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
'Soex4s' => [
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'xgMWoP' => [
'defaultMessage' => 'This is a default message',
],
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
],
$messages,
);
Expand Down Expand Up @@ -180,9 +180,9 @@ public function testProcessWithSimpleFormatterName(): void
[
'aTestId' => 'This is a default message',
'photos.count' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
'Q+U0TW' => 'Welcome!',
'Soex4s' => 'This is a default message',
'xgMWoP' => 'This is a default message',
'Q+U0TW' => 'Welcome!',
],
$messages,
);
Expand Down Expand Up @@ -230,16 +230,16 @@ public function testProcessWithSmartlingFormatterName(): void
'description' => 'A description with multiple lines and extra whitespace.',
'message' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
],
'Q+U0TW' => [
'message' => 'Welcome!',
],
'Soex4s' => [
'description' => 'A simple description of a fixture for testing purposes.',
'message' => 'This is a default message',
],
'xgMWoP' => [
'message' => 'This is a default message',
],
'Q+U0TW' => [
'message' => 'Welcome!',
],
],
$messages,
);
Expand Down Expand Up @@ -276,16 +276,16 @@ public function testProcessWithCrowdinFormatterName(): void
'description' => 'A description with multiple lines and extra whitespace.',
'message' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
],
'Q+U0TW' => [
'message' => 'Welcome!',
],
'Soex4s' => [
'description' => 'A simple description of a fixture for testing purposes.',
'message' => 'This is a default message',
],
'xgMWoP' => [
'message' => 'This is a default message',
],
'Q+U0TW' => [
'message' => 'Welcome!',
],
],
$messages,
);
Expand Down Expand Up @@ -322,16 +322,16 @@ public function testProcessWithChromeFormatterName(): void
'description' => 'A description with multiple lines and extra whitespace.',
'message' => 'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
],
'Q+U0TW' => [
'message' => 'Welcome!',
],
'Soex4s' => [
'description' => 'A simple description of a fixture for testing purposes.',
'message' => 'This is a default message',
],
'xgMWoP' => [
'message' => 'This is a default message',
],
'Q+U0TW' => [
'message' => 'Welcome!',
],
],
$messages,
);
Expand Down Expand Up @@ -623,32 +623,32 @@ public function testProcessWithCustomParser(): void
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'customGoodbye' => [
'defaultMessage' => 'Custom Goodbye!',
],
'customWelcome' => [
'defaultMessage' => 'Custom Welcome!',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'photos.count' => [
'defaultMessage' =>
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
'description' => 'A description with multiple lines and extra whitespace.',
],
'welcome' => [
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'Soex4s' => [
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'xgMWoP' => [
'defaultMessage' => 'This is a default message',
],
'Q+U0TW' => [
'welcome' => [
'defaultMessage' => 'Welcome!',
],
'customWelcome' => [
'defaultMessage' => 'Custom Welcome!',
],
'customGoodbye' => [
'defaultMessage' => 'Custom Goodbye!',
'xgMWoP' => [
'defaultMessage' => 'This is a default message',
],
],
$messages,
Expand Down Expand Up @@ -715,32 +715,32 @@ public function testProcessWithCustomParserAsClosure(): void
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'customGoodbye' => [
'defaultMessage' => 'Custom Goodbye!',
],
'customWelcome' => [
'defaultMessage' => 'Custom Welcome!',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'photos.count' => [
'defaultMessage' =>
'You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.} }',
'description' => 'A description with multiple lines and extra whitespace.',
],
'welcome' => [
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'Soex4s' => [
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'xgMWoP' => [
'defaultMessage' => 'This is a default message',
],
'Q+U0TW' => [
'welcome' => [
'defaultMessage' => 'Welcome!',
],
'customWelcome' => [
'defaultMessage' => 'Custom Welcome!',
],
'customGoodbye' => [
'defaultMessage' => 'Custom Goodbye!',
'xgMWoP' => [
'defaultMessage' => 'This is a default message',
],
],
$messages,
Expand Down Expand Up @@ -810,6 +810,9 @@ public function testProcessFlatten(): void
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'OpKKos' => [
'defaultMessage' => 'Hello!',
],
Expand All @@ -818,22 +821,19 @@ public function testProcessFlatten(): void
. '=1{You have one photo.} other{You have # photos.}}',
'description' => 'A description with multiple lines and extra whitespace.',
],
'welcome' => [
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
'goodbye' => [
'defaultMessage' => 'Goodbye!',
],
'Soex4s' => [
'defaultMessage' => 'This is a default message',
'description' => 'A simple description of a fixture for testing purposes.',
],
'welcome' => [
'defaultMessage' => 'Welcome!',
],
'xgMWoP' => [
'defaultMessage' => 'This is a default message',
],
'Q+U0TW' => [
'defaultMessage' => 'Welcome!',
],
],
$messages,
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Format/Writer/ChromeWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function testFormatter(): void

$this->assertSame(
[
'foo' => ['message' => ''],
'bar' => ['message' => 'some message'],
'baz' => ['description' => 'a description', 'message' => 'another message'],
'foo' => ['message' => ''],
],
$formatter($collection, $options),
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Format/Writer/CrowdinWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function testFormatter(): void

$this->assertSame(
[
'foo' => ['message' => ''],
'bar' => ['message' => 'some message'],
'baz' => ['description' => 'a description', 'message' => 'another message'],
'foo' => ['message' => ''],
],
$formatter($collection, $options),
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Format/Writer/FormatPHPWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function testFormatterBasic(): void

$this->assertSame(
[
'foo' => ['defaultMessage' => ''],
'bar' => ['defaultMessage' => 'some message'],
'baz' => ['defaultMessage' => 'another message', 'description' => 'a description'],
'foo' => ['defaultMessage' => ''],
],
$formatter($collection, $options),
);
Expand Down
Loading

0 comments on commit af5d303

Please sign in to comment.