Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Translation] Translation Debug command doesn't work correctly with domain as constant #53356

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
VincentLanglet opened this issue Jan 2, 2024 · 5 comments

Comments

@VincentLanglet
Copy link
Contributor

VincentLanglet commented Jan 2, 2024

Symfony version(s) affected

all

Description

Currently both

use App\Const\Domain;
$this->translator->trans('foo', Domain::CUSTOM);

in php and

{{ 'foo'|trans({}, constant('App\\Constant\\Domain::CUSTOM')) }};

in twig is misunderstood by translation extractor.

The first one thinks the domain is 'messages', the second one '_undefined'.

It would be nice to correctly support the domain.
If it's really not possible, it seems better to me to have '_undefined' in both case.

How to reproduce

Running

bin/console debug:translation

with the previous code.

Possible Solution

For the AbstractVisitor, I tried doing something with

if ($node instanceof Node\Expr\ClassConstFetch) {
            try {
                $reflection = new \ReflectionClass($node->class->toString());
                $constant = $reflection->getReflectionConstant($node->name->toString());
                if (false !== $constant && \is_string($constant->getValue())) {
                    return $constant->getValue();
                }
            } catch (\ReflectionException) {}
        }

but this works only with full FQCN, and not when it's imported/aliased at the start of the file...

For twig, I tried #53357

Additional Context

No response

@smnandre
Copy link
Member

smnandre commented Jan 2, 2024

{{ 'foo'|trans({}, constant('App\Constant\Domain::CUSTOM')) }};

Probably a typo but just to be sure, you used double backslashes instead of simple ones ' in your real code ?

{{ 'foo'|trans({}, constant('App\\Constant\\Domain::CUSTOM')) }};

@VincentLanglet
Copy link
Contributor Author

{{ 'foo'|trans({}, constant('App\Constant\Domain::CUSTOM')) }};

Probably a typo but just to be sure, you used double backslashes instead of simple ones ' in your real code ?

{{ 'foo'|trans({}, constant('App\\Constant\\Domain::CUSTOM')) }};

Yes indeed it was a typo, I edited the issue.

@VincentLanglet
Copy link
Contributor Author

@welcoMattic you introduced the PhpAstExtractor in #46161
maybe you'll know how to support the syntax:

use App\Const\Domain;
$this->translator->trans('foo', Domain::CUSTOM);

?

@welcoMattic
Copy link
Member

I will take a look on it next week. In the mean time, if you find a way to support it with FQCN, feel free to open a PR.

nicolas-grekas added a commit that referenced this issue Jan 23, 2024
…omain (VincentLanglet)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[Translation] Fix `TranslationNodeVisitor` with constant domain

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT

Related to #53356 but fixing only the twig part.

Commits
-------

d14795d [Translation] Fix `TranslationNodeVisitor` with constant domain
symfony-splitter pushed a commit to symfony/twig-bridge that referenced this issue Jan 23, 2024
…omain (VincentLanglet)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[Translation] Fix `TranslationNodeVisitor` with constant domain

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT

Related to symfony/symfony#53356 but fixing only the twig part.

Commits
-------

d14795dbb1 [Translation] Fix `TranslationNodeVisitor` with constant domain
@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Jan 24, 2024

I will take a look on it next week. In the mean time, if you find a way to support it with FQCN, feel free to open a PR.

I think I found a solution @welcoMattic ; by running the NameResolver before (I found this advice here nikic/PHP-Parser#188 (comment)) I get the full FQCN , cf #53624

nicolas-grekas added a commit that referenced this issue Jan 29, 2024
…actor (VincentLanglet)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Translation] Fix constant domain resolution in PhpAstExtractor

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #53356
| License       | MIT

Similar to #53357

Closes #53356

Commits
-------

c43f6c0 [Translation] Fix constant domain resolution in PhpAstExtractor
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

5 participants