Skip to content

Commit 3a0b7f6

Browse files
jeremy-smith-macormariuzzo
authored andcommitted
Add optional --no-sort option (#123)
* Added optional --no-sort option * Added check for Laravel 6
1 parent 0100db6 commit 3a0b7f6

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function handle()
6666
'json' => $this->option('json'),
6767
'no-lib' => $this->option('no-lib'),
6868
'source' => $this->option('source'),
69+
'no-sort' => $this->option('no-sort'),
6970
];
7071

7172
if ($this->generator->generate($target, $options)) {
@@ -111,6 +112,7 @@ protected function getOptions()
111112
['no-lib', 'nl', InputOption::VALUE_NONE, 'Do not include the lang.js library.', null],
112113
['json', 'j', InputOption::VALUE_NONE, 'Only output the messages json.', null],
113114
['source', 's', InputOption::VALUE_REQUIRED, 'Specifying a custom source folder', null],
115+
['no-sort', 'ns', InputOption::VALUE_NONE, 'Do not sort the messages', null],
114116
];
115117
}
116118
}

src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class LangJsGenerator
3838
/**
3939
* Name of the domain in which all string-translation should be stored under.
4040
* More about string-translation: https://laravel.com/docs/master/localization#retrieving-translation-strings
41-
*
41+
*
4242
* @var string
4343
*/
4444
protected $stringsDomain = 'strings';
@@ -70,7 +70,7 @@ public function generate($target, $options)
7070
$this->sourcePath = $options['source'];
7171
}
7272

73-
$messages = $this->getMessages();
73+
$messages = $this->getMessages($options['no-sort']);
7474
$this->prepareTarget($target);
7575

7676
if ($options['no-lib']) {
@@ -111,11 +111,12 @@ protected function sortMessages(&$messages)
111111
/**
112112
* Return all language messages.
113113
*
114+
* @param bool $noSort Whether sorting of the messages should be skipped.
114115
* @return array
115116
*
116117
* @throws \Exception
117118
*/
118-
protected function getMessages()
119+
protected function getMessages($noSort)
119120
{
120121
$messages = [];
121122
$path = $this->sourcePath;
@@ -157,7 +158,10 @@ protected function getMessages()
157158
}
158159
}
159160

160-
$this->sortMessages($messages);
161+
if (!$noSort)
162+
{
163+
$this->sortMessages($messages);
164+
}
161165

162166
return $messages;
163167
}
@@ -202,7 +206,7 @@ protected function isMessagesExcluded($filePath)
202206

203207
return true;
204208
}
205-
209+
206210
private function getVendorKey($key)
207211
{
208212
$keyParts = explode('.', $key, 4);

src/Mariuzzo/LaravelJsLocalization/LaravelJsLocalizationServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public function register()
6767
$this->app->singleton('localization.js', function ($app) {
6868
$app = $this->app;
6969
$laravelMajorVersion = (int) $app::VERSION;
70-
70+
7171
$files = $app['files'];
72-
72+
7373
if ($laravelMajorVersion === 4) {
7474
$langs = $app['path.base'].'/app/lang';
7575
} elseif ($laravelMajorVersion >= 5) {

tests/specs/LangJsCommandTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function testAllFilesShouldBeConverted()
127127

128128
$contents = file_get_contents($this->outputFilePath);
129129
$this->assertContains('gm8ft2hrrlq1u6m54we9udi', $contents);
130-
130+
131131
$this->assertNotContains('vendor.nonameinc.en.messages', $contents);
132132
$this->assertNotContains('vendor.nonameinc.es.messages', $contents);
133133
$this->assertNotContains('vendor.nonameinc.ht.messages', $contents);
@@ -323,6 +323,48 @@ public function testChangeDefaultLangSourceFolderForOneThatDosentExist()
323323
);
324324
}
325325

326+
/**
327+
* Test that messages are sorted alphabetically by default.
328+
*/
329+
public function testDoesSortMessages()
330+
{
331+
$generator = new LangJsGenerator(new File(), $this->langPath, ['pagination']);
332+
333+
$command = new LangJsCommand($generator);
334+
$command->setLaravel($this->app);
335+
336+
$code = $this->runCommand($command, ['target' => $this->outputFilePath]);
337+
$this->assertRunsWithSuccess($code);
338+
$this->assertFileExists($this->outputFilePath);
339+
340+
$contents = file_get_contents($this->outputFilePath);
341+
$this->assertContains('en.pagination', $contents);
342+
$this->assertContains('{"next":"Next »","previous":"« Previous"}', $contents);
343+
344+
$this->cleanupOutputDirectory();
345+
}
346+
347+
/**
348+
* Tests that the --no-sort option does not sort messages.
349+
*/
350+
public function testDoesNotSortMessages()
351+
{
352+
$generator = new LangJsGenerator(new File(), $this->langPath, ['pagination']);
353+
354+
$command = new LangJsCommand($generator);
355+
$command->setLaravel($this->app);
356+
357+
$code = $this->runCommand($command, ['target' => $this->outputFilePath, '--no-sort' => true]);
358+
$this->assertRunsWithSuccess($code);
359+
$this->assertFileExists($this->outputFilePath);
360+
361+
$contents = file_get_contents($this->outputFilePath);
362+
$this->assertContains('en.pagination', $contents);
363+
$this->assertContains('{"previous":"« Previous","next":"Next »"}', $contents);
364+
365+
$this->cleanupOutputDirectory();
366+
}
367+
326368
/**
327369
* Run the command.
328370
*

0 commit comments

Comments
 (0)