Revision: Wed, 26 Feb 2020 12:07:29 GMT

Internalization - Say Trait

Is it possible to add translation abilities to any applicatio object using specialized trait Spiral\Translator\Traits\TranslatorTrait.

Usage

We can add the translator trait to the controller:

namespace App\Controller;

use Spiral\Translator\Traits\TranslatorTrait;

class HomeController
{
    use TranslatorTrait;

    public function index()
    {
        return $this->say("hello world!");
    }
}

Run 'i18n:index' to index the string invocation.

Class messages

In cases where message is defined by logic and can not be indexed use constants and/or properties to declare class messages, every string wrapped with [[]] will be automatically indexed.

class HomeController 
{
    use TranslatorTrait;

    protected const MESSAGES = [
        'error'   => '[[An error]]',
        'success' => '[[Success]]'
    ];

    public function index()
    {
        echo $this->say(self::MESSAGES['error']);
    }
}
Edit this page