Answer the question
In order to leave comments, you need to log in
Service with translated parameter dictionary. Something is wrong?
There is a service (let's say StaticList) where all sorts of parameter lists are stored, for example:
public static function getCommissions()
{
$list = [
[
'key' => self::COMMISSION_NO_PROXY,
'name' => self::$translator->trans('_No proxy phone, no commission_'),
'projects' => ['all']
],
[
'key' => self::COMMISSION_ANSWERS,
'name' => self::$translator->trans('_Commission_'),
'projects' => ['1']
],
];
return $list;
}
Answer the question
In order to leave comments, you need to log in
The existing approach is quite appropriate, because accessing a service from a model is bad practice and displaying localized text from a model is not the job of the model, although the name field can be changed to type json in the DBMS.
In this case, I recommend renaming the StaticList service to ComissionManager and not making the getCommissions and getTextForKey methods static, because since the self::$translator construct still requires initial manual initialization of the object of the Translation class, let the constructor itself deal with it implicitly. Further, it is enough to implement the ComissionManager service as a dependency in the desired controller method and call $comissionManager->getTextForKey($model->commission_key), substituting the model explicitly, but $comissionManager->getText($model) can be shorter and more flexible.
Of course, you can go further and create an interface ComissionInterface with a getKey method and add it to models that will work with commissions. Then the implementation of the getText method will look like this:
public getText(ComissionInterface $model)
{
$key = $model->getKey();
$text = ...
return $text;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question