app->runningInConsole() && ! $this->app->runningUnitTests()) { return; } foreach ($this->collectLocalesStrings() as $locale) { // * Suported locales. Cache::rememberForever(sprintf('translations.%s', $locale), function () use ($locale) { $phpTransFallback = $this->phpTranslations(config()->string('app.fallback_locale')); $phpTransLocale = $this->phpTranslations($locale); $jsonTransFallback = $this->jsonTranslations(config()->string('app.fallback_locale')); $jsonTransLocale = $this->jsonTranslations($locale); return [ 'php' => ToolboxHelper::arrayMergeRecursiveDistinct( $phpTransFallback, $phpTransLocale, ), 'json' => ToolboxHelper::arrayMergeRecursiveDistinct( $jsonTransFallback, $jsonTransLocale, ), ]; }); } // End foreach. } /** * Gather provided langs checking from files. * * @return \Illuminate\Support\Collection */ private function collectLocalesStrings(): Collection { // * En dur dans ->keys array. /** @var array */ $locales = config('app.locales', []); $files = File::allFiles(base_path('lang/')); return Collection::make($files)->flatMap(function (SplFileInfo $file) { if ($file->getRelativePath() && ! str_starts_with($file->getRelativePath(), 'vendor')) { return [$file->getRelativePath() => '']; } })->keys()->merge($locales); } /** * Gather all php translations. * * @return array */ private function phpTranslations(string $locale): array { $path = base_path("lang/$locale"); if (! File::exists($path) || ! File::isReadable($path)) { return []; } $files = File::allFiles($path); return Collection::make($files)->flatMap(function ($file) use ($locale) { $translation = $file->getBasename('.php'); return [$translation => trans($translation, [], $locale)]; })->all(); } /** * Gather json translations. * * @return array */ private function jsonTranslations(string $locale): array { $path = base_path("lang/$locale.json"); if (! File::exists($path) || ! File::isReadable($path)) { return []; } return Arr::wrap(json_decode(File::get($path), true)); } }