Skip to content
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

TranslatableMessage not working in form #50744

Closed
kruggs opened this issue Jun 22, 2023 · 9 comments
Closed

TranslatableMessage not working in form #50744

kruggs opened this issue Jun 22, 2023 · 9 comments

Comments

@kruggs
Copy link

kruggs commented Jun 22, 2023

Symfony version(s) affected

6.3.0

Description

Hi,

TranslatableMessage seems not working in form option.
Maybe i don't get some point but according to the documentaiton, i should be able to use the TranslatableMessage in form, no ?

How to reproduce

I took, the Symfony demo project 2.2.3
Then, i replaced the field "title" in the PostType.php file

            ->add('title', null, [
                'attr' => ['autofocus' => true],
                'label' => new TranslatableMessage('label.title',[], 'forms')
            ])

The title is not translated.

Possible Solution

No response

Additional Context

No response

@xabbuh
Copy link
Member

xabbuh commented Jun 22, 2023

Did you define the translations in the forms domain?

@kruggs
Copy link
Author

kruggs commented Jun 22, 2023

@xabbuh It's the same translation unit id which already exist in the demo.
I tried with "form" and "form+intl-icu",it's not working.

I did not tried to create a new translation file : "form.xlf", but i assume the system should use the proper domain.

edit : my bad, i though the domain was "form" but in the demo, it's in "messages" and it's working now.

I have the same issue (not translated) in my personal project. I will look deeper since it's working in the demo.

Sorry for that.

Thanks

@kruggs
Copy link
Author

kruggs commented Jun 22, 2023

Finally after testing on demo project :

            ->add('title', null, [
                'attr' => ['autofocus' => true],
                'label' => new TranslatableMessage('label.title',[], 'messages'),
				'data'       => new TranslatableMessage('label.title',[], 'messages'),
				
            ])

The "data" display the value "label.title" instead of translation
It's not working neither with a ChoiceType with "attr/placeholder"

@xabbuh
Copy link
Member

xabbuh commented Jun 22, 2023

Can you create a small example application that allows to reproduce your issue?

@stof
Copy link
Member

stof commented Jun 22, 2023

data is not something that gets translated, so passing a TranslatableMessage here is not expected to work.

@kruggs
Copy link
Author

kruggs commented Jun 22, 2023

Hi @stof

I can understand your point but it's a limiting the feature, no ?

In my case :
I have a page to edit Taxonomy. In that page i display trans_unit_id, the English translation and user can choose to a parent is needs.
The field English translation is non-mapped obviously. To display the proper information in this field, i do a simple translatableMessage of the current Taxonomy->getTransUnitId().
So, i display this field for information (it's not editable by user for now)

@xabbuh For the exemple, you can added in PostType of the demo symfony project :

            ->add('choice', EntityType::class, [
                'label' => 'label.tags',
				'choice_label'  => function ($choice, $key, $value) {

					return new TranslatableMessage('label.title',[], 'messages');
				},
				'class'         => Post::class,
				'mapped' => false,
				'attr' =>
				[
				'data-placeholder' => new TranslatableMessage('label.title',[], 'messages')
				]
            ])

"data-placeholder" does not display the translated value
"choice_label" does not translate too

@stof
Copy link
Member

stof commented Jun 22, 2023

A TranslatableMessage still needs to be passed to the translator to actually produce a translated string. This class cannot magically get access to the translator to produce a translated string (btw, that's the reason why this class is not stringable).

In your example, choice_label is not translated because EntityType disables the translation of choices by default (as those are generally user-provided values stored in your entity), contrary to the its parent type ChoiceType. If you re-enable the translation of choices, it will work (not that if you don't need to pass parameters, returning the translation key is enough, except maybe for detection by the extractor)

@kruggs
Copy link
Author

kruggs commented Jun 22, 2023

I did not know for the option "choice_translation_domain". Thanks :)
I enabled it and used TranslatableMessage (because i have to pass the specific domain which is not the default one) and "choice_label" is working :)
For the information, the code was just for the example, in my code, i'm using data from the entity which is a "trans_unit_id", hence the translation after.

I'm sorry i did not understand the first part of you answer. What class are you speaking about ?

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@xabbuh xabbuh closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants