From e82bcc631c11053add83636a2f08fffa1e70ad7c Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Fri, 13 Sep 2024 23:53:39 +0330 Subject: [PATCH 1/2] Revert "Revert "Merge pull request #1920 from alissn/RegisterLangFiles"" This reverts commit b51e39ce8283b118349bcc8510205020a8539080. --- config/config.php | 10 +++++++++ src/LaravelModulesServiceProvider.php | 29 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) 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..db8d27274 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,32 @@ 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('/\/([^\/]+)\/lang/', $path, $matches); + $translator->addNamespace(strtolower($matches[1]), $path); + $translator->addJsonPath($path); + }); + }); + } + + private function registerEvents(): void { Event::listen( From 35873cd7460d8c33664ab80174fdb6f7aa6fe15f Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Fri, 13 Sep 2024 23:57:11 +0330 Subject: [PATCH 2/2] [style] fix typo --- src/LaravelModulesServiceProvider.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LaravelModulesServiceProvider.php b/src/LaravelModulesServiceProvider.php index db8d27274..06545b301 100644 --- a/src/LaravelModulesServiceProvider.php +++ b/src/LaravelModulesServiceProvider.php @@ -53,7 +53,7 @@ public function register() $this->registerProviders(); $this->registerMigrations(); - $this->registerTransactions(); + $this->registerTranslations(); $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'modules'); } @@ -119,7 +119,7 @@ protected function registerMigrations(): void }); } - protected function registerTransactions(): void + protected function registerTranslations(): void { if (! $this->app['config']->get('modules.auto-discover.translations', true)) { return;