62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class TranslationServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Bootstrap the application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$translations = collect();
|
|
|
|
foreach ($this->collectLocalesStrings() as $locale) {
|
|
// suported locales
|
|
Cache::rememberForever(sprintf('translations.%s', $locale), function () use ($locale) {
|
|
$translations = [
|
|
'php' => $this->phpTranslations($locale),
|
|
'json' => $this->jsonTranslations($locale),
|
|
];
|
|
return $translations;
|
|
});
|
|
}
|
|
}
|
|
|
|
private function collectLocalesStrings()
|
|
{
|
|
return collect(File::allFiles(resource_path('lang/')))->flatMap(function ($file) {
|
|
if ($file->getRelativePath()) {
|
|
return [$file->getRelativePath() => ''];
|
|
}
|
|
})->keys();
|
|
}
|
|
|
|
private function phpTranslations($locale)
|
|
{
|
|
$path = resource_path("lang/$locale");
|
|
|
|
return collect(File::allFiles($path))->flatMap(function ($file) use ($locale) {
|
|
$key = ($translation = $file->getBasename('.php'));
|
|
|
|
return [$key => trans($translation, [], $locale)];
|
|
});
|
|
}
|
|
|
|
private function jsonTranslations($locale)
|
|
{
|
|
$path = resource_path("lang/$locale.json");
|
|
|
|
if (is_string($path) && is_readable($path)) {
|
|
return json_decode(file_get_contents($path), true);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
}
|