feature #24860 [FrameworkBundle] Add default translations path option and convention (yceruto)

This PR was merged into the 3.4 branch.

Discussion
----------

[FrameworkBundle] Add default translations path option and convention

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | ToDo

Similar to Twig default path, this proposal adds a `default_path` option under `translator` config:
```yaml
framework:
    translator:
        default_path: '%kernel.project_dir%/config/translations'
```
adding this default path to the discovered translations dirs. Thus, overriding bundle translations is possible by using this new convention: `config/translations/<BundleName>/messages.en.xlf`.

Also a new container parameter `%translator.default_path%` is defined by external purpose (similar to https://github.com/symfony/symfony/pull/24840)

Note: The current convention `%kernel.root_dir%/Resources/translations` path has priority over the new one.

TODO:
- [x] Add more tests about the new path
- [x] Update changelog

Commits
-------

1a8b1b41c9 Add default translations path option and convention
This commit is contained in:
Fabien Potencier 2017-11-11 08:26:39 -08:00
commit b0ce1c13b1
7 changed files with 20 additions and 0 deletions

View File

@ -4,6 +4,7 @@ CHANGELOG
3.4.0
-----
* Added `translator.default_path` option and parameter
* Session `use_strict_mode` is now enabled by default and the corresponding option has been deprecated
* Made the `cache:clear` command to *not* clear "app" PSR-6 cache pools anymore,
but to still clear "system" ones; use the `cache:pool:clear` command to clear "app" pools instead

View File

@ -677,6 +677,10 @@ class Configuration implements ConfigurationInterface
->end()
->booleanNode('logging')->defaultValue($this->debug)->end()
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
->scalarNode('default_path')
->info('The default path used to load translations')
->defaultValue('%kernel.project_dir%/config/translations')
->end()
->arrayNode('paths')
->prototype('scalar')->end()
->end()

View File

@ -1131,6 +1131,7 @@ class FrameworkExtension extends Extension
$translator->addMethodCall('setFallbackLocales', array($config['fallbacks']));
$container->setParameter('translator.logging', $config['logging']);
$container->setParameter('translator.default_path', $config['default_path']);
// Discover translation directories
$dirs = array();
@ -1149,11 +1150,15 @@ class FrameworkExtension extends Extension
$dirs[] = dirname(dirname($r->getFileName())).'/Resources/translations';
}
$defaultDir = $container->getParameterBag()->resolveValue($config['default_path']);
$rootDir = $container->getParameter('kernel.root_dir');
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if ($container->fileExists($dir = $bundle['path'].'/Resources/translations')) {
$dirs[] = $dir;
}
if ($container->fileExists($dir = $defaultDir.'/'.$name)) {
$dirs[] = $dir;
}
if ($container->fileExists($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
$dirs[] = $dir;
}
@ -1167,6 +1172,9 @@ class FrameworkExtension extends Extension
}
}
if ($container->fileExists($defaultDir)) {
$dirs[] = $defaultDir;
}
if ($container->fileExists($dir = $rootDir.'/Resources/translations')) {
$dirs[] = $dir;
}

View File

@ -257,6 +257,7 @@ class ConfigurationTest extends TestCase
'logging' => true,
'formatter' => 'translator.formatter.default',
'paths' => array(),
'default_path' => '%kernel.project_dir%/config/translations',
),
'validation' => array(
'enabled' => !class_exists(FullStack::class),

View File

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

View File

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