<?php

namespace App\Traits;

use App\Models\Translation;

trait HasTranslations
{
    public function getTranslated(string $keyField, ?string $lang = null): ?string
    {
        $lang = $lang ?? request()->attributes->get('locale', 'fr');
        $key = $this->{$keyField};

        if (!$key) {
            return null;
        }

        return Translation::getTranslation($key, $lang);
    }
}
