Skip to content

Commit 01571b3

Browse files
authored
[11.x] Optimize loadTranslationsFrom function for simplicity and clarity (#54407)
* Update ServiceProvider.php * Update FileLoader.php * Update TranslationFileLoaderTest.php * Update TranslationFileLoaderTest.php * fix * Update SupportServiceProviderTest.php * fix * fix * fix style * fix style * Update SupportServiceProviderTest.php
1 parent 4a74d20 commit 01571b3

File tree

4 files changed

+127
-6
lines changed

4 files changed

+127
-6
lines changed

src/Illuminate/Support/ServiceProvider.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,17 @@ protected function loadViewComponentsAs($prefix, array $components)
236236
}
237237

238238
/**
239-
* Register a translation file namespace.
239+
* Register a translation file namespace or path.
240240
*
241241
* @param string $path
242-
* @param string $namespace
242+
* @param string|null $namespace
243243
* @return void
244244
*/
245-
protected function loadTranslationsFrom($path, $namespace)
245+
protected function loadTranslationsFrom($path, $namespace = null)
246246
{
247-
$this->callAfterResolving('translator', function ($translator) use ($path, $namespace) {
248-
$translator->addNamespace($namespace, $path);
249-
});
247+
$this->callAfterResolving('translator', fn ($translator) => is_null($namespace)
248+
? $translator->addPath($path)
249+
: $translator->addNamespace($namespace, $path));
250250
}
251251

252252
/**

src/Illuminate/Translation/FileLoader.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ public function addJsonPath($path)
204204
$this->jsonPaths[] = $path;
205205
}
206206

207+
/**
208+
* Get an array of all the registered paths to translation files.
209+
*
210+
* @return array
211+
*/
212+
public function paths()
213+
{
214+
return $this->paths;
215+
}
216+
207217
/**
208218
* Get an array of all the registered paths to JSON translation files.
209219
*

tests/Support/SupportServiceProviderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Config\Repository as Config;
66
use Illuminate\Foundation\Application;
77
use Illuminate\Support\ServiceProvider;
8+
use Illuminate\Translation\Translator;
89
use Mockery as m;
910
use PHPUnit\Framework\TestCase;
1011

@@ -160,6 +161,36 @@ public function testPublishesMigrations()
160161

161162
$this->assertNotContains('source/tagged/five', ServiceProvider::publishableMigrationPaths());
162163
}
164+
165+
public function testLoadTranslationsFromWithoutNamespace()
166+
{
167+
$translator = m::mock(Translator::class);
168+
$translator->shouldReceive('addPath')->once()->with(__DIR__.'/translations');
169+
170+
$this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) {
171+
$callback($translator);
172+
173+
return true;
174+
}));
175+
176+
$provider = new ServiceProviderForTestingOne($this->app);
177+
$provider->loadTranslationsFrom(__DIR__.'/translations');
178+
}
179+
180+
public function testLoadTranslationsFromWithNamespace()
181+
{
182+
$translator = m::mock(Translator::class);
183+
$translator->shouldReceive('addNamespace')->once()->with('namespace', __DIR__.'/translations');
184+
185+
$this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) {
186+
$callback($translator);
187+
188+
return true;
189+
}));
190+
191+
$provider = new ServiceProviderForTestingOne($this->app);
192+
$provider->loadTranslationsFrom(__DIR__.'/translations', 'namespace');
193+
}
163194
}
164195

165196
class ServiceProviderForTestingOne extends ServiceProvider
@@ -179,6 +210,13 @@ public function boot()
179210
$this->publishesMigrations(['source/tagged/three' => 'destination/tagged/three'], 'tag_three');
180211
$this->publishesMigrations(['source/tagged/multiple_two' => 'destination/tagged/multiple_two'], ['tag_four', 'tag_five']);
181212
}
213+
214+
public function loadTranslationsFrom($path, $namespace = null)
215+
{
216+
$this->callAfterResolving('translator', fn ($translator) => is_null($namespace)
217+
? $translator->addPath($path)
218+
: $translator->addNamespace($namespace, $path));
219+
}
182220
}
183221

184222
class ServiceProviderForTestingTwo extends ServiceProvider

tests/Translation/TranslationFileLoaderTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,69 @@ protected function tearDown(): void
1414
m::close();
1515
}
1616

17+
public function testLoadMethodLoadsTranslationsFromAddedPath()
18+
{
19+
$files = m::mock(Filesystem::class);
20+
$loader = new FileLoader($files, __DIR__);
21+
$loader->addPath(__DIR__.'/another');
22+
23+
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
24+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
25+
26+
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
27+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']);
28+
29+
$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash'], $loader->load('en', 'messages'));
30+
}
31+
32+
public function testLoadMethodHandlesMissingAddedPath()
33+
{
34+
$files = m::mock(Filesystem::class);
35+
$loader = new FileLoader($files, __DIR__);
36+
$loader->addPath(__DIR__.'/missing');
37+
38+
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
39+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
40+
41+
$files->shouldReceive('exists')->once()->with(__DIR__.'/missing/en/messages.php')->andReturn(false);
42+
43+
$this->assertEquals(['foo' => 'bar'], $loader->load('en', 'messages'));
44+
}
45+
46+
public function testLoadMethodOverwritesExistingKeysFromAddedPath()
47+
{
48+
$files = m::mock(Filesystem::class);
49+
$loader = new FileLoader($files, __DIR__);
50+
$loader->addPath(__DIR__.'/another');
51+
52+
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
53+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
54+
55+
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
56+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['foo' => 'baz']);
57+
58+
$this->assertEquals(['foo' => 'baz'], $loader->load('en', 'messages'));
59+
}
60+
61+
public function testLoadMethodLoadsTranslationsFromMultipleAddedPaths()
62+
{
63+
$files = m::mock(Filesystem::class);
64+
$loader = new FileLoader($files, __DIR__);
65+
$loader->addPath(__DIR__.'/another');
66+
$loader->addPath(__DIR__.'/yet-another');
67+
68+
$files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true);
69+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']);
70+
71+
$files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true);
72+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']);
73+
74+
$files->shouldReceive('exists')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(true);
75+
$files->shouldReceive('getRequire')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(['qux' => 'quux']);
76+
77+
$this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash', 'qux' => 'quux'], $loader->load('en', 'messages'));
78+
}
79+
1780
public function testLoadMethodWithoutNamespacesProperlyCallsLoader()
1881
{
1982
$loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__);
@@ -152,4 +215,14 @@ public function testAllAddedJsonPathsReturnProperly()
152215
$loader->addJsonPath($path2);
153216
$this->assertEquals([$path1, $path2], $loader->jsonPaths());
154217
}
218+
219+
public function testAllAddedPathsReturnProperly()
220+
{
221+
$loader = new FileLoader(m::mock(Filesystem::class), __DIR__);
222+
$path1 = __DIR__.'/another';
223+
$path2 = __DIR__.'/another2';
224+
$loader->addPath($path1);
225+
$loader->addPath($path2);
226+
$this->assertEquals([$path1, $path2], array_slice($loader->paths(), 1));
227+
}
155228
}

0 commit comments

Comments
 (0)