Skip to content

Commit

Permalink
Merge branch 'loadJSONtranslations/packages' of https://github.com/th…
Browse files Browse the repository at this point in the history
…emsaid/framework into themsaid-loadJSONtranslations/packages
  • Loading branch information
taylorotwell committed Aug 16, 2017
2 parents fb48968 + d798a99 commit 9a2ec2d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/Illuminate/Contracts/Translation/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function load($locale, $group, $namespace = null);
*/
public function addNamespace($namespace, $hint);

/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path);

/**
* Get an array of all the registered namespaces.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ protected function loadTranslationsFrom($path, $namespace)
$this->app['translator']->addNamespace($namespace, $path);
}

/**
* Register a JSON translation file path.
*
* @param string $path
* @return void
*/
protected function loadJsonTranslationsFrom($path)
{
$this->app['translator']->addJsonPath($path);
}

/**
* Register a database migration path.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Translation/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public function addNamespace($namespace, $hint)
//
}

/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
//
}

/**
* Add messages to the loader.
*
Expand Down
35 changes: 27 additions & 8 deletions src/Illuminate/Translation/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class FileLoader implements Loader
*/
protected $path;

/**
* All of the paths of JSON files.
*
* @var string
*/
protected $jsonPaths = [];

/**
* All of the namespace hints.
*
Expand Down Expand Up @@ -52,7 +59,7 @@ public function __construct(Filesystem $files, $path)
public function load($locale, $group, $namespace = null)
{
if ($group == '*' && $namespace == '*') {
return $this->loadJsonPath($this->path, $locale);
return $this->loadJsonPaths($locale);
}

if (is_null($namespace) || $namespace == '*') {
Expand Down Expand Up @@ -121,17 +128,18 @@ protected function loadPath($path, $locale, $group)
/**
* Load a locale from the given JSON file path.
*
* @param string $path
* @param string $locale
* @return array
*/
protected function loadJsonPath($path, $locale)
protected function loadJsonPaths($locale)
{
if ($this->files->exists($full = "{$path}/{$locale}.json")) {
return json_decode($this->files->get($full), true);
}

return [];
return collect(array_merge($this->jsonPaths, [$this->path]))
->reduce(function ($output, $path) use ($locale) {
return $this->files->exists($full = "{$path}/{$locale}.json")
? array_merge($output,
json_decode($this->files->get($full), true)
) : $output;
}, []);
}

/**
Expand All @@ -155,4 +163,15 @@ public function namespaces()
{
return $this->hints;
}

/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->jsonPaths[] = $path;
}
}
11 changes: 11 additions & 0 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ public function addNamespace($namespace, $hint)
$this->loader->addNamespace($namespace, $hint);
}

/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->loader->addJsonPath($path);
}

/**
* Parse a key into namespace, group, and item.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Translation/TranslationFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,17 @@ public function testLoadMethodForJSONProperlyCallsLoader()

$this->assertEquals(['foo' => 'bar'], $loader->load('en', '*', '*'));
}

public function testLoadMethodForJSONProperlyCallsLoaderForMultiplePaths()
{
$loader = new FileLoader($files = m::mock('Illuminate\Filesystem\Filesystem'), __DIR__);
$loader->addJsonPath(__DIR__.'/another');

$files->shouldReceive('exists')->once()->with(__DIR__.'/en.json')->andReturn(true);
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en.json')->andReturn(true);
$files->shouldReceive('get')->once()->with(__DIR__.'/en.json')->andReturn('{"foo":"bar"}');
$files->shouldReceive('get')->once()->with(__DIR__.'/another/en.json')->andReturn('{"foo":"backagebar", "baz": "backagesplash"}');

$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash'], $loader->load('en', '*', '*'));
}
}

0 comments on commit 9a2ec2d

Please sign in to comment.