Skip to content

[FrameworkBundle][Translation] allow register custom resources paths. #14399

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,17 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->info('translator configuration')
->canBeEnabled()
->fixXmlConfig('fallback')
->fixXmlConfig('path')
->children()
->arrayNode('fallbacks')
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
->prototype('scalar')->end()
->defaultValue(array('en'))
->end()
->arrayNode('paths')
->defaultValue(array())
->prototype('scalar')->end()
->end()
->booleanNode('logging')->defaultValue($this->debug)->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,15 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder

// Discover translation directories
$dirs = array();

foreach ($config['paths'] as $path) {
if (!is_dir($path)) {
throw new \InvalidArgumentException(sprintf('The "%s" translation path does not exist.', $path));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really a good idea to silently ignore values not representing an array? This would make debugging a bit harder I guess. What about doing validation in the Configuration class instead and providing a meaningful error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not possible to add validation it in the Configuration because we need to resolve parameters before checking directories, so I'll throw an exception here instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, that could still become an issue if a parameter were changed in a compiler pass, couldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true, is it safe to move it into TranslatorPass ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TranslatorPass is executed before the optimization passes (in which parameters are resolved). To be on the safe side, the compiler pass must be registered as a PassConfig::TYPE_BEFORE_REMOVING pass.


$dirs[] = $path;
}

if (class_exists('Symfony\Component\Validator\Validation')) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
<xsd:complexType name="translator">
<xsd:sequence>
<xsd:element name="fallback" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="fallback" type="xsd:string" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ protected static function getBundleDefaultConfig()
'enabled' => false,
'fallbacks' => array('en'),
'logging' => true,
'paths' => array(),
),
'validation' => array(
'enabled' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ public function testTranslatorMultipleFallbacks()
$this->assertEquals(array('en', 'fr'), $calls[0][1][0]);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The "foo" translation path does not exist.
*/
public function testTranslatorWithInvalidPaths()
{
$container = $this->createContainer();
$loader = new FrameworkExtension();
$loader->load(array(array('translator' => array('paths' => array('foo')))), $container);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
Expand Down