move into sonardesk

This commit is contained in:
Mark Gregory
2026-10-12 14:50:13 +01:00
parent 5f0d2ceb72
commit 38b4a7406f
879 changed files with 24021 additions and 1 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace Modules\Localization\Concerns;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Modules\Localization\Models\Locale;
use Modules\Localization\Models\LocalizedContent;
trait HasTranslations
{
public function translations(): MorphMany
{
return $this->morphMany(LocalizedContent::class, 'translatable');
}
public function translated(string $field, ?string $locale = null, mixed $fallback = null): mixed
{
$locale ??= app()->getLocale();
$translation = $this->translations->first(fn (LocalizedContent $item): bool => $item->field === $field && $item->locale?->code === $locale);
if ($translation && filled($translation->value)) {
return $translation->value;
}
return $fallback ?? $this->getAttribute($field);
}
public function setTranslation(string $field, string $localeCode, ?string $value, ?int $userId = null): LocalizedContent
{
$locale = Locale::query()->where('code', $localeCode)->firstOrFail();
return $this->translations()->updateOrCreate(
['locale_id' => $locale->id, 'field' => $field],
['value' => $value, 'updated_by' => $userId]
);
}
}