feature #14561 [FrameworkBundle][DX] Add option to specify additional translation loading paths (Seldaek)

This PR was squashed before being merged into the 2.8 branch (closes #14561).

Discussion
----------

[FrameworkBundle][DX] Add option to specify additional translation loading paths

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #971
| License       | MIT
| Doc PR        | TODO

Thanks to this PR, next time I want to just do:

```
    translator:
        fallback: "%locale%"
        paths: ['%kernel.root_dir%/../vendor/internal/package/translations']
```

I will be able to, and won't have to dig in docs, start building a translation loader before I realize it's not what I want, dig in the framework ext, and then have to write a compiler pass to inject my file to the translator resource paths.

Commits
-------

bba0a25 [FrameworkBundle][DX] Add option to specify additional translation loading paths
This commit is contained in:
Abdellatif Ait boudad 2015-05-06 20:51:24 +00:00
commit 88b7e7ce59
9 changed files with 25 additions and 1 deletions

View File

@ -575,6 +575,7 @@ class Configuration implements ConfigurationInterface
->info('translator configuration')
->canBeEnabled()
->fixXmlConfig('fallback')
->fixXmlConfig('path')
->children()
->arrayNode('fallbacks')
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
@ -582,6 +583,9 @@ class Configuration implements ConfigurationInterface
->defaultValue(array('en'))
->end()
->booleanNode('logging')->defaultValue($this->debug)->end()
->arrayNode('paths')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()

View File

@ -692,6 +692,13 @@ class FrameworkExtension extends Extension
$dirs[] = $dir;
}
}
foreach ($config['paths'] as $dir) {
if (is_dir($dir)) {
$dirs[] = $dir;
} else {
throw new \UnexpectedValueException(sprintf('%s defined in translator.paths does not exist or is not a directory', $dir));
}
}
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/translations')) {
$dirs[] = $dir;
}

View File

@ -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" />

View File

@ -146,6 +146,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
'enabled' => false,
'fallbacks' => array('en'),
'logging' => true,
'paths' => array(),
),
'validation' => array(
'enabled' => false,

View File

@ -50,6 +50,7 @@ $container->loadFromExtension('framework', array(
'translator' => array(
'enabled' => true,
'fallback' => 'fr',
'paths' => array('%kernel.root_dir%/Fixtures/translations'),
),
'validation' => array(
'enabled' => true,

View File

@ -34,7 +34,9 @@
<framework:resource>theme2</framework:resource>
</framework:form>
</framework:templating>
<framework:translator enabled="true" fallback="fr" logging="true" />
<framework:translator enabled="true" fallback="fr" logging="true">
<framework:path>%kernel.root_dir%/Fixtures/translations</framework:path>
</framework:translator>
<framework:validation enabled="true" cache="apc" />
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
<framework:serializer enabled="true" />

View File

@ -39,6 +39,7 @@ framework:
translator:
enabled: true
fallback: fr
paths: ['%kernel.root_dir%/Fixtures/translations']
validation:
enabled: true
cache: apc

View File

@ -244,6 +244,11 @@ abstract class FrameworkExtensionTest extends TestCase
$files,
'->registerTranslatorConfiguration() finds Security translation resources'
);
$this->assertContains(
strtr(__DIR__.'/Fixtures/translations/test_paths.en.yml', '/', DIRECTORY_SEPARATOR),
$files,
'->registerTranslatorConfiguration() finds translation resources in custom paths'
);
$calls = $container->getDefinition('translator.default')->getMethodCalls();
$this->assertEquals(array('fr'), $calls[0][1][0]);