Skip to content

Commit

Permalink
Add optional --no-sort option (#123)
Browse files Browse the repository at this point in the history
* Added optional --no-sort option

* Added check for Laravel 6
  • Loading branch information
jeremy-smith-maco authored and rmariuzzo committed Nov 11, 2019
1 parent 0100db6 commit 3a0b7f6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -157,7 +158,10 @@ protected function getMessages()
}
}

$this->sortMessages($messages);
if (!$noSort)
{
$this->sortMessages($messages);
}

return $messages;
}
Expand Down Expand Up @@ -202,7 +206,7 @@ protected function isMessagesExcluded($filePath)

return true;
}

private function getVendorKey($key)
{
$keyParts = explode('.', $key, 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 43 additions & 1 deletion tests/specs/LangJsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 3a0b7f6

Please sign in to comment.