collectLocalesStrings() as $locale) { // * Suported locales Cache::rememberForever(sprintf('translations.%s', $locale), fn (): array => [ 'php' => $this->phpTranslations($locale), 'json' => $this->jsonTranslations($locale), ]); } } /** * Gather provided langs checking from files * * @return Collection */ private function collectLocalesStrings(): Collection { return collect(File::allFiles(resource_path('lang/')))->flatMap(function (SplFileInfo $file) { if ($file->getRelativePath() && ! str_starts_with($file->getRelativePath(), 'vendor')) { return [$file->getRelativePath() => '']; } })->keys(); } /** * Gather all php translations * * @return array */ private function phpTranslations(string $locale): array { $path = resource_path("lang/$locale"); return collect(File::allFiles($path))->flatMap(function ($file) use ($locale): array { $translation = $file->getBasename('.php'); return [$translation => trans($translation, [], $locale)]; })->all(); } /** * Gather json translations * * @return array */ private function jsonTranslations(string $locale): array { $path = resource_path("lang/$locale.json"); if (is_string($path) && is_readable($path)) { return json_decode(file_get_contents($path), true) ?? []; } return []; } }