You shouldn t and in general it is not possible. According to the Single Responsibility Principle the entity have already their purpose, which is representing data on a database. Moreover the translation is a matter of representation, so it is unlikely that you want to address such a problem in the entity layer (unless you want to provide entities translated in different languages, which totally a different problem and shouldn t even be solved using the translator).
Rethink to your logic and try something different for this. Are you sure that you don t want to do this translation on the view layer? That would be the best thing probably. Otherwise (if your logic really need to have translation at a model level) you could create a wrapper class for entities and a factory to generate this "wrapped entities"; in that factory you could inject the translator service.
namespace AppUtils;
class TranslateObject
{
/** @var string */
protected $transKey;
/** @var array */
protected $placeholders;
public function __construct(string $transKey, array $placeholders = [])
{
$this->transKey = $transKey;
$this->placeholders = self::normalizePlaceholders($placeholders);
}
public static function normalizePlaceholders(array $placeholders): array
{
foreach ($placeholders as $key => &$placeholder) {
if (substr($key, 0, 1) !== % || substr($key, -1, 1) !== % ) {
throw new InvalidArgumentException( The $placeholder attribute must only contain keys in format "%placeholder%". );
}
if ($placeholder instanceof TranslateObject) {
continue;
}
if (is_scalar($placeholder)) {
$placeholder = [ value => $placeholder];
}
if (!isset($placeholder[ value ]) || !is_scalar($placeholder[ value ])) {
throw new InvalidArgumentException( $placeholders[ %placeholder% ][ value ] must be present and a scalar value. );
}
if (!isset($placeholder[ translate ])) {
$placeholder[ translate ] = false;
}
if (!is_bool($placeholder[ translate ])) {
throw new InvalidArgumentException( $placeholders[ %placeholder% ][ translate ] must be a boolean. );
}
}
return $placeholders;
}
public function getTransKey(): string
{
return $this->transKey;
}
public function getPlaceholders(): array
{
return $this->placeholders;
}
}
The helper looks like this and does the work:
namespace AppServices;
use AppUtilsTranslateObject;
use SymfonyContractsTranslationTranslatorInterface;
class TranslateObjectHelper
{
/** @var TranslatorInterface */
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function trans(TranslateObject $translateObject): string
{
$placeholders = $translateObject->getPlaceholders();
foreach ($placeholders as $key => &$placeholder) {
if ($placeholder instanceof TranslateObject) {
$placeholder = $this->trans($placeholder);
}
elseif (true === $placeholder[ translate ]) {
$placeholder = $this->translator->trans($placeholder[ value ]);
}
else {
$placeholder = $placeholder[ value ];
}
}
return $this->translator->trans($translateObject->getTransKey(), $placeholders);
}
}
/**
* Get an identifying name as a TranslateObject (for use with TranslateObjectHelper)
*/
public function getIdentifyingNameTranslateObject(): TranslateObject
{
return new TranslateObject( my.whatever.translation.key , [
%placeholder1% => $this->myEntityProperty1,
%placeholderWithANeedOfTranslation% => [
value => my.whatever.translation.values. . $this->myPropertyWithANeedOfTranslation,
translate => true,
],
%placeholderWithCascadingTranslationNeeds% => $this->getRelatedEntity()->getIdentifyingNameTranslateObject(),
]);
}
namespace AppTwig;
use AppServicesTranslateObjectHelper;
use AppUtilsTranslateObject;
class TranslateObjectExtension extends Twig_Extension
{
/** @var TranslateObjectHelper */
protected $translateObjectHelper;
public function __construct(TranslateObjectHelper $translateObjectHelper)
{
$this->translateObjectHelper = $translateObjectHelper;
}
public function getFilters()
{
return array(
new Twig_SimpleFilter( translateObject , [$this, translateObject ]),
);
}
/**
* sends a TranslateObject through a the translateObjectHelper->trans() method
*/
public function translateObject(TranslateObject $translateObject): string
{
return $this->translateObjectHelper->trans($translateObject);
}
public function getName(): string
{
return translate_object_twig_extension ;
}
}
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this:
User sends login information.
Check if username and password is ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image.
And is there a better way to create ...
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5.
I need ...
I want to create text as a watermark for an image. the water mark should have the following properties
front: Impact
color: white
opacity: 31%
Font style: regular, bold
Bevel and Emboss
size: 30 ...
How does php cast boolean variables?
I was trying to save a boolean value to an array:
$result["Users"]["is_login"] = true;
but when I use debug the is_login value is blank.
and when I do ...