File: /home/u756937133/domains/swingersnest.com/public_html/app/Traits/Translatable.php
<?php
namespace App\Traits;
use App\Models\Translation;
use Illuminate\Support\Facades\App;
trait Translatable
{
/**
* Get translations relationship
*/
public function translations()
{
return $this->morphMany(Translation::class, 'translatable');
}
/**
* Get a translated attribute value
*/
public function getTranslatedAttribute(string $field, ?string $locale = null): ?string
{
$locale = $locale ?? App::getLocale();
// If locale is 'en' (default), return original value from attributes
if ($locale === 'en') {
return $this->attributes[$field] ?? null;
}
// Get translation from database
$translation = $this->translations()
->where('locale', $locale)
->where('field', $field)
->first();
// Return translation if exists and has value
if ($translation && !empty($translation->value)) {
return $translation->value;
}
// Fallback to English value from attributes
return $this->attributes[$field] ?? null;
}
/**
* Set a translation for a specific field and locale
*/
public function setTranslation(string $field, string $locale, ?string $value): void
{
if ($locale === 'en') {
// For English, update the main model attribute
$this->attributes[$field] = $value;
$this->save();
return;
}
// For other languages, store in translations table
$this->translations()->updateOrCreate(
[
'locale' => $locale,
'field' => $field,
],
[
'value' => $value,
]
);
}
/**
* Set multiple translations at once
*/
public function setTranslations(array $translations): void
{
foreach ($translations as $locale => $fields) {
foreach ($fields as $field => $value) {
$this->setTranslation($field, $locale, $value);
}
}
}
/**
* Get all translations for a specific locale
*/
public function getTranslationsForLocale(string $locale): array
{
$translations = [];
// Get translatable fields from model
$translatableFields = $this->getTranslatableFields();
foreach ($translatableFields as $field) {
$translations[$field] = $this->getTranslatedAttribute($field, $locale);
}
return $translations;
}
/**
* Get translatable fields - override in model if needed
*/
protected function getTranslatableFields(): array
{
return $this->translatableFields ?? [];
}
/**
* Magic method to get translated attributes
* Only intercepts translatable fields when not in English locale
*/
public function __get($key)
{
$translatableFields = $this->getTranslatableFields();
// Only intercept translatable fields when not in English
if (!empty($translatableFields) && in_array($key, $translatableFields) && App::getLocale() !== 'en') {
$translated = $this->getTranslatedAttribute($key);
// Only return translated value if it exists, otherwise fall through to parent
if ($translated !== null && $translated !== '') {
return $translated;
}
}
// Default Laravel behavior for non-translatable fields or English locale
return parent::__get($key);
}
/**
* Check if translation exists for a field and locale
*/
public function hasTranslation(string $field, string $locale): bool
{
if ($locale === 'en') {
return isset($this->attributes[$field]) && !empty($this->attributes[$field]);
}
return $this->translations()
->where('locale', $locale)
->where('field', $field)
->whereNotNull('value')
->exists();
}
/**
* Delete all translations for this model
*/
public function deleteTranslations(): void
{
$this->translations()->delete();
}
/**
* Delete translation for a specific locale
*/
public function deleteTranslationsForLocale(string $locale): void
{
$this->translations()->where('locale', $locale)->delete();
}
}