init project
This commit is contained in:
21
app/Models/Traits/HasLocalizedName.php
Normal file
21
app/Models/Traits/HasLocalizedName.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Traits;
|
||||
|
||||
trait HasLocalizedName
|
||||
{
|
||||
//
|
||||
public function getNameAttribute()
|
||||
{
|
||||
$locale = app()->getLocale(); // e.g. 'en' or 'fr'
|
||||
// Default fallback language
|
||||
$column = 'name_en';
|
||||
|
||||
if (in_array($locale, ['en', 'kh'])) {
|
||||
$column = "name_{$locale}";
|
||||
}
|
||||
|
||||
// Return localized name if exists, else fallback
|
||||
return $this->{$column} ?? $this->name_en;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user