minor #33329 [Translator] Nullable message id? (yceruto)
This PR was merged into the 4.4 branch. Discussion ---------- [Translator] Nullable message id? | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | -610a4e978f/src/Symfony/Component/Translation/DataCollectorTranslator.php (L144)
The message id shouldn't be `null`, but it's breaking the current code now. Shouldn't we first deprecate of passing `null` even if it's well documented? Out there can be a lot of `->trans($var)` and `var|trans()` (like the current ones fixed here) that will break without previous warning. WDTY? Commits -------55eac63
Nullable message id?
This commit is contained in:
commit
9690562410
@ -124,7 +124,7 @@
|
|||||||
{{- block('form_widget_simple') -}}
|
{{- block('form_widget_simple') -}}
|
||||||
{%- set label_attr = label_attr|merge({ class: (label_attr.class|default('') ~ ' custom-file-label')|trim }) -%}
|
{%- set label_attr = label_attr|merge({ class: (label_attr.class|default('') ~ ' custom-file-label')|trim }) -%}
|
||||||
<label for="{{ form.vars.id }}" {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
|
<label for="{{ form.vars.id }}" {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
|
||||||
{%- if attr.placeholder is defined -%}
|
{%- if attr.placeholder is defined and attr.placeholder is not none -%}
|
||||||
{{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}}
|
{{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}}
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
</label>
|
</label>
|
||||||
|
@ -141,7 +141,7 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
|
|||||||
return $this->messages;
|
return $this->messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = [])
|
private function collectMessage(?string $locale, ?string $domain, ?string $id, string $translation, ?array $parameters = [])
|
||||||
{
|
{
|
||||||
if (null === $domain) {
|
if (null === $domain) {
|
||||||
$domain = 'messages';
|
$domain = 'messages';
|
||||||
|
@ -131,7 +131,7 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
|
|||||||
/**
|
/**
|
||||||
* Logs for missing translations.
|
* Logs for missing translations.
|
||||||
*/
|
*/
|
||||||
private function log(string $id, ?string $domain, ?string $locale)
|
private function log(?string $id, ?string $domain, ?string $locale)
|
||||||
{
|
{
|
||||||
if (null === $domain) {
|
if (null === $domain) {
|
||||||
$domain = 'messages';
|
$domain = 'messages';
|
||||||
|
@ -491,6 +491,19 @@ class TranslatorTest extends TestCase
|
|||||||
$this->addToAssertionCount(1);
|
$this->addToAssertionCount(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTransNullId()
|
||||||
|
{
|
||||||
|
$translator = new Translator('en');
|
||||||
|
$translator->addLoader('array', new ArrayLoader());
|
||||||
|
$translator->addResource('array', ['foo' => 'foofoo'], 'en');
|
||||||
|
|
||||||
|
$this->assertSame('', $translator->trans(null));
|
||||||
|
|
||||||
|
(\Closure::bind(function () use ($translator) {
|
||||||
|
$this->assertSame([], $translator->catalogues);
|
||||||
|
}, $this, Translator::class))();
|
||||||
|
}
|
||||||
|
|
||||||
public function getTransFileTests()
|
public function getTransFileTests()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@ -512,6 +525,7 @@ class TranslatorTest extends TestCase
|
|||||||
['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], 'fr', ''],
|
['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], 'fr', ''],
|
||||||
['Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => 'awesome'], 'fr', ''],
|
['Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => 'awesome'], 'fr', ''],
|
||||||
['Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr', ''],
|
['Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr', ''],
|
||||||
|
['', null, '', [], 'fr', ''],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,11 +210,14 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
|||||||
*/
|
*/
|
||||||
public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
||||||
{
|
{
|
||||||
|
if ('' === $id = (string) $id) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
if (null === $domain) {
|
if (null === $domain) {
|
||||||
$domain = 'messages';
|
$domain = 'messages';
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = (string) $id;
|
|
||||||
$catalogue = $this->getCatalogue($locale);
|
$catalogue = $this->getCatalogue($locale);
|
||||||
$locale = $catalogue->getLocale();
|
$locale = $catalogue->getLocale();
|
||||||
while (!$catalogue->defines($id, $domain)) {
|
while (!$catalogue->defines($id, $domain)) {
|
||||||
@ -242,6 +245,10 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
|||||||
{
|
{
|
||||||
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), E_USER_DEPRECATED);
|
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), E_USER_DEPRECATED);
|
||||||
|
|
||||||
|
if ('' === $id = (string) $id) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
|
if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
|
||||||
throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', \get_class($this->formatter)));
|
throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', \get_class($this->formatter)));
|
||||||
}
|
}
|
||||||
@ -250,7 +257,6 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
|
|||||||
$domain = 'messages';
|
$domain = 'messages';
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = (string) $id;
|
|
||||||
$catalogue = $this->getCatalogue($locale);
|
$catalogue = $this->getCatalogue($locale);
|
||||||
$locale = $catalogue->getLocale();
|
$locale = $catalogue->getLocale();
|
||||||
while (!$catalogue->defines($id, $domain)) {
|
while (!$catalogue->defines($id, $domain)) {
|
||||||
|
@ -43,7 +43,9 @@ trait TranslatorTrait
|
|||||||
*/
|
*/
|
||||||
public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
||||||
{
|
{
|
||||||
$id = (string) $id;
|
if ('' === $id = (string) $id) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) {
|
if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) {
|
||||||
return strtr($id, $parameters);
|
return strtr($id, $parameters);
|
||||||
|
Reference in New Issue
Block a user