-
-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not force string return type from ITranslator interface #231
Comments
We are in the same situation as @smuuf |
How about this? interface Translator
{
/** @param mixed ...$parameters */
function translate(string|Stringable $message, ...$parameters): string|Stringable;
} |
Looks good 👍 |
I think it's not even a BC break. |
What is correct solution of this phpstan error? It looks like BC for me... $presenter->flashMessage($presenter->translator->translate('Test'));
|
I rewrite code to |
@ondrejmirtes Could you resolve error in |
@petrparolek I'm not going to read all this. Please open an issue in phpstan-nette with a full description of what you are trying to fix. |
Is your feature request related to a problem? Please describe.
Due to the change in
\Nette\Localization\ITranslator
, wherestring
return type was added in version3
, we're no longer able to have easily translatable pieces of HTML in our Latte templates/Nette forms.Explain your intentions.
Previously, in Nette 2.4, we were able to return instances of
\Nette\Utils\Html
(class which implements the\Nette\Utils\IHtmlString
interface) from our own implementation of\Nette\Localization\ITranslator
. This is no longer possible as of version3
, due to the addedstring
return type, which disallows the previously working logic.This, unfortunately, makes the transition to Nette 3 very painful.
A "translatable piece of HTML" can, for example, be some piece of Markdown rendered into HTML. Markdown-markuped text is identified by our translator, processed, and then returned as a
\Nette\Utils\Html
object (instead of juststring
) - which is then not escaped when placed inside a Latte template.Minimal example from our current
2.4
translator:The translator can simply return a
Html
object and then we don't have to deal with manually escaping/unescaping everything in Latte templates. This is because the\Latte\Runtime\Filters\escapeHtmlText
itself supports this:https://github.com/nette/latte/blob/bda925b140fe1fbc42b14c3f4f6343b808caf795/src/Latte/Runtime/Filters.php#L45-L50
Now, because of the change of the
ITranslator
interface forcing thestring
return type, this mechanism can no longer be used.Yes, in PHP 8 with union types it might be ok to use union
string|IHtmlString|HtmlStringable
return type for thetranslate
method, but since that's not what can be done now, I'm proposing for the removal of the currently needlessly limitingstring
return type.The text was updated successfully, but these errors were encountered: