Skip to content

Commit

Permalink
Merge pull request #47 from cybercog/feature/exclude-messages-config
Browse files Browse the repository at this point in the history
Add an ability to configurate list of files to convert to be used by javascript
  • Loading branch information
rmariuzzo committed Jul 28, 2016
2 parents 609a3b1 + fe66dd7 commit d43122a
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 13 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ In your Laravel app go to `app/config/app.php` and add the following service pro
```
That's it!

## Configuration

Run `php artisan vendor:publish --provider="Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider"` first. This command copies the package's default configuration to `config/localization-js.php`.

You may edit this file to define the messages you need in your Javascript code. Just edit the `messages` array in the config file. *Empty messages array will include all the language files in build.*

To make only `pagination.php` & `validation.php` files to be included in build process:

```php
<?php

return [

'messages' => [
'pagination',
'validation',
],

];
```

## Usage

This project comes with a command that generate the JavaScript version of all your messages found at: `app/lang` or `resources/lang` directory. The resulting JavaScript file will have the whole bunch of messages and a thin library similar to Laravel's `Lang` class.
Expand Down
40 changes: 40 additions & 0 deletions src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class LangJsGenerator
*/
protected $sourcePath;

/**
* List of messages should be included in build.
*
* @var array
*/
protected $messagesIncluded = [];

/**
* Construct a new LangJsGenerator instance.
*
Expand All @@ -30,6 +37,7 @@ public function __construct(File $file, $sourcePath)
{
$this->file = $file;
$this->sourcePath = $sourcePath;
$this->collectMessagesIncluded();
}

/**
Expand Down Expand Up @@ -78,6 +86,10 @@ protected function getMessages()

if ( $this->file->extension($pathName) !== 'php' ) continue;

if ($this->isMessagesExcluded($file->getFileName())) {
continue;
}

$key = substr($pathName, 0, -4);
$key = str_replace('\\', '.', $key);
$key = str_replace('/', '.', $key);
Expand All @@ -102,4 +114,32 @@ protected function prepareTarget($target)
$this->file->makeDirectory($dirname, null, true);
}
}

/**
* If messages should be excluded from build.
*
* @param $filename
* @return bool
*/
protected function isMessagesExcluded($filename)
{
if (empty($this->messagesIncluded)) {
return false;
}

$filename = substr($filename, 0, -4);
if (in_array($filename, $this->messagesIncluded)) {
return false;
}

return true;
}

/**
* Prepare list of messages should be included in build.
*/
protected function collectMessagesIncluded()
{
$this->messagesIncluded = config('localization-js.messages');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ class LaravelJsLocalizationServiceProvider extends ServiceProvider
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
// Publish config files
$this->publishes([
__DIR__ . '/../../config/config.php' => config_path('localization-js.php'),
]);

$this->mergeConfigFrom(
__DIR__ . '/../../config/config.php', 'localization-js'
);
}

/**
* Register the service provider.
*
Expand Down
88 changes: 75 additions & 13 deletions tests/LangJsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,52 @@
*/
class LangJsCommandTest extends TestCase
{
private $outputFilePath;

public function __construct()
{
$this->setOutputFilePath(__DIR__ . '/output/lang.js');
}

public function getOutputFilePath()
{
return $this->outputFilePath;
}

public function setOutputFilePath($filePath)
{
$this->outputFilePath = $filePath;
}

/**
* Test the command.
*/
public function testShouldCommandRun()
{
$generator = new LangJsGenerator(new File(), __DIR__.'/fixtures/lang');
$generator = new LangJsGenerator(new File(), __DIR__ . '/fixtures/lang');

$command = new LangJsCommand($generator);
$command->setLaravel($this->app);

$code = $this->runCommand($command, ['target' => __DIR__.'/output/lang.js']);
$code = $this->runCommand($command, ['target' => $this->getOutputFilePath()]);

$this->assertRunsWithSuccess($code);

$this->assertFileExists(__DIR__.'/output/lang.js');
$this->assertFileExists($this->getOutputFilePath());

$template = __DIR__.'/../src/Mariuzzo/LaravelJsLocalization/Generators/Templates/langjs_with_messages.js';
$template = __DIR__ . '/../src/Mariuzzo/LaravelJsLocalization/Generators/Templates/langjs_with_messages.js';

$this->assertFileExists($template);

$this->assertFileNotEquals($template, __DIR__.'/output/lang.js');
$this->assertFileNotEquals($template, $this->getOutputFilePath());
}

/**
* @return void
*/
public function testShouldTemplateHasHandlebars()
{
$template = __DIR__.'/../src/Mariuzzo/LaravelJsLocalization/Generators/Templates/langjs_with_messages.js';
$template = __DIR__ . '/../src/Mariuzzo/LaravelJsLocalization/Generators/Templates/langjs_with_messages.js';

$this->assertFileExists($template);

Expand All @@ -62,11 +79,9 @@ public function testShouldTemplateHasHandlebars()
*/
public function testShouldOutputHasNotHandlebars()
{
$output = __DIR__.'/output/lang.js';

$this->assertFileExists($output);
$this->assertFileExists($this->getOutputFilePath());

$contents = file_get_contents($output);
$contents = file_get_contents($this->getOutputFilePath());

$this->assertNotEmpty($contents);

Expand All @@ -75,6 +90,53 @@ public function testShouldOutputHasNotHandlebars()
$this->assertHasNotHandlebars('langjs', $contents);
}

/**
* @return void
*/
public function testAllFilesShouldBeConverted()
{
$generator = new LangJsGenerator(new File(), __DIR__ . '/fixtures/lang');

$command = new LangJsCommand($generator);
$command->setLaravel($this->app);

$code = $this->runCommand($command, ['target' => $this->getOutputFilePath()]);

$this->assertRunsWithSuccess($code);

$this->assertFileExists($this->getOutputFilePath());

$contents = file_get_contents($this->getOutputFilePath());

$this->assertContains('createCongregation', $contents);
}

/**
* @return void
*/
public function testFilesSelectedInConfigShouldBeConverted()
{
$this->app['config']->set('localization-js.messages', [
'messages',
]);

$generator = new LangJsGenerator(new File(), __DIR__ . '/fixtures/lang');

$command = new LangJsCommand($generator);
$command->setLaravel($this->app);

$code = $this->runCommand($command, ['target' => $this->getOutputFilePath()]);

$this->assertRunsWithSuccess($code);

$this->assertFileExists($this->getOutputFilePath());

$contents = file_get_contents($this->getOutputFilePath());

$this->assertContains('en.messages', $contents);
$this->assertNotContains('en.validation', $contents);
}

/**
* Run the command.
* @param \Illuminate\Console\Command $command
Expand All @@ -88,7 +150,7 @@ protected function runCommand($command, $input = [])

/**
* Assert the code return is success.
* @param int $code
* @param int $code
* @param null $message
*/
protected function assertRunsWithSuccess($code, $message = null)
Expand All @@ -102,7 +164,7 @@ protected function assertRunsWithSuccess($code, $message = null)
*/
protected function assertHasHandlebars($handle, $contents)
{
$this->assertEquals(1, preg_match('/\'\{(\s)'.preg_quote($handle).'(\s)\}\'/', $contents));
$this->assertEquals(1, preg_match('/\'\{(\s)' . preg_quote($handle) . '(\s)\}\'/', $contents));
}

/**
Expand All @@ -111,6 +173,6 @@ protected function assertHasHandlebars($handle, $contents)
*/
protected function assertHasNotHandlebars($handle, $contents)
{
$this->assertEquals(0, preg_match('/\'\{(\s)'.preg_quote($handle).'(\s)\}\'/', $contents));
$this->assertEquals(0, preg_match('/\'\{(\s)' . preg_quote($handle) . '(\s)\}\'/', $contents));
}
}

0 comments on commit d43122a

Please sign in to comment.