Skip to content

Commit

Permalink
feature #32543 [FrameworkBundle] add config translator cache_dir (Rau…
Browse files Browse the repository at this point in the history
…lnet)

This PR was submitted for the 4.3 branch but it was squashed and merged into the 4.4 branch instead (closes #32543).

Discussion
----------

[FrameworkBundle] add config translator cache_dir

| Q             | A
| ------------- | ---
| Branch?       |  4.4 (be careful when merging)
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32542
| License       | MIT

Now the parameter cache_dir of Translator constructor can be change or disable on config

```yaml
#framework.yaml
framework:
    ...
    translator:
        cache_dir: null #(cache disable)
    ...
```

Commits
-------

7613c7d [FrameworkBundle] add config translator cache_dir
  • Loading branch information
fabpot committed Aug 4, 2019
2 parents 7479543 + 7613c7d commit 35bf2fa
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 2 deletions.
Expand Up @@ -762,6 +762,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->end()
->booleanNode('logging')->defaultValue(false)->end()
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
->scalarNode('cache_dir')->defaultValue('%kernel.cache_dir%/translations')->end()
->scalarNode('default_path')
->info('The default path used to load translations')
->defaultValue('%kernel.project_dir%/translations')
Expand Down
Expand Up @@ -1118,6 +1118,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$translator = $container->findDefinition('translator.default');
$translator->addMethodCall('setFallbackLocales', [$config['fallbacks'] ?: [$defaultLocale]]);

$defaultOptions = $translator->getArgument(4);
$defaultOptions['cache_dir'] = $config['cache_dir'];
$translator->setArgument(4, $defaultOptions);

$container->setParameter('translator.logging', $config['logging']);
$container->setParameter('translator.default_path', $config['default_path']);

Expand Down
Expand Up @@ -186,6 +186,7 @@
<xsd:attribute name="fallback" type="xsd:string" />
<xsd:attribute name="logging" type="xsd:boolean" />
<xsd:attribute name="formatter" type="xsd:string" />
<xsd:attribute name="cache-dir" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="validation">
Expand Down
Expand Up @@ -241,6 +241,7 @@ protected static function getBundleDefaultConfig()
'translator' => [
'enabled' => !class_exists(FullStack::class),
'fallbacks' => [],
'cache_dir' => '%kernel.cache_dir%/translations',
'logging' => false,
'formatter' => 'translator.formatter.default',
'paths' => [],
Expand Down
Expand Up @@ -46,6 +46,7 @@
'enabled' => true,
'fallback' => 'fr',
'paths' => ['%kernel.project_dir%/Fixtures/translations'],
'cache_dir' => '%kernel.cache_dir%/translations',
],
'validation' => [
'enabled' => true,
Expand Down
@@ -0,0 +1,7 @@
<?php

$container->loadFromExtension('framework', [
'translator' => [
'cache_dir' => null,
],
]);
Expand Up @@ -26,7 +26,7 @@
</framework:format>
</framework:request>
<framework:assets version="v1" />
<framework:translator enabled="true" fallback="fr" logging="true">
<framework:translator enabled="true" fallback="fr" logging="true" cache-dir="%kernel.cache_dir%/translations">
<framework:path>%kernel.project_dir%/Fixtures/translations</framework:path>
</framework:translator>
<framework:validation enabled="true" />
Expand Down
@@ -0,0 +1,12 @@
<?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">
<framework:translator enabled="true" fallback="fr" logging="true" cache-dir="null" />
</framework:config>
</container>
Expand Up @@ -36,6 +36,7 @@ framework:
enabled: true
fallback: fr
default_path: '%kernel.project_dir%/translations'
cache_dir: '%kernel.cache_dir%/translations'
paths: ['%kernel.project_dir%/Fixtures/translations']
validation:
enabled: true
Expand Down
@@ -0,0 +1,3 @@
framework:
translator:
cache_dir: ~
Expand Up @@ -51,11 +51,11 @@
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\Util\LegacyTranslatorProxy;
use Symfony\Component\Workflow;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class FrameworkExtensionTest extends TestCase
{
Expand Down Expand Up @@ -783,6 +783,9 @@ public function testTranslator()
$this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator');
$options = $container->getDefinition('translator.default')->getArgument(4);

$this->assertArrayHasKey('cache_dir', $options);
$this->assertSame($container->getParameter('kernel.cache_dir').'/translations', $options['cache_dir']);

$files = array_map('realpath', $options['resource_files']['en']);
$ref = new \ReflectionClass('Symfony\Component\Validator\Validation');
$this->assertContains(
Expand Down Expand Up @@ -853,6 +856,13 @@ public function testTranslatorMultipleFallbacks()
$this->assertEquals(['en', 'fr'], $calls[1][1][0]);
}

public function testTranslatorCacheDirDisabled()
{
$container = $this->createContainerFromFile('translator_cache_dir_disabled');
$options = $container->getDefinition('translator.default')->getArgument(4);
$this->assertNull($options['cache_dir']);
}

/**
* @group legacy
*/
Expand Down

0 comments on commit 35bf2fa

Please sign in to comment.