Skip to content

Commit

Permalink
Only load custom translation layer on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed May 31, 2023
1 parent 4cf022b commit 963340f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/ScribeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,8 @@

class ScribeServiceProvider extends ServiceProvider
{
public static bool $customTranslationLayerLoaded = false;

public function boot()
{
$this->registerViews();
Expand Down Expand Up @@ -56,14 +59,8 @@ protected function configureTranslations(): void
__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
Expand Down Expand Up @@ -109,4 +106,16 @@ protected function registerCommands(): void
]);
}
}

// 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 changes: 6 additions & 0 deletions src/Tools/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -361,6 +362,11 @@ public static function filterDocBlockTags(array $tags, string ...$names): array
*/
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) {
Expand Down
1 change: 1 addition & 0 deletions tests/BaseLaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function getEnvironmentSetUp($app)
'database' => ':memory:',
'prefix' => '',
]);
ScribeServiceProvider::$customTranslationLayerLoaded = false;
}

/**
Expand Down

0 comments on commit 963340f

Please sign in to comment.