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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-7.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Security
* Deprecate `AbstractListener::__invoke`
* Deprecate `LazyFirewallContext::__invoke()`

Translation
-----------

* Deprecate `TranslatableMessage::__toString`

Validator
---------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ col-sm-2

{% block form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group' ~ ((not compound or force_error|default(false)) and not valid ? ' has-error'))|trim})} %}{{ block('attributes') }}{% endwith %}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
{% set embed_label_classes = parent_label_class|split(' ')|filter(class => class in ['checkbox-inline', 'radio-inline']) %}
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ embed_label_classes|join(' '))|trim}) -%}
{% endif %}
{%- if label is not same as(false) and label is empty -%}
{%- if label is not same as(false) and not label -%}
{%- if label_format is not empty -%}
{%- set label = label_format|replace({
'%name%': name,
Expand Down Expand Up @@ -129,7 +129,7 @@

{% block form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group' ~ ((not compound or force_error|default(false)) and not valid ? ' has-error'))|trim})} %}{{ block('attributes') }}{% endwith %}>
Expand Down Expand Up @@ -199,7 +199,7 @@
{# Help #}

{% block form_help -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-block')|trim}) -%}
<span id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
{%- if translation_domain is same as(false) -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ col-sm-2
{{ block('fieldset_form_row') }}
{%- else -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group row' ~ ((not compound or force_error|default(false)) and not valid ? ' is-invalid'))|trim})} %}{{ block('attributes') }}{% endwith %}>
Expand All @@ -40,7 +40,7 @@ col-sm-2

{% block fieldset_form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<fieldset{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
{%- set element = 'fieldset' -%}
{%- endif -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<{{ element|default('div') }}{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' form-group')|trim})} %}{{ block('attributes') }}{% endwith %}>
Expand All @@ -310,7 +310,7 @@
{# Help #}

{% block form_help -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text text-muted')|trim}) -%}
<small id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
{{- block('form_help_content') -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{{ block('fieldset_form_row') }}
{%- else -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
{%- set row_class = row_class|default(row_attr.class|default('mb-3')) -%}
Expand Down Expand Up @@ -72,7 +72,7 @@

{% block fieldset_form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<fieldset{% with {attr: row_attr|merge({class: row_attr.class|default('mb-3')|trim})} %}{{ block('attributes') }}{% endwith %}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
{%- set element = 'fieldset' -%}
{%- endif -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
{%- set row_class = row_class|default(row_attr.class|default('mb-3')|trim) -%}
Expand Down Expand Up @@ -367,7 +367,7 @@
{#- Hack to properly display help with input group -#}
{%- set help_class = ' input-group-text' -%}
{%- endif -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ help_class ~ ' mb-0')|trim}) -%}
{%- endif -%}
{{- parent() -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
{%- endblock range_widget %}

{%- block button_widget -%}
{%- if label is empty -%}
{%- if not label -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
Expand Down Expand Up @@ -301,7 +301,7 @@
{%- endblock form_label -%}

{%- block form_label_content -%}
{%- if label is empty -%}
{%- if not label -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
Expand Down Expand Up @@ -331,7 +331,7 @@
{# Help #}

{% block form_help -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-text')|trim}) -%}
<{{ element|default('div') }} id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
{{- block('form_help_content') -}}
Expand Down Expand Up @@ -367,7 +367,7 @@

{%- block form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<div{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{%- block form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<tr{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
{% set embed_label_classes = parent_label_class|split(' ')|filter(class => class in ['checkbox-inline', 'radio-inline']) %}
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ embed_label_classes|join(' '))|trim}) -%}
{% endif %}
{% if label is empty %}
{% if not label %}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
Expand All @@ -283,7 +283,7 @@

{% block form_row -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<div{% with {attr: row_attr|merge({class: (row_attr.class|default('') ~ ' row')|trim})} %}{{ block('attributes') }}{% endwith %}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{%- block checkbox_row -%}
{%- set row_attr = row_attr|merge({ class: row_attr.class|default(row_class|default('mb-6')) }) -%}
{%- set widget_attr = {} -%}
{%- if help is not empty -%}
{%- if help -%}
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
{%- endif -%}
<div{% with {attr: row_attr} %}{{ block('attributes') }}{% endwith %}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
{%- endblock _text_id_widget %}

{% block _names_entry_label -%}
{% if label is empty %}
{% if not label %}
{%- set label = name|humanize -%}
{% endif -%}
<label>Custom label: {{ label|trans(label_translation_parameters, translation_domain) }}</label>
{%- endblock _names_entry_label %}

{% block _name_c_entry_label -%}
{% if label is empty %}
{% if not label %}
{%- set label = name|humanize -%}
{% endif -%}
<label>Custom name label: {{ label|trans(label_translation_parameters, translation_domain) }}</label>
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Deprecate `TranslatableMessage::__toString`

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function testFlattenedTrans($expected, $messages, $translatable)
$this->assertSame($expected, $translatable->trans($translator, 'fr'));
}

/**
* @group legacy
*/
public function testToString()
{
$this->assertSame('Symfony is great!', (string) new TranslatableMessage('Symfony is great!'));
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/TranslatableMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ public function __construct(
) {
}

/**
* @deprecated since Symfony 7.4
*/
public function __toString(): string
{
trigger_deprecation('symfony/translation', '7.4', 'Method "%s()" is deprecated.', __METHOD__);

return $this->getMessage();
}

Expand Down
Loading