From 49c5741de03cf08321872ed509cfabd1e72ebde2 Mon Sep 17 00:00:00 2001 From: Mikolaj Adamczyk Date: Fri, 18 Feb 2022 12:49:52 +0100 Subject: [PATCH] IBX-2301: Added configuration to enable translations from `i18n` package (#78) * IBX-2301: Added onfiguration to enable translations from `i18n` package * IBX-1821: Fixed docblock --- .../Compiler/TranslationCollectorPass.php | 13 ++++--- .../DependencyInjection/Configuration.php | 34 +++++++++++++++++ .../IbexaCoreExtension.php | 6 +++ .../Compiler/TranslationCollectorPassTest.php | 28 ++++++++++++-- .../IbexaCoreExtensionTest.php | 38 +++++++++++++++++++ 5 files changed, 111 insertions(+), 8 deletions(-) diff --git a/src/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPass.php b/src/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPass.php index 181ea81e96..5274e55c11 100644 --- a/src/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPass.php +++ b/src/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPass.php @@ -44,12 +44,15 @@ public function process(ContainerBuilder $container) $collector = new GlobCollector($container->getParameterBag()->get('kernel.project_dir')); $availableTranslations = [self::ORIGINAL_TRANSLATION]; - foreach ($collector->collect() as $file) { - /* TODO - to remove when translation files will have proper names. */ - if (isset(self::LOCALES_MAP[$file['locale']])) { - $file['locale'] = self::LOCALES_MAP[$file['locale']]; + + if ($container->getParameter('ibexa.ui.translations.enabled')) { + foreach ($collector->collect() as $file) { + /* TODO - to remove when translation files will have proper names. */ + if (isset(self::LOCALES_MAP[$file['locale']])) { + $file['locale'] = self::LOCALES_MAP[$file['locale']]; + } + $availableTranslations[] = $file['locale']; } - $availableTranslations[] = $file['locale']; } $container->setParameter('available_translations', array_values(array_unique($availableTranslations))); diff --git a/src/bundle/Core/DependencyInjection/Configuration.php b/src/bundle/Core/DependencyInjection/Configuration.php index 2e69b608e6..4b859f673b 100644 --- a/src/bundle/Core/DependencyInjection/Configuration.php +++ b/src/bundle/Core/DependencyInjection/Configuration.php @@ -64,6 +64,7 @@ public function getConfigTreeBuilder() $this->addImagePlaceholderSection($rootNode); $this->addUrlWildcardsSection($rootNode); $this->addOrmSection($rootNode); + $this->addUITranslationsSection($rootNode); // Delegate SiteAccess config to configuration parsers $this->mainSiteAccessConfigParser->addSemanticConfig($this->generateScopeBaseNode($rootNode)); @@ -529,6 +530,39 @@ private function addOrmSection($rootNode): ArrayNodeDefinition ->end() ->end(); } + + /** + * Defines configuration for UI Translations. + * + * The configuration is available at: + * + * ibexa: + * ui: + * translations: + * enabled: true + * + * + * + * @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $rootNode + */ + private function addUITranslationsSection($rootNode): ArrayNodeDefinition + { + return $rootNode + ->children() + ->arrayNode('ui') + ->children() + ->arrayNode('translations') + ->children() + ->booleanNode('enabled') + ->defaultFalse() + ->info('When enabled UI will be translated based on translations from i18n package') + ->end() + ->end() + ->end() + ->end() + ->end() + ->end(); + } } class_alias(Configuration::class, 'eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration'); diff --git a/src/bundle/Core/DependencyInjection/IbexaCoreExtension.php b/src/bundle/Core/DependencyInjection/IbexaCoreExtension.php index e40e287504..1ef01ed5a8 100644 --- a/src/bundle/Core/DependencyInjection/IbexaCoreExtension.php +++ b/src/bundle/Core/DependencyInjection/IbexaCoreExtension.php @@ -131,6 +131,7 @@ public function load(array $configs, ContainerBuilder $container) $this->registerUrlAliasConfiguration($config, $container); $this->registerUrlWildcardsConfiguration($config, $container); $this->registerOrmConfiguration($config, $container); + $this->registerUITranslationsConfiguration($config, $container); // Routing $this->handleRouting($config, $container, $loader); @@ -320,6 +321,11 @@ private function registerOrmConfiguration(array $config, ContainerBuilder $conta $container->setParameter('ibexa.orm.entity_mappings', $entityMappings); } + private function registerUITranslationsConfiguration(array $config, ContainerBuilder $container): void + { + $container->setParameter('ibexa.ui.translations.enabled', $config['ui']['translations']['enabled'] ?? false); + } + /** * Handle routing parameters. * diff --git a/tests/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPassTest.php b/tests/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPassTest.php index 344441d5dc..0493675f98 100644 --- a/tests/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPassTest.php +++ b/tests/bundle/Core/DependencyInjection/Compiler/TranslationCollectorPassTest.php @@ -18,14 +18,20 @@ protected function registerCompilerPass(ContainerBuilder $container): void $container->addCompilerPass(new TranslationCollectorPass()); } - public function testTranslationCollector(): void - { + /** + * @dataProvider translationCollectorProvider + */ + public function testTranslationCollector( + bool $translationsEnabled, + array $availableTranslations + ): void { $this->setDefinition('translator.default', new Definition()); $this->setParameter('kernel.project_dir', __DIR__ . $this->normalizePath('/../Fixtures')); + $this->setParameter('ibexa.ui.translations.enabled', $translationsEnabled); $this->compile(); - $this->assertContainerBuilderHasParameter('available_translations', ['en', 'hi', 'nb']); + $this->assertContainerBuilderHasParameter('available_translations', $availableTranslations); } /** @@ -37,6 +43,22 @@ private function normalizePath($path) { return str_replace('/', \DIRECTORY_SEPARATOR, $path); } + + /** + * @return iterable + */ + public function translationCollectorProvider(): iterable + { + yield 'translations enabled' => [ + true, + ['en', 'hi', 'nb'], + ]; + + yield 'translations disabled' => [ + false, + ['en'], + ]; + } } class_alias(TranslationCollectorPassTest::class, 'eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Compiler\TranslationCollectorPassTest'); diff --git a/tests/bundle/Core/DependencyInjection/IbexaCoreExtensionTest.php b/tests/bundle/Core/DependencyInjection/IbexaCoreExtensionTest.php index 2f740e67ed..99855f4edf 100644 --- a/tests/bundle/Core/DependencyInjection/IbexaCoreExtensionTest.php +++ b/tests/bundle/Core/DependencyInjection/IbexaCoreExtensionTest.php @@ -147,6 +147,44 @@ public function testImageMagickConfigurationBasic() $this->assertContainerBuilderHasParameter('ibexa.image.imagemagick.executable', basename($_ENV['imagemagickConvertPath'])); } + /** + * @dataProvider translationsConfigurationProvider + */ + public function testUITranslationsConfiguration( + bool $enabled, + bool $expectedParameterValue + ): void { + if (is_bool($enabled)) { + $this->load( + [ + 'ui' => [ + 'translations' => [ + 'enabled' => $enabled, + ], + ], + ] + ); + } + + $this->assertContainerBuilderHasParameter('ibexa.ui.translations.enabled', $expectedParameterValue); + } + + /** + * @return iterable + */ + public function translationsConfigurationProvider(): iterable + { + yield 'translations enabled' => [ + true, + true, + ]; + + yield 'translations disabled' => [ + false, + false, + ]; + } + public function testImageMagickConfigurationFilters() { if (!isset($_ENV['imagemagickConvertPath']) || !is_executable($_ENV['imagemagickConvertPath'])) {