Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upDo not force string return type from ITranslator interface #231
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is your feature request related to a problem? Please describe.
Due to the change in
\Nette\Localization\ITranslator, wherestringreturn 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\IHtmlStringinterface) from our own implementation of\Nette\Localization\ITranslator. This is no longer possible as of version3, due to the addedstringreturn 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\Htmlobject (instead of juststring) - which is then not escaped when placed inside a Latte template.Minimal example from our current
2.4translator:The translator can simply return a
Htmlobject and then we don't have to deal with manually escaping/unescaping everything in Latte templates. This is because the\Latte\Runtime\Filters\escapeHtmlTextitself supports this:https://github.com/nette/latte/blob/bda925b140fe1fbc42b14c3f4f6343b808caf795/src/Latte/Runtime/Filters.php#L45-L50
Now, because of the change of the
ITranslatorinterface forcing thestringreturn type, this mechanism can no longer be used.Yes, in PHP 8 with union types it might be ok to use union
string|IHtmlString|HtmlStringablereturn type for thetranslatemethod, but since that's not what can be done now, I'm proposing for the removal of the currently needlessly limitingstringreturn type.