Skip to content

Commit 8ca1510

Browse files
Seldaekaitboudad
authored andcommitted
[FrameworkBundle][DX] Add option to specify additional translation loading paths
1 parent 3529b85 commit 8ca1510

File tree

9 files changed

+25
-1
lines changed

9 files changed

+25
-1
lines changed

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,17 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
575575
->info('translator configuration')
576576
->canBeEnabled()
577577
->fixXmlConfig('fallback')
578+
->fixXmlConfig('path')
578579
->children()
579580
->arrayNode('fallbacks')
580581
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
581582
->prototype('scalar')->end()
582583
->defaultValue(array('en'))
583584
->end()
584585
->booleanNode('logging')->defaultValue($this->debug)->end()
586+
->arrayNode('paths')
587+
->prototype('scalar')->end()
588+
->end()
585589
->end()
586590
->end()
587591
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,13 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
692692
$dirs[] = $dir;
693693
}
694694
}
695+
foreach ($config['paths'] as $dir) {
696+
if (is_dir($dir)) {
697+
$dirs[] = $dir;
698+
} else {
699+
throw new \UnexpectedValueException(sprintf('%s defined in translator.paths does not exist or is not a directory', $dir));
700+
}
701+
}
695702
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/translations')) {
696703
$dirs[] = $dir;
697704
}

Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<xsd:complexType name="translator">
184184
<xsd:sequence>
185185
<xsd:element name="fallback" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
186+
<xsd:element name="path" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
186187
</xsd:sequence>
187188
<xsd:attribute name="enabled" type="xsd:boolean" />
188189
<xsd:attribute name="fallback" type="xsd:string" />

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ protected static function getBundleDefaultConfig()
146146
'enabled' => false,
147147
'fallbacks' => array('en'),
148148
'logging' => true,
149+
'paths' => array(),
149150
),
150151
'validation' => array(
151152
'enabled' => false,

Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'translator' => array(
5151
'enabled' => true,
5252
'fallback' => 'fr',
53+
'paths' => array('%kernel.root_dir%/Fixtures/translations'),
5354
),
5455
'validation' => array(
5556
'enabled' => true,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
custom:
2+
paths: test

Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
<framework:resource>theme2</framework:resource>
3535
</framework:form>
3636
</framework:templating>
37-
<framework:translator enabled="true" fallback="fr" logging="true" />
37+
<framework:translator enabled="true" fallback="fr" logging="true">
38+
<framework:path>%kernel.root_dir%/Fixtures/translations</framework:path>
39+
</framework:translator>
3840
<framework:validation enabled="true" cache="apc" />
3941
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
4042
<framework:serializer enabled="true" />

Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ framework:
3939
translator:
4040
enabled: true
4141
fallback: fr
42+
paths: ['%kernel.root_dir%/Fixtures/translations']
4243
validation:
4344
enabled: true
4445
cache: apc

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ public function testTranslator()
244244
$files,
245245
'->registerTranslatorConfiguration() finds Security translation resources'
246246
);
247+
$this->assertContains(
248+
strtr(__DIR__.'/Fixtures/translations/test_paths.en.yml', '/', DIRECTORY_SEPARATOR),
249+
$files,
250+
'->registerTranslatorConfiguration() finds translation resources in custom paths'
251+
);
247252

248253
$calls = $container->getDefinition('translator.default')->getMethodCalls();
249254
$this->assertEquals(array('fr'), $calls[0][1][0]);

0 commit comments

Comments
 (0)