Skip to content

[Translation] translation:extract with bundle - should also extract translations from PHP code #54798

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

Open
cuchac opened this issue May 2, 2024 · 4 comments

Comments

@cuchac
Copy link
Contributor

cuchac commented May 2, 2024

Symfony version(s) affected

7.1

Description

Documentation mentions

The translation:extract command looks for missing translations in:
...
 - Any PHP file/class that injects or autowires the translator service and makes calls to the trans() method..
...

When bundle is specified, this does not work as stated. Following line limits path to extract texts to only bundle template folder:

$codePaths = [is_dir($bundleDir.'/Resources/views') ? $bundleDir.'/Resources/views' : $bundleDir.'/templates'];

I did not find any other official way to extract PHP code translations from bundle. I believe at least PHP files using translator service should be included in extracted bundle translations.

How to reproduce

php bin/console translation:extract --force fr AcmeBundle

Only translations from templates (AcmeBundle/templates) are extracted. No PHP trans() calls are extracted.

Possible Solution

Use result from $this->getRootCodePaths($kernel) and leave all files from given bundle.

$codePaths = $this->getRootCodePaths($kernel);
$currentName = 'default directory';
// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
try {
$foundBundle = $kernel->getBundle($input->getArgument('bundle'));
$bundleDir = $foundBundle->getPath();
$transPaths = [is_dir($bundleDir.'/Resources/translations') ? $bundleDir.'/Resources/translations' : $bundleDir.'/translations'];
$codePaths = [is_dir($bundleDir.'/Resources/views') ? $bundleDir.'/Resources/views' : $bundleDir.'/templates'];

Additional Context

No response

@cuchac
Copy link
Contributor Author

cuchac commented May 2, 2024

Would you accept contribution to handle this issue? Should there be any command-line parameter or it could be the default behavior?

@welcoMattic welcoMattic added Feature and removed Bug labels Oct 31, 2024
@welcoMattic
Copy link
Member

Hello @cuchac, thank you for your report, it makes sense to be able to extract translations from PHP code from bundles too. However, Symfony could never guarantee that external code respect the recommended ways to translate strings. So like in your own code, the bundles you want to extract translation may use some special cases like concatenation of strings the translation:extract command could never extract at all.

But please, if you want, open a Pull Request to handle translations extraction from PHP Code of bundles, then we can discuss about the proper implementation.

@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?
Every feature is developed by the community.
Perhaps someone would like to try?
You can read how to contribute to get started.

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

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

3 participants