Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] allow loading JSON translations for packages #20599

Merged
merged 3 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '*', '*'));
}
}