Skip to content

Form Class translation extraction #35082

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

Closed
StefanNiederkofler opened this issue Dec 22, 2019 · 16 comments
Closed

Form Class translation extraction #35082

StefanNiederkofler opened this issue Dec 22, 2019 · 16 comments

Comments

@StefanNiederkofler
Copy link

StefanNiederkofler commented Dec 22, 2019

Symfony version(s) affected: 4.4.2 and 5.0.2

Description
Labels defined in a Form Class are not extracted to translation file

How to reproduce
Download symfony/demo, remove translation files, run

translation:update --force de

search for translation "label.tags". It is defined in Form/PostType as label, but it is missing now in the translation file.

@StefanNiederkofler
Copy link
Author

@javiereguiluz is this a new feature? Isn't that supported yet?
For now i resolved it by using php-translation/extractor, but I think this would be a important feature, if it really is not implemented by the symfony/translation yet. Like that, the component is not really usable because an important part of the translations are missing.
What do you think?

@xabbuh
Copy link
Member

xabbuh commented Dec 24, 2019

It's indeed a known limitation that extraction from form labels is not possible. That will be a new feature.

@craigh
Copy link

craigh commented Jan 10, 2020

It's indeed a known limitation that extraction from form labels is not possible. That will be a new feature.

This blog says it is supported... not a new feature.
https://symfony.com/blog/new-in-symfony-4-3-improved-form-translation

@xabbuh
Copy link
Member

xabbuh commented Jan 10, 2020

I don't see anything in the blog post talking about the extraction of translations.

@StefanNiederkofler
Copy link
Author

Yes, it's only about using variables in labels and helpers.
But it's funny that there are improvements on that, but the basics (extraction of simple labels) cannot be done yet with the symfony translation :)

@xabbuh
Copy link
Member

xabbuh commented Jan 10, 2020

That's probably because writing the code to extract the translations is a harder task. But if you would like to work on it, I will happily review a PR implementing this task. :)

@Pixelshaped
Copy link

Pixelshaped commented Jan 21, 2020

Pretty sure this was working beforehand and it's not a feature but a bug ?

In a FormType:

$builder
            ->add('title', TextType::class, [
                'label' => 'My Title'
            ])

Used to work, but not anymore. It's supposed to as per documentation: https://symfony.com/doc/current/reference/forms/types/form.html#label-translation-parameters

Those strings are not extracted anymore. But maybe I dreamt and those strings were never extracted to begin with!

@xabbuh
Copy link
Member

xabbuh commented Jan 21, 2020

@Pixelshaped The documentation you linked to does not talk about the extraction of translations. It just shows how you can translate the label.

@Pixelshaped
Copy link

You're right. I think what mislead me was the debug toolbar reporting the right translation domain for the form once configured in configureOptions.

@ro0NL
Copy link
Contributor

ro0NL commented Sep 8, 2020

now with #37670 merged, we could think of 'label' => new Translatable('id') :) that could reduce the no. of related form options by a lot :)

@aleho
Copy link
Contributor

aleho commented Oct 21, 2020

Out of curiosity I tried to get the PhpExtractor to handle FormTypes but adding the path to twig setup.

twig:
    default_path: '%kernel.project_dir%/templates'

    paths:
        '%kernel.project_dir%/src/Form': ~

Using $label = new TranslatableMessage('Password'); in my Types the extractor seems to pick everything up correctly.

Maybe pointing PhpExtractor to FormTypes could make it into 5.2?

@yceruto
Copy link
Member

yceruto commented Feb 20, 2021

It's not easy to detect where to look for new translations. Maybe this should be a particular decision per case. A new option in related commands could help with this need:

bin/console translation:update en --extra-path=src/Controller --extra-path=src/Form

For the record: this PR #40229 was open to add the whole src/.

fabpot added a commit that referenced this issue Mar 5, 2021
… from all of src (natewiebe13)

This PR was squashed before being merged into the 5.3-dev branch.

Discussion
----------

[FrameworkBundle][Translation] Extract translation IDs from all of src

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Related to #39126 possibly #35082 as well
| License       | MIT
| Doc PR        | TBD

This PR allows extracting (`bin/console translation:update`) and debugging (`bin/console debug:translation`) translations using Translatable messages.

Currently we only check classes that include the `TranslatorInterface`, but this no longer covers all instances of this.

Current considerations:
- Should this be treated as a bug fix or a new feature? On one hand, text extraction would no longer work if moving to TranslatableMessages (like we're doing) on the other, it wasn't intended to search all PHP files. As a bug fix would get this into Symfony faster, as a feature would mean having to wait until 5.3 is released.
- Is there a better way to get the source directory that doesn't involve hardcoding `/src`?
- Adding this in on a project with ~12k LOC in `/src` takes these operations from about 3s to 5s, but I feel like this is reasonable considering this command isn't likely called constantly. I can provide more accurate stats as requested.

Commits
-------

b02ae50 [FrameworkBundle][Translation] Extract translation IDs from all of src
n-peugnet added a commit to n-peugnet/antilope that referenced this issue Apr 8, 2021
This is a hack to scan froms for translation while waiting for
symfony/symfony#40229 to be released.
See symfony/symfony#35082 (comment)
n-peugnet added a commit to n-peugnet/antilope that referenced this issue Apr 9, 2021
This is a hack to scan froms for translation while waiting for
symfony/symfony#40229 to be released.
See symfony/symfony#35082 (comment)
@Tobion
Copy link
Contributor

Tobion commented Jul 13, 2021

now with #37670 merged, we could think of 'label' => new Translatable('id') :) that could reduce the no. of related form options by a lot :)

@ro0NL I've opened #42077 to deprecate those outdated options

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@carsonbot
Copy link

Could I get a reply or should I close this?

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

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

10 participants