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

[translation][extractor] double translation of forms labels / implementing labels extractor feature #33499

Closed
sabat24 opened this issue Sep 7, 2019 · 6 comments

Comments

@sabat24
Copy link

sabat24 commented Sep 7, 2019

Translaction extractor can't extract texts from labels in form builder. Offical doc is advising to DI translator and use it when creating labels. So for example I have got something like that:

->add('email', EmailType::class, ['label' => $this->translator->trans('email', [], 'admin')])

Extractor extracts that placeholder correctly, but then I get errors in SF profiler that I have got a lot of missing translations.

obraz

I think it is because of that Form component is trying to translate again the text, which was already translated and passed to label.

In this case is there an option to tell Form that it shouldn't translate my labels?

answer translation_domain = false

What is more confusing - why extractor just can't extract labels directly from forms?
There is a JMSTranslationBundle which handles quite well with extracting labels from form builders and it can do it since SF 2.0. Is there any reason why you didn't add such option to your extractor? (are going to do that in very near future?)

It leads to another problem. Let's assume that I have got in my messages domain a translation for "Email" placeholder. It will result with wrong translation of label.

At first translator translates 'email' from 'admin' domain to 'Email'. Then Form component translates 'Email' from messages domain to something like 'Email is a fun thing'.

Is there any solution to the first problem of double translation of labels?

EDIT
Ok I found that I need to use 'translation_domain' set as false when adding fields. It wasn't very obvious at first to me.

@sabat24 sabat24 changed the title [translation][extractor] double translation of forms labels [translation][extractor] double translation of forms labels / implementing labels extractor feature Sep 7, 2019
@xabbuh
Copy link
Member

xabbuh commented Sep 9, 2019

I am closing here since this is indeed not a bug but the expected behaviour and setting the translation domain to false is the solution as pointed out.

@xabbuh xabbuh closed this as completed Sep 9, 2019
@stof
Copy link
Member

stof commented Sep 9, 2019

well, we should not be translating the label in the form type when passing the label option, as the form theme already translate it.
If the official doc advises translating explicitly, I would consider that a documentation bug.

@xabbuh
Copy link
Member

xabbuh commented Sep 9, 2019

I don't see that pattern used anywhere in the docs.

@stof
Copy link
Member

stof commented Sep 9, 2019

Translaction extractor can't extract texts from labels in form builder. Offical doc is advising to DI translator and use it when creating labels. So for example I have got something like that:

@sabat24 can you share with us where you saw that advice in the official documentation ?

@sabat24
Copy link
Author

sabat24 commented Sep 9, 2019

In translation component in summary there is information:

Abstract messages in your application by wrapping each in the trans() method;

In different parts of doc, there is also mentioned indirectly that extractor searches for

Any PHP file/class that injects or autowires the translator service and makes calls to the trans() function.

For me based mainly on first statement it was an obvious conslusion that if I want get translations extracted from my form class I need use Translator service and trans() method. Which is basically correct, but it took me a while to find information that I need to set the translation_domain to false as well in such case.

@StefanNiederkofler
Copy link

@stof is the only way to extract the label strings for translation the approach used by sabat24? (using translator service in the Form Class).
I'm using symfony 4.4 and my labels are not extracted, but if I add them manually to the translation file, they are translated correctly.
Everything else is extracted correctly (controllers where i use the translator service and twig files)

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

6 participants