mirror of
https://github.com/ambieco/scribe.git
synced 2026-04-23 18:10:06 +08:00
Only load custom translation layer on demand
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -23,6 +23,7 @@ class BaseLaravelTest extends TestCase
|
||||
'database' => ':memory:',
|
||||
'prefix' => '',
|
||||
]);
|
||||
ScribeServiceProvider::$customTranslationLayerLoaded = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user