lms/Modules/Language/app/Services/LanguageService.php
Ahmed Darrazi b381624f12
All checks were successful
Build & Push Docker Image / docker (push) Successful in 1m49s
bugfixes
2025-12-17 14:15:13 +01:00

187 lines
4.7 KiB
PHP

<?php
namespace Modules\Language\Services;
use App\Services\MediaService;
use Illuminate\Http\Request;
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;
class LanguageService extends MediaService
{
public string $cacheKey;
public function __construct(Request $request)
{
$this->cacheKey = 'language_properties';
}
function storeLanguage($data)
{
$langPath = base_path('lang');
$langDir = $langPath . "/" . $data['code'];
$appLangPath = storage_path('app/lang/default');
$groupNames = ['auth','button','common','dashboard','frontend','input','settings'];
$groups = [];
foreach ($groupNames as $groupName) {
$repoGroupPath1 = base_path("lang/{$data['code']}/groups/{$groupName}.php");
$repoGroupPath2 = base_path("lang/{$data['code']}/{$groupName}.php");
if (file_exists($repoGroupPath1)) {
$groups[$groupName] = require $repoGroupPath1;
continue;
}
if (file_exists($repoGroupPath2)) {
$file = require $repoGroupPath2;
$groups[$groupName] = [
[
'name' => ucfirst(str_replace('_', ' ', $groupName)),
'slug' => $groupName,
'properties' => $file,
],
];
continue;
}
$groups[$groupName] = require storage_path("app/lang/groups/{$groupName}.php");
}
$languages = Language::create($data);
foreach ($groups as $key => $group) {
foreach ($group as $value) {
LanguageProperty::create([
...$value,
'group' => $key,
'language_id' => $languages->id,
]);
}
}
$alreadyExists = is_dir($langDir);
if (! $alreadyExists) {
File::makeDirectory($langDir, 0777, true, true);
File::copyDirectory($appLangPath, $langDir);
}
}
function updateLanguage($id, $data)
{
Language::find($id)->update($data);
}
function defaultLanguage($id)
{
Language::where('is_default', true)->update(['is_default' => false]);
Language::where('id', $id)->update(['is_default' => true]);
}
protected function getCacheKey(string $locale): string
{
return "{$this->cacheKey}_{$locale}";
}
public function forgetLanguageCache(string $locale): void
{
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) {
$language = Language::where('code', $locale)
->with('properties')
->first();
if (! $language) {
return [];
}
$groups = [];
foreach ($language->properties as $property) {
$groups[$property->group] = array_merge($groups[$property->group] ?? [], $property->properties);
}
$translations = [];
foreach ($groups as $group => $properties) {
foreach ($properties as $key => $value) {
$translations[$group . '.' . $key] = $value;
}
}
return $translations;
});
Lang::addLines($cached, $locale);
}
function deleteLanguage($id)
{
$lang = Language::find($id);
$langPath = base_path('lang');
$langDir = $langPath . "/" . $lang->code;
if ($lang->is_default) {
throw new Exception("Default language can't be deleted");
}
if (is_dir($langDir)) {
File::deleteDirectory($langDir);
}
$lang->delete();
}
}