8000 [Translation] Entity constraints are not taken into account in the translation:extract · Issue #45039 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Entity constraints are not taken into account in the translation:extract #45039

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
geoffroyp opened this issue Jan 17, 2022 · 3 comments · Fixed by #46161
Closed

Comments

@geoffroyp
Copy link

Description

In the current symfony documentation ( https://symfony.com/doc/current/validation/translations.html ), it says that we can translate validation constraints by simply adding a translation key in "message" property, then adding this key in a "validators" catalog file.

Nice. But it would be soooo much better, easier and safer it we could generate this file by using the translation:extract command like for any other translation.

However, according to the translator documentation: https://symfony.com/doc/current/translation.html#extracting-translation-contents-and-updating-catalogs-automatically

The translation:extract command looks for missing translations in:

Templates stored in the templates/ directory (or any other directory defined in the twig.default_path and twig.paths config options);
Any PHP file/class that injects or autowires the translator service and makes calls to the trans() method.
Any PHP file/class stored in the src/ directory that creates Translations using the constructor or the t() method or calls the trans() method.

Therefore, since validator in entities

  • are not in /templates/
  • do not inject translator service
  • do not call trans() or t()

then these keys are never extracted by the translation:extract command. And indeed, after trying it in my project, I can confirm they are not.

Could we get a fix for that?

Example

No response

@ro0NL
Copy link
Contributor
ro0NL commented Jan 17, 2022

ref #44911 (comment)

@nicolas-grekas
Copy link
Member

@geoffroyp, @ro0NL or anyone else, would you like to work on this?

@welcoMattic
Copy link
Member
welcoMattic commented Apr 8, 2022

There is a workaround to "force" entity constraint messages to be extracted:

#[ORM\Entity(repositoryClass: FooRepository::class)]
class Foo
{
    #[ORM\Column(type: 'string', length: 255)]
    #[Assert\NotBlank([], new TranslatableMessage('foo.name.not_blank.message', [], 'validators'))]
    private string $name;

As PhpExtractor is looking for TranslatableMessage occurences, and TranslatableMessage implements a __toString method, it can be extracted.

However, it needs PHP >= 8.1, as instanciation in expression are not allowed before PHP 8.1.

I think this issue can join the reasons list to write a new PhpExtractor (see #45972).

cc @victou

@fabpot fabpot closed this as completed Oct 20, 2022
fabpot added a commit that referenced this issue Oct 20, 2022
This PR was merged into the 6.2 branch.

Discussion
----------

[Translation] Add `PhpAstExtractor`

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #44899, #42285, #45972, #45039
| License       | MIT
| Doc PR        | symfony/symfony-docs#17369

After discussions with `@stof` and `@nicolas`-grekas at SymfonyLive Paris 2022, it appears clear that we need a brand new PhpExtractor in Translation to support various syntax, especially PHP8 new syntax (named arguments for example).

In order to make this new extractor sustainable, performant and maintainable, it must be based on AST. That's why this PR adds a new optional dependency on [nikic/php-parser](https://github.com/nikic/PHP-Parser).

The tests suite is the same as PhpExtractorTest, in addition to new cases for newly supported syntax.

NB: I was inspired by https://github.com/php-translation/extractor design, with adaptations for Symfony.

To-do:

- [x] As suggested by `@Nyholm`, take a look at https://github.com/php-translation/extractor
- [x] Fix #45039 (could be achieved easily with AST tree)

Commits
-------

5d4a81f Add PhpAstExtractor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
0