|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | | -namespace Cone\Root\Models; |
| 5 | +namespace Cone\Root\Traits; |
6 | 6 |
|
7 | | -use Cone\Root\Database\Factories\TranslationFactory; |
8 | | -use Cone\Root\Interfaces\Models\Translation as Contract; |
9 | | -use Cone\Root\Traits\InteractsWithProxy; |
10 | | -use Illuminate\Database\Eloquent\Casts\AsArrayObject; |
11 | | -use Illuminate\Database\Eloquent\Factories\HasFactory; |
12 | | -use Illuminate\Database\Eloquent\Model; |
13 | | -use Illuminate\Database\Eloquent\Relations\MorphTo; |
| 7 | +use Cone\Root\Models\Translation; |
| 8 | +use Illuminate\Database\Eloquent\Relations\MorphMany; |
| 9 | +use Illuminate\Support\Facades\App; |
14 | 10 |
|
15 | | -class Translation extends Model implements Contract |
| 11 | +trait Translatable |
16 | 12 | { |
17 | | - use HasFactory; |
18 | | - use InteractsWithProxy; |
19 | | - |
20 | | - /** |
21 | | - * The attributes that should be cast to native types. |
22 | | - * |
23 | | - * @var array<string, string> |
24 | | - */ |
25 | | - protected $casts = [ |
26 | | - 'values' => AsArrayObject::class, |
27 | | - ]; |
28 | | - |
29 | | - /** |
30 | | - * The attributes that are mass assignable. |
31 | | - * |
32 | | - * @var list<string> |
33 | | - */ |
34 | | - protected $fillable = [ |
35 | | - 'locale', |
36 | | - 'values', |
37 | | - ]; |
38 | | - |
39 | 13 | /** |
40 | | - * The table associated with the model. |
41 | | - * |
42 | | - * @var string |
| 14 | + * Get the translations for the model. |
43 | 15 | */ |
44 | | - protected $table = 'root_translations'; |
45 | | - |
46 | | - /** |
47 | | - * The translatable model's locale. |
48 | | - */ |
49 | | - protected static string $translatableLocale = 'en'; |
50 | | - |
51 | | - /** |
52 | | - * Get the proxied interface. |
53 | | - */ |
54 | | - public static function getProxiedInterface(): string |
| 16 | + public function translations(): MorphMany |
55 | 17 | { |
56 | | - return Contract::class; |
| 18 | + return $this->morphMany(Translation::getProxiedClass(), 'translatable'); |
57 | 19 | } |
58 | 20 |
|
59 | 21 | /** |
60 | | - * Create a new factory instance for the model. |
| 22 | + * Translate the value of the given key. |
61 | 23 | */ |
62 | | - protected static function newFactory(): TranslationFactory |
| 24 | + public function translate(string $key, ?string $locale = null): mixed |
63 | 25 | { |
64 | | - return TranslationFactory::new(); |
65 | | - } |
| 26 | + $locale ??= App::getLocale(); |
66 | 27 |
|
67 | | - /** |
68 | | - * Set the translatable model's locale. |
69 | | - */ |
70 | | - public static function setTranslatableLocale(string $locale): void |
71 | | - { |
72 | | - static::$translatableLocale = $locale; |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * Get the translatable model's locale. |
77 | | - */ |
78 | | - public static function getTranslatableLocale(): string |
79 | | - { |
80 | | - return static::$translatableLocale; |
81 | | - } |
82 | | - |
83 | | - /** |
84 | | - * Get the translatable model for the translation. |
85 | | - */ |
86 | | - public function translatable(): MorphTo |
87 | | - { |
88 | | - return $this->morphTo(); |
| 28 | + return match ($locale) { |
| 29 | + (Translation::proxy())::getTranslatableLocale() => $this->getAttribute($key), |
| 30 | + default => $this->translations->firstWhere('locale', $locale)?->values[$key] ?? $this->getAttribute($key), |
| 31 | + }; |
89 | 32 | } |
90 | 33 | } |
0 commit comments