Browse Source

Only load custom translation layer on demand

shalvah 1 year ago
parent
commit
963340f2be
3 changed files with 24 additions and 8 deletions
  1. 17 8
      src/ScribeServiceProvider.php
  2. 6 0
      src/Tools/Utils.php
  3. 1 0
      tests/BaseLaravelTest.php

+ 17 - 8
src/ScribeServiceProvider.php

@@ -2,6 +2,7 @@
 
 namespace Knuckles\Scribe;
 
+use Illuminate\Support\Facades\Artisan;
 use Illuminate\Support\ServiceProvider;
 use Knuckles\Scribe\Commands\DiffConfig;
 use Knuckles\Scribe\Commands\GenerateDocumentation;
@@ -15,6 +16,8 @@ use Knuckles\Scribe\Writing\CustomTranslationsLoader;
 
 class ScribeServiceProvider extends ServiceProvider
 {
+    public static bool $customTranslationLayerLoaded = false;
+
     public function boot()
     {
         $this->registerViews();
@@ -56,14 +59,8 @@ class ScribeServiceProvider extends ServiceProvider
             __DIR__.'/../lang/' => $this->app->langPath(),
         ], 'scribe-translations');
 
-        if ($this->app->runningInConsole()) {
-            $this->loadTranslationsFrom($this->app->langPath('scribe.php'), 'scribe');
-            $this->loadTranslationsFrom(realpath(__DIR__.'/../lang'), 'scribe');
-
-            $this->app->extend('translation.loader', function ($defaultFileLoader) {
-                return new CustomTranslationsLoader($defaultFileLoader);
-            });
-        }
+        $this->loadTranslationsFrom($this->app->langPath('scribe.php'), 'scribe');
+        $this->loadTranslationsFrom(realpath(__DIR__ . '/../lang'), 'scribe');
     }
 
     protected function registerViews(): void
@@ -109,4 +106,16 @@ class ScribeServiceProvider extends ServiceProvider
             ]);
         }
     }
+
+    // Allows our custom translation layer to be loaded on demand,
+    // so we minimize issues with interference from framework/package/environment.
+    // ALso, Laravel's `app->runningInConsole()` isn't reliable enough. See issue #676
+    public function loadCustomTranslationLayer(): void
+    {
+        $this->app->extend('translation.loader', function ($defaultFileLoader) {
+            return new CustomTranslationsLoader($defaultFileLoader);
+        });
+        $this->app->forgetInstance('translator');
+        self::$customTranslationLayerLoaded = true;
+    }
 }

+ 6 - 0
src/Tools/Utils.php

@@ -12,6 +12,7 @@ use Illuminate\Routing\Route;
 use Illuminate\Support\Str;
 use Knuckles\Scribe\Exceptions\CouldntFindFactory;
 use Knuckles\Scribe\Exceptions\CouldntGetRouteDetails;
+use Knuckles\Scribe\ScribeServiceProvider;
 use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
 use League\Flysystem\Filesystem;
 use League\Flysystem\Local\LocalFilesystemAdapter;
@@ -361,6 +362,11 @@ class Utils
      */
     public static function trans(string $key, array $replace = [])
     {
+        // We only load our custom translation layer if we really need it
+        if (!ScribeServiceProvider::$customTranslationLayerLoaded) {
+            (new ScribeServiceProvider(app()))->loadCustomTranslationLayer();
+        }
+
         $translation = trans($key, $replace);
 
         if ($translation === $key || $translation === null) {

+ 1 - 0
tests/BaseLaravelTest.php

@@ -23,6 +23,7 @@ class BaseLaravelTest extends TestCase
             'database' => ':memory:',
             'prefix'   => '',
         ]);
+        ScribeServiceProvider::$customTranslationLayerLoaded = false;
     }
 
     /**