This commit is contained in:
parent
6e9a2438c5
commit
b381624f12
@ -3,11 +3,11 @@
|
||||
namespace Modules\Language\Services;
|
||||
|
||||
use App\Services\MediaService;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Lang;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Lang;
|
||||
use Modules\Language\Models\Language;
|
||||
use Modules\Language\Models\LanguageProperty;
|
||||
use Exception;
|
||||
@ -96,8 +96,48 @@ class LanguageService extends MediaService
|
||||
Cache::forget($this->getCacheKey($locale));
|
||||
}
|
||||
|
||||
protected function getDefaultLocale(): string
|
||||
{
|
||||
return config('app.fallback_locale', config('app.locale', 'en'));
|
||||
}
|
||||
|
||||
protected function buildTranslationsFromFiles(string $locale): array
|
||||
{
|
||||
$langPath = lang_path($locale);
|
||||
|
||||
if (! is_dir($langPath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$translations = [];
|
||||
|
||||
foreach (File::allFiles($langPath) as $file) {
|
||||
if ($file->getExtension() !== 'php') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$group = $file->getFilenameWithoutExtension();
|
||||
$values = require $file->getPathname();
|
||||
|
||||
foreach (Arr::dot($values) as $key => $value) {
|
||||
$translations[$group . '.' . $key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $translations;
|
||||
}
|
||||
|
||||
function setLanguageProperties(string $locale): void
|
||||
{
|
||||
$defaultLocale = $this->getDefaultLocale();
|
||||
|
||||
if ($locale === $defaultLocale) {
|
||||
$lines = $this->buildTranslationsFromFiles($locale);
|
||||
Lang::addLines($lines, $locale);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$cacheKey = $this->getCacheKey($locale);
|
||||
|
||||
$cached = Cache::rememberForever($cacheKey, function () use ($locale) {
|
||||
@ -105,6 +145,10 @@ class LanguageService extends MediaService
|
||||
->with('properties')
|
||||
->first();
|
||||
|
||||
if (! $language) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$groups = [];
|
||||
foreach ($language->properties as $property) {
|
||||
$groups[$property->group] = array_merge($groups[$property->group] ?? [], $property->properties);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user