Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Translation] Handle the translation of empty strings#48833

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@javiereguiluz
Copy link
Member

QA
Branch?5.4
Bug fix?yes
New feature?no
Deprecations?no
Tickets-
LicenseMIT
Doc PR-

In some apps, you might use an expression as the value of someTranslatableMessage object. The result of this expression can result in using an empty string as the value of the that object:

newTranslatableMessage($someCondition ?$someObject->someMethod() :'')

The issue is that Symfony will report that empty string in the list of "missing translations" (like in the first row of this image):

I think this is a bug and an empty string should just be output "as is" without reporting it as missing.

What do you think? Is this truly a bug? Would it be a new feature for 6.3? The current behavior is correct and we should close without merging? Thanks.

@carsonbot
Copy link

Hey!

I think@rvanlaak has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@maxbeckers
Copy link
Contributor

maxbeckers commentedJan 10, 2023
edited
Loading

Hi@javiereguiluz,

i can't reproduce the problem. When I translate{{ 'invalid'|trans }}, then i get one message for missing Translation. When I translate{{ ''|trans }} then there is no message.

Tried with a clean symfony 5.4 and 6.1 environment.

UPDATE
I can reproduce with setting this in controller['test' => new TranslatableMessage(''),] and then{{ test|trans }}. Sorry, didn't read the issue correlctly. Then I'm fine with the fix :) And yes for me it's as well a kind of bugfix, so I'm fine with 5.4.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedJan 11, 2023
edited
Loading

Why don't you use this code instead?

$someCondition ?newTranslatableMessage($someObject->someMethod()) :''

It would make more sense to me. Why wrap the empty string in a translatable when it's not translatable content?

@javiereguiluz
Copy link
MemberAuthor

javiereguiluz commentedJan 17, 2023
edited
Loading

Looking at Twig Bridgetrans() function, I think there's an inconsistency:

publicfunctiontrans(string|\Stringable|TranslatableInterface|null$message,array|string$arguments = [],string$domain =null,string$locale =null,int$count =null):string
{
if ($messageinstanceof TranslatableInterface) {
if ([] !==$arguments && !\is_string($arguments)) {
thrownew \TypeError(sprintf('Argument 2 passed to "%s()" must be a locale passed as a string when the message is a "%s", "%s" given.',__METHOD__, TranslatableInterface::class,get_debug_type($arguments)));
}
return$message->trans($this->getTranslator(),$locale ?? (\is_string($arguments) ?$arguments :null));
}
if (!\is_array($arguments)) {
thrownew \TypeError(sprintf('Unless the message is a "%s", argument 2 passed to "%s()" must be an array of parameters, "%s" given.', TranslatableInterface::class,__METHOD__,get_debug_type($arguments)));
}
if ('' ===$message = (string)$message) {
return'';
}
if (null !==$count) {
$arguments['%count%'] =$count;
}
return$this->getTranslator()->trans($message,$arguments,$domain,$locale);
}

The behavior is different:

(1) If we pass a string:

  • If it's empty, return''
  • If it's not empty, translate it

(2) It we pass aTranslatableInterface object:

  • Translate it in all cases, even if it's an empty string

That's why we don't get a "missing translation" warning when doing''|trans but we get it when doingt('')|trans

Would you agree to updatetrans() code to also return'' whenTranslatableInterface content is an empty string?

@javiereguiluz
Copy link
MemberAuthor

I've changed the proposed solution completely. If you can, please review it again. Thanks.

@nicolas-grekas
Copy link
Member

Thank you@javiereguiluz.

javiereguiluz reacted with thumbs up emoji

@nicolas-grekasnicolas-grekas merged commitcfda9b4 intosymfony:5.4Feb 23, 2023
@javiereguiluzjaviereguiluz deleted the translatable_empty branchFebruary 23, 2023 15:37
This was referencedFeb 28, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@welcoMatticwelcoMatticwelcoMattic approved these changes

+1 more reviewer

@maxbeckersmaxbeckersmaxbeckers approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

5.4

Development

Successfully merging this pull request may close these issues.

5 participants

@javiereguiluz@carsonbot@maxbeckers@nicolas-grekas@welcoMattic

[8]ページ先頭

©2009-2025 Movatter.jp