Skip to content

Commit

Permalink
Merge pull request #1920 from alissn/RegisterLangFiles
Browse files Browse the repository at this point in the history
Auto-Register Module Language Files
  • Loading branch information
dcblogdev committed Aug 14, 2024
2 parents 7079478 + 7092f06 commit fa92a63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@
*/
'migrations' => true,

/*
|--------------------------------------------------------------------------
| Translations
|--------------------------------------------------------------------------
|
| This option for register lang file automatically.
|
*/
'translations' => false,

],

/*
Expand Down
29 changes: 29 additions & 0 deletions src/LaravelModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +53,7 @@ public function register()
$this->registerProviders();

$this->registerMigrations();
$this->registerTransactions();

$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'modules');
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit fa92a63

Please sign in to comment.