diff --git a/src/Illuminate/Contracts/Translation/Loader.php b/src/Illuminate/Contracts/Translation/Loader.php index feecc47d1b65..807f0b63cae5 100755 --- a/src/Illuminate/Contracts/Translation/Loader.php +++ b/src/Illuminate/Contracts/Translation/Loader.php @@ -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. * diff --git a/src/Illuminate/Support/ServiceProvider.php b/src/Illuminate/Support/ServiceProvider.php index 379bef46fe5f..fd472e83ca35 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -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. * diff --git a/src/Illuminate/Translation/ArrayLoader.php b/src/Illuminate/Translation/ArrayLoader.php index b853fc750a23..47482d45fec6 100644 --- a/src/Illuminate/Translation/ArrayLoader.php +++ b/src/Illuminate/Translation/ArrayLoader.php @@ -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. * diff --git a/src/Illuminate/Translation/FileLoader.php b/src/Illuminate/Translation/FileLoader.php index a42397ec1b78..dd7b4856cb50 100755 --- a/src/Illuminate/Translation/FileLoader.php +++ b/src/Illuminate/Translation/FileLoader.php @@ -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. * @@ -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 == '*') { @@ -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; + }, []); } /** @@ -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; + } } diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index db4aec2848e4..51d77e8abf79 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -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. * diff --git a/tests/Translation/TranslationFileLoaderTest.php b/tests/Translation/TranslationFileLoaderTest.php index da6bdc3cc027..29ec77070ad6 100755 --- a/tests/Translation/TranslationFileLoaderTest.php +++ b/tests/Translation/TranslationFileLoaderTest.php @@ -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', '*', '*')); + } }