2023-03-14 14:27:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
use Symfony\Component\Finder\SplFileInfo;
|
2025-02-18 13:45:23 +01:00
|
|
|
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
|
2023-03-14 14:27:51 +01:00
|
|
|
|
|
|
|
|
class TranslationServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Bootstrap the application services.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-04-13 12:14:06 +02:00
|
|
|
public function boot(): void
|
2023-03-14 14:27:51 +01:00
|
|
|
{
|
|
|
|
|
foreach ($this->collectLocalesStrings() as $locale) {
|
2025-02-18 13:45:23 +01:00
|
|
|
Cache::remember(sprintf('translations_%s', $locale), 172800, function () use ($locale) {
|
2024-01-17 12:23:48 +01:00
|
|
|
$phpTransFallback = $this->phpTranslations(config('app.fallback_locale'));
|
|
|
|
|
$phpTransLocale = $this->phpTranslations($locale);
|
2025-03-05 12:05:04 +01:00
|
|
|
return ToolboxHelper::arrayMergeRecursiveDistinct(
|
|
|
|
|
$phpTransFallback,
|
|
|
|
|
$phpTransLocale,
|
|
|
|
|
);
|
2023-03-14 14:27:51 +01:00
|
|
|
});
|
2024-02-12 11:07:43 +01:00
|
|
|
} //end foreach
|
2023-03-14 14:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-12 11:07:43 +01:00
|
|
|
* Gather provided langs checking from files.
|
2023-03-14 14:27:51 +01:00
|
|
|
*
|
2023-04-13 12:14:06 +02:00
|
|
|
* @return \Illuminate\Support\Collection<string, string>
|
2023-03-14 14:27:51 +01:00
|
|
|
*/
|
2024-02-12 11:07:43 +01:00
|
|
|
private function collectLocalesStrings(): \Illuminate\Support\Collection
|
2023-03-14 14:27:51 +01:00
|
|
|
{
|
2024-01-19 18:15:26 +01:00
|
|
|
// @phpstan-ignore-next-line
|
2025-02-18 13:45:23 +01:00
|
|
|
return collect(File::allFiles(base_path('lang/')))->flatMap(function (SplFileInfo $file) {
|
2023-03-14 14:27:51 +01:00
|
|
|
if ($file->getRelativePath() and \strpos($file->getRelativePath(), 'vendor') !== 0) {
|
|
|
|
|
return [$file->getRelativePath() => ''];
|
|
|
|
|
}
|
2024-01-17 12:23:48 +01:00
|
|
|
})->keys()->merge(config('app.locales', []));
|
2023-03-14 14:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-12 11:07:43 +01:00
|
|
|
* Gather all php translations.
|
2023-03-14 14:27:51 +01:00
|
|
|
*
|
|
|
|
|
* @param string $locale
|
2023-04-13 12:14:06 +02:00
|
|
|
* @return array<string, string>
|
2023-03-14 14:27:51 +01:00
|
|
|
*/
|
|
|
|
|
private function phpTranslations(string $locale): array
|
|
|
|
|
{
|
2025-02-18 13:45:23 +01:00
|
|
|
$path = base_path("lang/$locale");
|
2023-03-14 14:27:51 +01:00
|
|
|
|
2024-01-17 12:23:48 +01:00
|
|
|
if (!File::exists($path) or !File::isReadable($path)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 14:27:51 +01:00
|
|
|
return collect(File::allFiles($path))->flatMap(function ($file) use ($locale) {
|
|
|
|
|
$translation = $file->getBasename('.php');
|
|
|
|
|
return [$translation => trans($translation, [], $locale)];
|
|
|
|
|
})->all();
|
|
|
|
|
}
|
|
|
|
|
}
|