From 3a0b7f620315a1cd71f92f9231ef9047360bc72f Mon Sep 17 00:00:00 2001 From: Jeremy Smith Date: Mon, 11 Nov 2019 12:21:37 +1000 Subject: [PATCH] Add optional --no-sort option (#123) * Added optional --no-sort option * Added check for Laravel 6 --- .../Commands/LangJsCommand.php | 2 + .../Generators/LangJsGenerator.php | 14 +++--- .../LaravelJsLocalizationServiceProvider.php | 4 +- tests/specs/LangJsCommandTest.php | 44 ++++++++++++++++++- 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php b/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php index 813b5bc..b98111c 100644 --- a/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php +++ b/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php @@ -66,6 +66,7 @@ public function handle() 'json' => $this->option('json'), 'no-lib' => $this->option('no-lib'), 'source' => $this->option('source'), + 'no-sort' => $this->option('no-sort'), ]; if ($this->generator->generate($target, $options)) { @@ -111,6 +112,7 @@ protected function getOptions() ['no-lib', 'nl', InputOption::VALUE_NONE, 'Do not include the lang.js library.', null], ['json', 'j', InputOption::VALUE_NONE, 'Only output the messages json.', null], ['source', 's', InputOption::VALUE_REQUIRED, 'Specifying a custom source folder', null], + ['no-sort', 'ns', InputOption::VALUE_NONE, 'Do not sort the messages', null], ]; } } diff --git a/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php b/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php index eb174c0..d756989 100644 --- a/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php +++ b/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php @@ -38,7 +38,7 @@ class LangJsGenerator /** * Name of the domain in which all string-translation should be stored under. * More about string-translation: https://laravel.com/docs/master/localization#retrieving-translation-strings - * + * * @var string */ protected $stringsDomain = 'strings'; @@ -70,7 +70,7 @@ public function generate($target, $options) $this->sourcePath = $options['source']; } - $messages = $this->getMessages(); + $messages = $this->getMessages($options['no-sort']); $this->prepareTarget($target); if ($options['no-lib']) { @@ -111,11 +111,12 @@ protected function sortMessages(&$messages) /** * Return all language messages. * + * @param bool $noSort Whether sorting of the messages should be skipped. * @return array * * @throws \Exception */ - protected function getMessages() + protected function getMessages($noSort) { $messages = []; $path = $this->sourcePath; @@ -157,7 +158,10 @@ protected function getMessages() } } - $this->sortMessages($messages); + if (!$noSort) + { + $this->sortMessages($messages); + } return $messages; } @@ -202,7 +206,7 @@ protected function isMessagesExcluded($filePath) return true; } - + private function getVendorKey($key) { $keyParts = explode('.', $key, 4); diff --git a/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php b/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php index 348d759..eab4548 100644 --- a/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php +++ b/src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php @@ -67,9 +67,9 @@ public function register() $this->app->singleton('localization.js', function ($app) { $app = $this->app; $laravelMajorVersion = (int) $app::VERSION; - + $files = $app['files']; - + if ($laravelMajorVersion === 4) { $langs = $app['path.base'].'/app/lang'; } elseif ($laravelMajorVersion >= 5) { diff --git a/tests/specs/LangJsCommandTest.php b/tests/specs/LangJsCommandTest.php index 7db8039..0952a11 100644 --- a/tests/specs/LangJsCommandTest.php +++ b/tests/specs/LangJsCommandTest.php @@ -127,7 +127,7 @@ public function testAllFilesShouldBeConverted() $contents = file_get_contents($this->outputFilePath); $this->assertContains('gm8ft2hrrlq1u6m54we9udi', $contents); - + $this->assertNotContains('vendor.nonameinc.en.messages', $contents); $this->assertNotContains('vendor.nonameinc.es.messages', $contents); $this->assertNotContains('vendor.nonameinc.ht.messages', $contents); @@ -323,6 +323,48 @@ public function testChangeDefaultLangSourceFolderForOneThatDosentExist() ); } + /** + * Test that messages are sorted alphabetically by default. + */ + public function testDoesSortMessages() + { + $generator = new LangJsGenerator(new File(), $this->langPath, ['pagination']); + + $command = new LangJsCommand($generator); + $command->setLaravel($this->app); + + $code = $this->runCommand($command, ['target' => $this->outputFilePath]); + $this->assertRunsWithSuccess($code); + $this->assertFileExists($this->outputFilePath); + + $contents = file_get_contents($this->outputFilePath); + $this->assertContains('en.pagination', $contents); + $this->assertContains('{"next":"Next »","previous":"« Previous"}', $contents); + + $this->cleanupOutputDirectory(); + } + + /** + * Tests that the --no-sort option does not sort messages. + */ + public function testDoesNotSortMessages() + { + $generator = new LangJsGenerator(new File(), $this->langPath, ['pagination']); + + $command = new LangJsCommand($generator); + $command->setLaravel($this->app); + + $code = $this->runCommand($command, ['target' => $this->outputFilePath, '--no-sort' => true]); + $this->assertRunsWithSuccess($code); + $this->assertFileExists($this->outputFilePath); + + $contents = file_get_contents($this->outputFilePath); + $this->assertContains('en.pagination', $contents); + $this->assertContains('{"previous":"« Previous","next":"Next »"}', $contents); + + $this->cleanupOutputDirectory(); + } + /** * Run the command. *