Interface for translator and translatable messages
Install with Composer
composer require orisai/translation-contracts
Translator
is an interface for translating message identifiers
use Orisai\TranslationContracts\Translator;
assert($translator instanceof Translator);
$translator->translate('message.id'); // string
$translator->translate('message.id', ['parameter' => 'value'], 'en');
TranslatableMessage
is an object that may be translated later, via Translator->translate()
use Orisai\TranslationContracts\TranslatableMessage;
$message = new TranslatableMessage('message.id');
$message = new TranslatableMessage('message.id', ['parameter' => 'value'], 'en');
$translator->translateMessage($message); // string
$translator->translateMessage($message, /* overrides locale from message */ 'en'); // string
As an alternative to TranslatableMessage
we may implement Translatable
interface
$translator->translateMessage(new Example());
Shortcuts for calling Translator->translate()
and Translator->translateMessage()
use Orisai\TranslationContracts\TranslatableMessage;
use function Orisai\TranslationContracts\t;
use function Orisai\TranslationContracts\tm;
t('message.id'); // string
t('message.id', ['parameter' => 'value'], 'en'); // string
tm(new TranslatableMessage('message.id')); // string
tm(new TranslatableMessage('message.id'), 'en'); // string
Shortcuts are set up via TranslatorHolder
use Orisai\TranslationContracts\SimpleTranslatorGetter;
use Orisai\TranslationContracts\TranslatorHolder;
TranslatorHolder::setTranslatorGetter(new SimpleTranslatorGetter($translator));
SimpleTranslatorGetter
may be replaced by lazy TranslatorGetter
implementation