diff --git a/config/config.php b/config/config.php index d7228eaef..c233e1329 100644 --- a/config/config.php +++ b/config/config.php @@ -188,6 +188,16 @@ */ 'migrations' => true, + /* + |-------------------------------------------------------------------------- + | Translations + |-------------------------------------------------------------------------- + | + | This option for register lang file automatically. + | + */ + 'translations' => false, + ], /* diff --git a/src/LaravelModulesServiceProvider.php b/src/LaravelModulesServiceProvider.php index 1fd804373..c65c97c45 100644 --- a/src/LaravelModulesServiceProvider.php +++ b/src/LaravelModulesServiceProvider.php @@ -3,11 +3,13 @@ namespace Nwidart\Modules; use Composer\InstalledVersions; +use Illuminate\Contracts\Translation\Translator as TranslatorContract; use Illuminate\Database\Migrations\Migrator; use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Console\AboutCommand; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Event; +use Illuminate\Translation\Translator; use Nwidart\Modules\Constants\ModuleEvent; use Nwidart\Modules\Contracts\RepositoryInterface; use Nwidart\Modules\Exceptions\InvalidActivatorClass; @@ -51,6 +53,7 @@ public function register() $this->registerProviders(); $this->registerMigrations(); + $this->registerTransactions(); $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'modules'); } @@ -116,6 +119,34 @@ protected function registerMigrations(): void }); } + protected function registerTransactions(): void + { + if (! $this->app['config']->get('modules.auto-discover.translations', true)) { + return; + } + + $this->callAfterResolving('translator', function (TranslatorContract $translator) { + if (! $translator instanceof Translator) { + return; + } + + $path = implode(DIRECTORY_SEPARATOR, [ + $this->app['config']->get('modules.paths.modules'), + '*', + 'lang', + ]); + + collect(glob($path, GLOB_ONLYDIR)) + ->each(function (string $path) use ($translator) { + preg_match('/Modules\/([^\/]+)/', $path, $matches); + + $translator->addNamespace(strtolower($matches[1]), $path); + $translator->addJsonPath($path); + }); + }); + } + + private function registerEvents(): void { Event::listen(