Skip to content
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

[Translation] Allow to add bundles translations path in commands #52456

Open
wants to merge 3 commits into
base: 7.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,19 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode, callable $e
->end()
->defaultValue([])
->end()
->arrayNode('include_bundles_translations_in_commands')
->info('Allow to include bundles translation for commands like translation:extract or translation:push')
->canBeEnabled()
->fixXmlConfig('excluded_bundle')
->children()
->arrayNode('excluded_bundles')
->prototype('scalar')->end()
->defaultValue([])
->info('Bundles that shouldn\'t be included in commands')
->example(['VendorBundle'])
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,12 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if ($container->fileExists($dir = $bundle['path'].'/Resources/translations') || $container->fileExists($dir = $bundle['path'].'/translations')) {
$dirs[] = $dir;
if (
$config['include_bundles_translations_in_commands']['enabled']
&& !\in_array($name, $config['include_bundles_translations_in_commands']['excluded_bundles'], true)
) {
$transPaths[] = $dir;
}
} else {
$nonExistingDirs[] = $dir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="pseudo-localization" type="pseudo_localization" minOccurs="0" maxOccurs="1" />
<xsd:element name="provider" type="translation_provider" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="include-bundles-translations-in-commands" type="include_bundles_translations_in_commands" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="fallback" type="xsd:string" />
Expand Down Expand Up @@ -259,6 +260,13 @@
<xsd:attribute name="dsn" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="include_bundles_translations_in_commands">
<xsd:sequence>
<xsd:element name="excluded-bundle" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean"/>
</xsd:complexType>

<xsd:complexType name="validation">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="static-method" type="xsd:string" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ protected static function getBundleDefaultConfig()
'localizable_html_attributes' => [],
],
'providers' => [],
'include_bundles_translations_in_commands' => [
'enabled' => false,
'excluded_bundles' => [],
],
],
'validation' => [
'enabled' => !class_exists(FullStack::class),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'translator' => [
'include_bundles_translations_in_commands' => [
'excluded_bundles' => ['CustomPathBundle']
],
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config secret="s3cr3t" http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:translator enabled="true">
<framework:include-bundles-translations-in-commands enabled="true">
<framework:excluded-bundle>CustomPathBundle</framework:excluded-bundle>
</framework:include-bundles-translations-in-commands>
</framework:translator>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
framework:
annotations: false
http_method_override: false
handle_all_throwables: true
php_errors:
log: true
translator:
include_bundles_translations_in_commands:
excluded_bundles:
- CustomPathBundle
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,38 @@ public function testTranslatorCacheDirDisabled()
$this->assertNull($options['cache_dir']);
}

public function testTranslatorIncludeBundleTranslationsInCommands()
{
require_once __DIR__.'/Fixtures/TestBundle/TestBundle.php';
require_once __DIR__.'/Fixtures/CustomPathBundle/src/CustomPathBundle.php';

$container = $this->createContainerFromFile('translator_include_bundles_translations_in_commands', [
'kernel.bundles' => [
'FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle',
'TestBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\TestBundle',
'CustomPathBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\CustomPathBundle',
],
'kernel.bundles_metadata' => [
'FrameworkBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..'],
'TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle'],
'CustomPathBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/CustomPathBundle'],
],
]);

$transPaths = $container->getDefinition('console.command.translation_extract')->getArgument(6);
$this->assertContains(
strtr(__DIR__.'/Fixtures/TestBundle/Resources/translations', '/', \DIRECTORY_SEPARATOR),
$transPaths,
'->registerTranslatorConfiguration() finds translation resources in bundles to extract'
);

$this->assertNotContains(
strtr(__DIR__.'/Fixtures/CustomPathBundle/Resources/translations', '/', \DIRECTORY_SEPARATOR),
$transPaths,
'->registerTranslatorConfiguration() exclude some bundles from translation resources to extract'
);
}

public function testValidation()
{
$container = $this->createContainerFromFile('full');
Expand Down