Nullable message id?

This commit is contained in:
Yonel Ceruto 2019-08-25 10:29:07 -04:00
parent 959eb56488
commit 55eac639c5
6 changed files with 28 additions and 6 deletions

View File

@ -124,7 +124,7 @@
{{- block('form_widget_simple') -}}
{%- 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 %}>
{%- 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) -}}
{%- endif -%}
</label>

View File

@ -141,7 +141,7 @@ class DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorIn
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) {
$domain = 'messages';

View File

@ -131,7 +131,7 @@ class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterfac
/**
* 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) {
$domain = 'messages';

View File

@ -491,6 +491,19 @@ class TranslatorTest extends TestCase
$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()
{
return [
@ -512,6 +525,7 @@ class TranslatorTest extends TestCase
['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], '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', ''],
['', null, '', [], 'fr', ''],
];
}

View File

@ -210,11 +210,14 @@ class Translator implements LegacyTranslatorInterface, TranslatorInterface, Tran
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
if ('' === $id = (string) $id) {
return '';
}
if (null === $domain) {
$domain = 'messages';
}
$id = (string) $id;
$catalogue = $this->getCatalogue($locale);
$locale = $catalogue->getLocale();
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);
if ('' === $id = (string) $id) {
return '';
}
if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
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';
}
$id = (string) $id;
$catalogue = $this->getCatalogue($locale);
$locale = $catalogue->getLocale();
while (!$catalogue->defines($id, $domain)) {

View File

@ -43,7 +43,9 @@ trait TranslatorTrait
*/
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%'])) {
return strtr($id, $parameters);