From e64b16bb425fc5b2ac817574d8b2dc329153d1cd Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 16 Aug 2017 16:46:00 +0200 Subject: [PATCH 1/3] allow loading JSON transaltions for packages --- .../Contracts/Translation/Loader.php | 8 +++++ src/Illuminate/Support/ServiceProvider.php | 11 ++++++ src/Illuminate/Translation/ArrayLoader.php | 11 ++++++ src/Illuminate/Translation/FileLoader.php | 35 ++++++++++++++----- src/Illuminate/Translation/Translator.php | 11 ++++++ .../Translation/TranslationFileLoaderTest.php | 13 +++++++ 6 files changed, 81 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Contracts/Translation/Loader.php b/src/Illuminate/Contracts/Translation/Loader.php index feecc47d1b65..88e012f96ffc 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..3db14f441b32 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..2af9a2fbfa11 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..e73f9f93bfc6 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..53700dea78b0 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..fca9c82c213a 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', '*', '*')); + } } From 28705f332681666f9b4b8c40be1f1425843e16eb Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 16 Aug 2017 17:45:58 +0200 Subject: [PATCH 2/3] update method names --- src/Illuminate/Contracts/Translation/Loader.php | 2 +- src/Illuminate/Support/ServiceProvider.php | 2 +- src/Illuminate/Translation/ArrayLoader.php | 2 +- src/Illuminate/Translation/FileLoader.php | 2 +- src/Illuminate/Translation/Translator.php | 4 ++-- tests/Translation/TranslationFileLoaderTest.php | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Contracts/Translation/Loader.php b/src/Illuminate/Contracts/Translation/Loader.php index 88e012f96ffc..807f0b63cae5 100755 --- a/src/Illuminate/Contracts/Translation/Loader.php +++ b/src/Illuminate/Contracts/Translation/Loader.php @@ -29,7 +29,7 @@ public function addNamespace($namespace, $hint); * @param string $path * @return void */ - public function addJSONPath($path); + 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 3db14f441b32..d648b8f3d8f4 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -106,7 +106,7 @@ protected function loadTranslationsFrom($path, $namespace) * @param string $path * @return void */ - protected function loadJSONTranslationsFrom($path) + protected function loadJsonTranslationsFrom($path) { $this->app['translator']->addJSONPath($path); } diff --git a/src/Illuminate/Translation/ArrayLoader.php b/src/Illuminate/Translation/ArrayLoader.php index 2af9a2fbfa11..47482d45fec6 100644 --- a/src/Illuminate/Translation/ArrayLoader.php +++ b/src/Illuminate/Translation/ArrayLoader.php @@ -50,7 +50,7 @@ public function addNamespace($namespace, $hint) * @param string $path * @return void */ - public function addJSONPath($path) + public function addJsonPath($path) { // } diff --git a/src/Illuminate/Translation/FileLoader.php b/src/Illuminate/Translation/FileLoader.php index e73f9f93bfc6..dd7b4856cb50 100755 --- a/src/Illuminate/Translation/FileLoader.php +++ b/src/Illuminate/Translation/FileLoader.php @@ -170,7 +170,7 @@ public function namespaces() * @param string $path * @return void */ - public function addJSONPath($path) + public function addJsonPath($path) { $this->jsonPaths[] = $path; } diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index 53700dea78b0..51d77e8abf79 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -357,9 +357,9 @@ public function addNamespace($namespace, $hint) * @param string $path * @return void */ - public function addJSONPath($path) + public function addJsonPath($path) { - $this->loader->addJSONPath($path); + $this->loader->addJsonPath($path); } /** diff --git a/tests/Translation/TranslationFileLoaderTest.php b/tests/Translation/TranslationFileLoaderTest.php index fca9c82c213a..29ec77070ad6 100755 --- a/tests/Translation/TranslationFileLoaderTest.php +++ b/tests/Translation/TranslationFileLoaderTest.php @@ -74,7 +74,7 @@ public function testLoadMethodForJSONProperlyCallsLoader() public function testLoadMethodForJSONProperlyCallsLoaderForMultiplePaths() { $loader = new FileLoader($files = m::mock('Illuminate\Filesystem\Filesystem'), __DIR__); - $loader->addJSONPath(__DIR__.'/another'); + $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); From d798a997d735d8230110e3dfecc297d450bdbf02 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 16 Aug 2017 17:46:42 +0200 Subject: [PATCH 3/3] update method names --- src/Illuminate/Support/ServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/ServiceProvider.php b/src/Illuminate/Support/ServiceProvider.php index d648b8f3d8f4..fd472e83ca35 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -108,7 +108,7 @@ protected function loadTranslationsFrom($path, $namespace) */ protected function loadJsonTranslationsFrom($path) { - $this->app['translator']->addJSONPath($path); + $this->app['translator']->addJsonPath($path); } /**