简体   繁体   中英

Laravel 5.4 translations with constant values

I'm facing a problem with translations. I'm pretty new with them, as I never tried to add translations into a project. I've this model for notifications in Laravel 5.4:

// /App/Notification.php
class Notification extends Model
{
    const NO_STATUS = 0;
    const SENT      = 100;
    const ACCEPTED  = 200;
    const ERROR     = 300;

    public static $statuses = [
        self::NO_STATUS => 'No status',
        self::SENT      => 'Sent',
        self::ACCEPTED  => 'Accepted',
        self::ERROR     => 'Error',
    ];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'from', 'to', 'status',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        //
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'from' => 'integer',
        'status' => 'integer',
    ];

    public function isSent()
    {
        return $this->status == self::SENT;
    }

    public function isAccepted()
    {
        return $this->status == self::ACCEPTED;
    }

    public function isError()
    {
        return $this->status == self::ERROR;
    }

    public static function statuses()
    {
        return self::statuses();
    }

    public function getStatusAttribute($value)
    {
       return self::$statuses[$value];
    }
}

And I have this translation file, which I'm intended to use it to convert numeric values stored in the database as status into readable text for users when they see the notifications list in a view:

// /resources/lang/en/constants.php
return [

    /*
     * Notifications constants
     */
    'notification' => [
        'no_status' => 'No status',
        'sent'      => 'Sent',
        'accepted'  => 'Accepted',
        'error'     => 'Error',
    ],
];

I have the getStatusAttribute accessor function in the model to retrieve its status code as text, but I can't set a value to a static attribute (obviusly), like so:

public static $statuses = [
    self::NO_STATUS => trans('constants.notification.no_status'),
    self::SENT      => trans('constants.notification.sent'),
    self::ACCEPTED  => trans('constants.notification.accepted'),
    self::ERROR     => trans('constants.notification.error'),
];

Any suggestion about how can I achieve this?

I want that the returned property in the object becomes already a translated text, instead to translate it in the view, if possible.

Thanks for your time.

You should return a function, not a constant.

Class Status
{
    public static function getStatusArray = [
        self::NO_STATUS => __('constants.notification.no_status'),
        self::SENT      => __('constants.notification.sent'),
        self::ACCEPTED  => __('constants.notification.accepted'),
        self::ERROR     => __('constants.notification.error'),
    ];
} 

After that you can call a function like so:

array_get(App\Class::getStatusArray(), 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM