ldc-legaulois/app/Providers/TranslationServiceProvider.php

94 lines
2.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Providers;
2024-01-30 10:43:25 +01:00
use App\Lib\Helpers\ToolboxHelper;
2026-09-25 16:25:31 +02:00
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
2026-09-25 16:25:31 +02:00
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
2024-01-10 14:49:46 +01:00
use Symfony\Component\Finder\SplFileInfo;
class TranslationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
2024-01-10 14:49:46 +01:00
public function boot(): void
{
2022-02-10 16:14:58 +01:00
foreach ($this->collectLocalesStrings() as $locale) {
2024-02-20 10:38:38 +01:00
// * Suported locales.
Cache::remember(sprintf('translations.%s', $locale), 86400, function () use ($locale) {
2024-01-30 10:43:25 +01:00
$phpTransFallback = $this->phpTranslations(config('app.fallback_locale'));
$phpTransLocale = $this->phpTranslations($locale);
$jsonTransFallback = $this->jsonTranslations(config('app.fallback_locale'));
$jsonTransLocale = $this->jsonTranslations($locale);
$translations = [
2024-02-20 10:38:38 +01:00
'php' => ToolboxHelper::arrayMergeRecursiveDistinct(
2024-01-30 10:43:25 +01:00
$phpTransFallback,
2024-02-20 10:38:38 +01:00
$phpTransLocale,
2024-01-30 10:43:25 +01:00
),
'json' => ToolboxHelper::arrayMergeRecursiveDistinct(
$jsonTransFallback,
2024-02-20 10:38:38 +01:00
$jsonTransLocale,
2024-01-30 10:43:25 +01:00
),
];
2026-09-25 16:25:31 +02:00
return $translations;
});
2026-09-25 16:25:31 +02:00
} // end foreach
}
2024-01-10 14:49:46 +01:00
/**
2024-02-20 10:38:38 +01:00
* Gather provided langs checking from files.
2024-01-10 14:49:46 +01:00
*
* @return \Illuminate\Support\Collection<string, string>
*/
2026-09-25 16:25:31 +02:00
private function collectLocalesStrings(): Collection
{
2024-01-30 10:43:25 +01:00
// * En dur dans ->keys array<int,string>.
// @phpstan-ignore-next-line
2024-01-10 14:49:46 +01:00
return collect(File::allFiles(resource_path('lang/')))->flatMap(function (SplFileInfo $file) {
if ($file->getRelativePath() and \strpos($file->getRelativePath(), 'vendor') !== 0) {
return [$file->getRelativePath() => ''];
}
2024-01-30 10:43:25 +01:00
})->keys()->merge(config('app.locales', []));
}
2024-01-10 14:49:46 +01:00
/**
2024-02-20 10:38:38 +01:00
* Gather all php translations.
2024-01-10 14:49:46 +01:00
*
* @return array<string, string>
*/
private function phpTranslations(string $locale): array
{
$path = resource_path("lang/$locale");
2026-09-25 16:25:31 +02:00
if (! File::exists($path) or ! File::isReadable($path)) {
2024-01-30 10:43:25 +01:00
return [];
}
return collect(File::allFiles($path))->flatMap(function ($file) use ($locale) {
2024-01-10 14:49:46 +01:00
$translation = $file->getBasename('.php');
2026-09-25 16:25:31 +02:00
2024-01-10 14:49:46 +01:00
return [$translation => trans($translation, [], $locale)];
})->all();
}
2024-01-10 14:49:46 +01:00
/**
2024-02-20 10:38:38 +01:00
* Gather json translations.
2024-01-10 14:49:46 +01:00
*
* @return array<string, string>
*/
private function jsonTranslations(string $locale): array
{
$path = resource_path("lang/$locale.json");
2026-09-25 16:25:31 +02:00
if (! File::exists($path) or ! File::isReadable($path)) {
2024-01-30 10:43:25 +01:00
return [];
}
2024-01-30 10:43:25 +01:00
return json_decode(File::get($path), true) ?? [];
}
}