Follow-up to #2570285: Make sure TranslatableMarkup accepts string values only
Follow-up to #2557113: Make t() return a TranslationWrapper object to remove reliance on a static, unpredictable safe list
Problem/Motivation
In #2557113: Make t() return a TranslationWrapper object to remove reliance on a static, unpredictable safe list we left the input value problem unaddressed - $this->string() can still be anything.
Ideally we would use an exception/assert/trigger_error to indicate this is wrong but the docs of t() still say:
"You should never use t() to translate variables, such as calling t($text) unless the text that the variable holds has been passed through t() elsewhere (e.g., $text is one of several translated literal strings in an array)."
So we should address that first.
In the mean time we can at least prevent potential new bugs by casting the input to string, as we've never properly tested passing in toStringable objects.
Proposed solution
Cast $this->string to string, like we do in other value objects.
API changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 2579121-4.patch | 1.92 KB | stefan.r |
| #2 | 2579121-1.patch | 903 bytes | stefan.r |
Comments
Comment #2
stefan.r commentedComment #3
dawehnerCan we have some test coverage, you know, this never can be bad.
Comment #4
stefan.r commentedComment #5
dawehnerHere is a different idea. What about checking whether its an object. In case it is, bypass the translation manager and set
$this->translatedMarkupdirectly?Comment #6
stefan.r commenteddawehner what do we set it to though? The problem is our docs say we can still do t(t('string')):
"You should never use t() to translate variables, such as calling t($text) unless the text that the variable holds has been passed through t() elsewhere (e.g., $text is one of several translated literal strings in an array)."
Comment #7
stefan.r commentedOK so that piece of documentation actually means something far easier:
<ksenzee> isn't that just saying that t($var) is Bad, unless you have t('banana') elsewhere, and $var == 'banana'?So maybe we can just RTBC #2570285: Make sure TranslatableMarkup accepts string values only instead
Comment #8
stefan.r commentedas to #5, it's a change in behavior if we make it so that t(t(t(t('string')))) == t('string').. that'd need to happen pre-RC :( If we're changing behavior I'd rather just get #2570285: Make sure TranslatableMarkup accepts string values only in.
Comment #9
alexpottDiscussed with @berdir - we decided to go with #2570285: Make sure TranslatableMarkup accepts string values only - passing non strings into TranslatableMarkup should be explicit forbidden and putting TranslatableMarkup into TranslatableMarkup really makes no sense.