Skip to content

[Translation] Deprecate TranslatableMessage::__toString #61109

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

Merged
merged 1 commit into from
Jul 17, 2025

Conversation

VincentLanglet
Copy link
Contributor

Q A
Branch? 7.4
Bug fix? no
New feature? no
Deprecations? yes
Issues Fix #...
License MIT

Resolve the following discussion #60935 (comment)
cc @stof @xabbuh

@VincentLanglet
Copy link
Contributor Author

I tried to deprecate TranslatableMessage::__toString but then it creates a deprecation when writing

if label is empty

because of the twig implémentation

public static function testEmpty($value): bool
    {
        if ($value instanceof \Countable) {
            return 0 === \count($value);
        }

        if ($value instanceof \Traversable) {
            return !iterator_count($value);
        }

        if ($value instanceof \Stringable) {
            return '' === (string) $value;
        }

        return '' === $value || false === $value || null === $value || [] === $value;
    }

Not sure how to do then @stof @xabbuh ?

Also it means that removing the toString method in 8.0 will imply that such thing as

new TranslatableMessage('');
new StaticMessage('');

won't be considered as empty anymore.

@VincentLanglet
Copy link
Contributor Author

I extracted the first fix in #61111

@OskarStark OskarStark changed the title [Translation] Deprecate TranslatableMessage::__toString [Translation] Deprecate TranslatableMessage::__toString Jul 13, 2025
@stof
Copy link
Member

stof commented Jul 15, 2025

If the form theme relies on checking whether a TranslatableMessage is empty before translating it, it is broken, as it checks the emptyness of the wrong string.
A TranslatableMessage could have a non-empty key producing an empty message.

@VincentLanglet
Copy link
Contributor Author

If the form theme relies on checking whether a TranslatableMessage is empty before translating it, it is broken, as it checks the emptyness of the wrong string. A TranslatableMessage could have a non-empty key producing an empty message.

If it's broken then how would you "fix" it @stof ?

I'm not sure we could call this broken since it's the same with "string key".
You could decide to have a non-empty string key producing an empty message too ; and the check will be done before translating. So the check seems consistent.

My main issue is currently the fact that

  • If I have a toString in TranslatableMessage and not StaticMessage, the behavior will be different.
  • If I trigger a deprecation in TranslatableMessage::__toString, it will be trigger in twig.

Should I just add @deprecated without a trigger_deprecation call ?

@nicolas-grekas
Copy link
Member

The fix might be to NOT wrap empty strings into TranslatableMessage. Is that possible?

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Jul 15, 2025

The fix might be to NOT wrap empty strings into TranslatableMessage. Is that possible?

We can "forbidden" this with

  • a @param non-empty-string phpdoc
  • An InvalidArgument if a empty string is given on Symfony 8
  • A deprecation on Symfony 7

But that does not solve the fact that a deprecation in the __toString method will be triggered by twig/twig when doing the is empty test: '' === (string) $value because of the current implementation #61109 (comment)

For this, I dunno what to do @nicolas-grekas :/

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 15, 2025

Can't we replace the empty check by a simple if msg one?

@VincentLanglet VincentLanglet force-pushed the toStringTranslatable branch 3 times, most recently from 5571d6e to b217c93 Compare July 15, 2025 13:13
@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Jul 15, 2025

Can't we replace the empty check by a simple if msg one?

Great, it solves everything.

I just need #61111 to be merge into 7.4 now.

UPGRADE-7.4.md Outdated
Translation
-----------

* Deprecate `TranslatableMessage::__toString`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Deprecate `TranslatableMessage::__toString`
* Deprecate `TranslatableMessage::__toString`

@VincentLanglet VincentLanglet force-pushed the toStringTranslatable branch 2 times, most recently from f60e9e2 to 0e67069 Compare July 17, 2025 13:52
@nicolas-grekas
Copy link
Member

Thank you @VincentLanglet.

@nicolas-grekas nicolas-grekas merged commit 2f180d4 into symfony:7.4 Jul 17, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants