From b4247feacd6890130420a4de193b8b62c71a5050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 18:08:33 +0300 Subject: [PATCH 01/11] Update ServiceProvider.php --- src/Illuminate/Support/ServiceProvider.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/ServiceProvider.php b/src/Illuminate/Support/ServiceProvider.php index 086cbd220fc2..2f5ada78bb1a 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -236,17 +236,17 @@ protected function loadViewComponentsAs($prefix, array $components) } /** - * Register a translation file namespace. + * Register a translation file namespace or path. * * @param string $path - * @param string $namespace + * @param string|null $namespace * @return void */ - protected function loadTranslationsFrom($path, $namespace) + protected function loadTranslationsFrom($path, $namespace = null) { - $this->callAfterResolving('translator', function ($translator) use ($path, $namespace) { - $translator->addNamespace($namespace, $path); - }); + $this->callAfterResolving('translator', fn($translator) => is_null($namespace) + ? $translator->addPath($path) + : $translator->addNamespace($namespace, $path)); } /** From 221286a6cdc3db2b6e2c335195fd14139a138aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 20:40:26 +0300 Subject: [PATCH 02/11] Update FileLoader.php --- src/Illuminate/Translation/FileLoader.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Illuminate/Translation/FileLoader.php b/src/Illuminate/Translation/FileLoader.php index 65c4c4abc323..2723491e07a8 100755 --- a/src/Illuminate/Translation/FileLoader.php +++ b/src/Illuminate/Translation/FileLoader.php @@ -204,6 +204,16 @@ public function addJsonPath($path) $this->jsonPaths[] = $path; } + /** + * Get an array of all the registered paths to translation files. + * + * @return array + */ + public function paths() + { + return $this->paths; + } + /** * Get an array of all the registered paths to JSON translation files. * From 8d959f0dbdeb0dc54a04893130562aa96cc2ca92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 20:46:09 +0300 Subject: [PATCH 03/11] Update TranslationFileLoaderTest.php --- tests/Translation/TranslationFileLoaderTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Translation/TranslationFileLoaderTest.php b/tests/Translation/TranslationFileLoaderTest.php index e5b3479944ba..1f1164c5e114 100755 --- a/tests/Translation/TranslationFileLoaderTest.php +++ b/tests/Translation/TranslationFileLoaderTest.php @@ -152,4 +152,14 @@ public function testAllAddedJsonPathsReturnProperly() $loader->addJsonPath($path2); $this->assertEquals([$path1, $path2], $loader->jsonPaths()); } + + public function testAllAddedPathsReturnProperly() + { + $loader = new FileLoader(m::mock(Filesystem::class), __DIR__); + $path1 = __DIR__.'/another'; + $path2 = __DIR__.'/another2'; + $loader->addPath($path1); + $loader->addPath($path2); + $this->assertEquals([$path1, $path2], $loader->paths()); + } } From 061bd3bcb55019e5db479b0cdf12f5c519943bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 20:56:08 +0300 Subject: [PATCH 04/11] Update TranslationFileLoaderTest.php --- .../Translation/TranslationFileLoaderTest.php | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/Translation/TranslationFileLoaderTest.php b/tests/Translation/TranslationFileLoaderTest.php index 1f1164c5e114..6d402b71363c 100755 --- a/tests/Translation/TranslationFileLoaderTest.php +++ b/tests/Translation/TranslationFileLoaderTest.php @@ -14,6 +14,69 @@ protected function tearDown(): void m::close(); } + public function testLoadMethodLoadsTranslationsFromAddedPath() + { + $files = m::mock(Filesystem::class); + $loader = new FileLoader($files, __DIR__); + $loader->addPath(__DIR__ . '/another'); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(['baz' => 'backagesplash']); + + $this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash'], $loader->load('en', 'messages')); + } + + public function testLoadMethodHandlesMissingAddedPath() + { + $files = m::mock(Filesystem::class); + $loader = new FileLoader($files, __DIR__); + $loader->addPath(__DIR__ . '/missing'); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/missing/en/messages.php')->andReturn(false); + + $this->assertEquals(['foo' => 'bar'], $loader->load('en', 'messages')); + } + + public function testLoadMethodOverwritesExistingKeysFromAddedPath() + { + $files = m::mock(Filesystem::class); + $loader = new FileLoader($files, __DIR__); + $loader->addPath(__DIR__ . '/another'); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(['foo' => 'baz']); + + $this->assertEquals(['foo' => 'baz'], $loader->load('en', 'messages')); + } + + public function testLoadMethodLoadsTranslationsFromMultipleAddedPaths() + { + $files = m::mock(Filesystem::class); + $loader = new FileLoader($files, __DIR__); + $loader->addPath(__DIR__ . '/another'); + $loader->addPath(__DIR__ . '/yet-another'); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(['baz' => 'backagesplash']); + + $files->shouldReceive('exists')->once()->with(__DIR__ . '/yet-another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/yet-another/en/messages.php')->andReturn(['qux' => 'quux']); + + $this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash', 'qux' => 'quux'], $loader->load('en', 'messages')); + } + public function testLoadMethodWithoutNamespacesProperlyCallsLoader() { $loader = new FileLoader($files = m::mock(Filesystem::class), __DIR__); From 60ff2dc95f010c9eb098a1a6a6fa5e6f53a0b9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 20:59:04 +0300 Subject: [PATCH 05/11] fix --- 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 2f5ada78bb1a..3db60f9f5988 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -244,7 +244,7 @@ protected function loadViewComponentsAs($prefix, array $components) */ protected function loadTranslationsFrom($path, $namespace = null) { - $this->callAfterResolving('translator', fn($translator) => is_null($namespace) + $this->callAfterResolving('translator', fn ($translator) => is_null($namespace) ? $translator->addPath($path) : $translator->addNamespace($namespace, $path)); } From a65734896a2f6b809314b99a55ec32eedf5331c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 21:03:49 +0300 Subject: [PATCH 06/11] Update SupportServiceProviderTest.php --- tests/Support/SupportServiceProviderTest.php | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/Support/SupportServiceProviderTest.php b/tests/Support/SupportServiceProviderTest.php index 2cf1cf17b63f..f9dc9bcd4ce4 100644 --- a/tests/Support/SupportServiceProviderTest.php +++ b/tests/Support/SupportServiceProviderTest.php @@ -5,6 +5,7 @@ use Illuminate\Config\Repository as Config; use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; +use Illuminate\Translation\Translator; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -160,6 +161,34 @@ public function testPublishesMigrations() $this->assertNotContains('source/tagged/five', ServiceProvider::publishableMigrationPaths()); } + + public function testLoadTranslationsFromWithoutNamespace() + { + $translator = m::mock(Translator::class); + $translator->shouldReceive('addPath')->once()->with(__DIR__ . '/translations'); + + $this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) { + $callback($translator); + return true; + })); + + $provider = new ServiceProviderForTestingOne($this->app); + $provider->loadTranslationsFrom(__DIR__ . '/translations'); + } + + public function testLoadTranslationsFromWithNamespace() + { + $translator = m::mock(Translator::class); + $translator->shouldReceive('addNamespace')->once()->with('namespace', __DIR__ . '/translations'); + + $this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) { + $callback($translator); + return true; + })); + + $provider = new ServiceProviderForTestingOne($this->app); + $provider->loadTranslationsFrom(__DIR__ . '/translations', 'namespace'); + } } class ServiceProviderForTestingOne extends ServiceProvider @@ -179,6 +208,13 @@ public function boot() $this->publishesMigrations(['source/tagged/three' => 'destination/tagged/three'], 'tag_three'); $this->publishesMigrations(['source/tagged/multiple_two' => 'destination/tagged/multiple_two'], ['tag_four', 'tag_five']); } + + protected function loadTranslationsFrom($path, $namespace = null) + { + $this->callAfterResolving('translator', fn ($translator) => is_null($namespace) + ? $translator->addPath($path) + : $translator->addNamespace($namespace, $path)); + } } class ServiceProviderForTestingTwo extends ServiceProvider From eb3fbe391f711a79126dabaedf7467aecebaac6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 21:43:06 +0300 Subject: [PATCH 07/11] fix --- tests/Support/SupportServiceProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportServiceProviderTest.php b/tests/Support/SupportServiceProviderTest.php index f9dc9bcd4ce4..43d203e78815 100644 --- a/tests/Support/SupportServiceProviderTest.php +++ b/tests/Support/SupportServiceProviderTest.php @@ -209,7 +209,7 @@ public function boot() $this->publishesMigrations(['source/tagged/multiple_two' => 'destination/tagged/multiple_two'], ['tag_four', 'tag_five']); } - protected function loadTranslationsFrom($path, $namespace = null) + public function loadTranslationsFrom($path, $namespace = null) { $this->callAfterResolving('translator', fn ($translator) => is_null($namespace) ? $translator->addPath($path) From 198d318941e077b1ba975e8f4de0682bfd6d04c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 21:53:12 +0300 Subject: [PATCH 08/11] fix --- tests/Translation/TranslationFileLoaderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Translation/TranslationFileLoaderTest.php b/tests/Translation/TranslationFileLoaderTest.php index 6d402b71363c..e90c629ed394 100755 --- a/tests/Translation/TranslationFileLoaderTest.php +++ b/tests/Translation/TranslationFileLoaderTest.php @@ -223,6 +223,6 @@ public function testAllAddedPathsReturnProperly() $path2 = __DIR__.'/another2'; $loader->addPath($path1); $loader->addPath($path2); - $this->assertEquals([$path1, $path2], $loader->paths()); + $this->assertEquals([$path1, $path2], array_slice($loader->paths(), 1)); } } From a73f56b7644a4296d12585cc09038c432dcf627c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 22:03:47 +0300 Subject: [PATCH 09/11] fix style --- .../Translation/TranslationFileLoaderTest.php | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/Translation/TranslationFileLoaderTest.php b/tests/Translation/TranslationFileLoaderTest.php index e90c629ed394..b7e74f18bfaa 100755 --- a/tests/Translation/TranslationFileLoaderTest.php +++ b/tests/Translation/TranslationFileLoaderTest.php @@ -18,13 +18,13 @@ public function testLoadMethodLoadsTranslationsFromAddedPath() { $files = m::mock(Filesystem::class); $loader = new FileLoader($files, __DIR__); - $loader->addPath(__DIR__ . '/another'); + $loader->addPath(__DIR__.'/another'); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(['baz' => 'backagesplash']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']); $this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash'], $loader->load('en', 'messages')); } @@ -33,12 +33,12 @@ public function testLoadMethodHandlesMissingAddedPath() { $files = m::mock(Filesystem::class); $loader = new FileLoader($files, __DIR__); - $loader->addPath(__DIR__ . '/missing'); + $loader->addPath(__DIR__.'/missing'); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/missing/en/messages.php')->andReturn(false); + $files->shouldReceive('exists')->once()->with(__DIR__.'/missing/en/messages.php')->andReturn(false); $this->assertEquals(['foo' => 'bar'], $loader->load('en', 'messages')); } @@ -47,13 +47,13 @@ public function testLoadMethodOverwritesExistingKeysFromAddedPath() { $files = m::mock(Filesystem::class); $loader = new FileLoader($files, __DIR__); - $loader->addPath(__DIR__ . '/another'); + $loader->addPath(__DIR__.'/another'); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(['foo' => 'baz']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['foo' => 'baz']); $this->assertEquals(['foo' => 'baz'], $loader->load('en', 'messages')); } @@ -62,17 +62,17 @@ public function testLoadMethodLoadsTranslationsFromMultipleAddedPaths() { $files = m::mock(Filesystem::class); $loader = new FileLoader($files, __DIR__); - $loader->addPath(__DIR__ . '/another'); - $loader->addPath(__DIR__ . '/yet-another'); + $loader->addPath(__DIR__.'/another'); + $loader->addPath(__DIR__.'/yet-another'); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/en/messages.php')->andReturn(['foo' => 'bar']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/en/messages.php')->andReturn(['foo' => 'bar']); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/another/en/messages.php')->andReturn(['baz' => 'backagesplash']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/another/en/messages.php')->andReturn(['baz' => 'backagesplash']); - $files->shouldReceive('exists')->once()->with(__DIR__ . '/yet-another/en/messages.php')->andReturn(true); - $files->shouldReceive('getRequire')->once()->with(__DIR__ . '/yet-another/en/messages.php')->andReturn(['qux' => 'quux']); + $files->shouldReceive('exists')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(true); + $files->shouldReceive('getRequire')->once()->with(__DIR__.'/yet-another/en/messages.php')->andReturn(['qux' => 'quux']); $this->assertEquals(['foo' => 'bar', 'baz' => 'backagesplash', 'qux' => 'quux'], $loader->load('en', 'messages')); } From ea06d3ac8980aec0ecae53c2b090c182b41625c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 22:04:56 +0300 Subject: [PATCH 10/11] fix style --- tests/Support/SupportServiceProviderTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Support/SupportServiceProviderTest.php b/tests/Support/SupportServiceProviderTest.php index 43d203e78815..a2a2719ed880 100644 --- a/tests/Support/SupportServiceProviderTest.php +++ b/tests/Support/SupportServiceProviderTest.php @@ -165,7 +165,7 @@ public function testPublishesMigrations() public function testLoadTranslationsFromWithoutNamespace() { $translator = m::mock(Translator::class); - $translator->shouldReceive('addPath')->once()->with(__DIR__ . '/translations'); + $translator->shouldReceive('addPath')->once()->with(__DIR__.'/translations'); $this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) { $callback($translator); @@ -173,13 +173,13 @@ public function testLoadTranslationsFromWithoutNamespace() })); $provider = new ServiceProviderForTestingOne($this->app); - $provider->loadTranslationsFrom(__DIR__ . '/translations'); + $provider->loadTranslationsFrom(__DIR__.'/translations'); } public function testLoadTranslationsFromWithNamespace() { $translator = m::mock(Translator::class); - $translator->shouldReceive('addNamespace')->once()->with('namespace', __DIR__ . '/translations'); + $translator->shouldReceive('addNamespace')->once()->with('namespace', __DIR__.'/translations'); $this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) { $callback($translator); @@ -187,7 +187,7 @@ public function testLoadTranslationsFromWithNamespace() })); $provider = new ServiceProviderForTestingOne($this->app); - $provider->loadTranslationsFrom(__DIR__ . '/translations', 'namespace'); + $provider->loadTranslationsFrom(__DIR__.'/translations', 'namespace'); } } From 15d9cf9027d65116791da8276b6b32dc30adb038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sel=C3=A7uk=20=C3=87ukur?= <5716652+selcukcukur@users.noreply.github.com> Date: Wed, 29 Jan 2025 22:06:30 +0300 Subject: [PATCH 11/11] Update SupportServiceProviderTest.php --- tests/Support/SupportServiceProviderTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Support/SupportServiceProviderTest.php b/tests/Support/SupportServiceProviderTest.php index a2a2719ed880..9c4f825c540c 100644 --- a/tests/Support/SupportServiceProviderTest.php +++ b/tests/Support/SupportServiceProviderTest.php @@ -169,6 +169,7 @@ public function testLoadTranslationsFromWithoutNamespace() $this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) { $callback($translator); + return true; })); @@ -183,6 +184,7 @@ public function testLoadTranslationsFromWithNamespace() $this->app->shouldReceive('afterResolving')->once()->with('translator', m::on(function ($callback) use ($translator) { $callback($translator); + return true; }));