I have the following code under user profile form:
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstName', TextType::class,
['label' => $this->translator->trans('First name', [], 'profile')]);
// more fields
}
My profile.en.yaml
file contains line:
'First name': First name value
I just added ' value'
to reproduce the exact error.
Now, when I update my translations with
php bin/console translation:update --force en
An xml file for profile
domain is generated.
When I run the form on my browser, I get the value translated into 'First name value'
.
But on the symfony profiler toolbar I get an error. When I open it, I have record for successful profile
translation:
These messages are correctly translated into the given locale.
Locale Domain Times used Message ID Message Preview
en profile 1 First name First name value
And error message for missing message
translation:
These messages are not available for the given locale and cannot be found in the fallback locales. Add them to the translation catalogue to avoid Symfony outputting untranslated contents.
Locale Domain Times used Message ID Message Preview
en messages 1 First name value First name value
How can I get rid of the error without using default messages
domain?
Edit 1: I played a little with domains and values. Some update to my question. Even if I use messages
domain, if I translate id value to a different text, I get two records under profiler - one for successful translation from id to value and one for 'missing' value.
My assumption: when I use translator under form builder with label, Symfony attempts to translate a value from that label and the value from the form view. If two values are the same, I get record with two successful translation. If value is different from id, I have one successful translation and one failed.
When I just use explicit translation under rendered view, everything works fine.
Therefore my question is the same, but more specific: how to tell Symfony not to translate value upon the form view if it was already translated as label under Form class.
Edit 2: The only method worked so far is to get rid of translator under Form class and assign texts to labels without translation. But with this approach I can use only messages
and cannot use different domain.
In my twig file the form is encapsulated under
{{ form_widget(form) }}
and I cannot get labels out of that if I do not specify the code or use form themes. And even if I do that, it's still confusing, because I cannot use translator under Form class / builder labels and I lose such an important capability to specify profile
domain and translation ids under Form class and not to worry about that under Form view. So the initial question is not answered.