Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Command/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$bundleName = $this->input->getArgument('bundle');
if ($bundleName) {
$bundle = $this->getApplication()->getKernel()->getBundle($bundleName);

if (Kernel::VERSION_ID < 40000 && null !== $bundle->getParent()) {
// due to symfony's bundle inheritance if a bundle has a parent it is fetched first.
// so we tell getBundle to NOT fetch the first if a parent is present
$bundles = $this->getApplication()->getKernel()->getBundle($bundle->getParent(), false);
$bundle = $bundles[1];
$this->output->writeln('<info>Using: ' . $bundle->getName() . ' as bundle to lookup translations files for.');
}

$this->importBundleTranslationFiles($bundle, $locales, $domains, (bool)$this->input->getOption('globals'));

} else {
if (!$this->input->getOption('import-path')) {

Expand Down
25 changes: 8 additions & 17 deletions DependencyInjection/Compiler/TranslatorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Kernel;

/**
* Translator compiler pass to automatically pass loader to the other services.
Expand Down Expand Up @@ -50,24 +49,16 @@ public function process(ContainerBuilder $container): void

if ($translatorId && $container->hasDefinition($translatorId)) {
$translatorDef = $container->findDefinition($translatorId);

if (Kernel::VERSION_ID >= 30300) {
$serviceRefs = [...$loadersReferencesById, ...['event_dispatcher' => new Reference('event_dispatcher')]];

// Use named arguments if available, otherwise use numeric indices
if ($translatorDef->getArguments() && array_key_exists('$container', $translatorDef->getArguments())) {
$translatorDef->replaceArgument('$container', ServiceLocatorTagPass::register($container, $serviceRefs));
$translatorDef->replaceArgument('$loaderIds', $loaders);
} else {
$translatorDef->replaceArgument(0, ServiceLocatorTagPass::register($container, $serviceRefs));
$translatorDef->replaceArgument(3, $loaders);
}
$serviceRefs = [...$loadersReferencesById, ...['event_dispatcher' => new Reference('event_dispatcher')]];

// Use named arguments if available, otherwise use numeric indices
if ($translatorDef->getArguments() && array_key_exists('$container', $translatorDef->getArguments())) {
$translatorDef->replaceArgument('$container', ServiceLocatorTagPass::register($container, $serviceRefs));
$translatorDef->replaceArgument('$loaderIds', $loaders);
} else {
if ($translatorDef->getArguments() && array_key_exists('$loaderIds', $translatorDef->getArguments())) {
$translatorDef->replaceArgument('$loaderIds', $loaders);
} else {
$translatorDef->replaceArgument(2, $loaders);
}
$translatorDef->replaceArgument(0, ServiceLocatorTagPass::register($container, $serviceRefs));
$translatorDef->replaceArgument(3, $loaders);
}
}

Expand Down
61 changes: 8 additions & 53 deletions DependencyInjection/LexikTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Kernel;

Expand Down Expand Up @@ -63,9 +62,6 @@ public function load(array $configs, ContainerBuilder $container): void

$objectManager = $config['storage']['object_manager'] ?? null;

// Translator is now handled via service decoration in services.yaml
// buildTranslatorDefinition() is deprecated and no longer needed for Symfony 8
// $this->buildTranslatorDefinition($container);
$this->buildTranslationStorageDefinition($container, $config['storage']['type'], $objectManager);

if (true === $config['auto_cache_clean']) {
Expand All @@ -79,44 +75,6 @@ public function load(array $configs, ContainerBuilder $container): void
$this->registerTranslatorConfiguration($config, $container);
}

public function buildTranslatorDefinition(ContainerBuilder $container)
{
$translator = new Definition();
$translator->setClass('%lexik_translation.translator.class%');

if (Kernel::VERSION_ID >= 30400) {
$arguments = [
new Reference('service_container'), // Will be replaced by service locator
new Reference('translator.formatter.default'),
new Parameter('kernel.default_locale'),
[], // translation loaders
new Parameter('lexik_translation.translator.options')
];
$translator->setPublic(true);
} elseif (Kernel::VERSION_ID >= 30300) {
$arguments = [
new Reference('service_container'), // Will be replaced by service locator
new Reference('translator.selector'),
new Parameter('kernel.default_locale'),
[], // translation loaders
new Parameter('lexik_translation.translator.options')
];
} else {
$arguments = [
new Reference('service_container'),
new Reference('translator.selector'),
[], // translation loaders
new Parameter('lexik_translation.translator.options')
];
}

$translator->setArguments($arguments);
$translator->addMethodCall('setConfigCacheFactory', [new Reference('config_cache_factory')]);
$translator->addTag('kernel.locale_aware');

$container->setDefinition('lexik_translation.translator', $translator);
}

/**
* @param int $cacheInterval
*/
Expand Down Expand Up @@ -167,7 +125,7 @@ protected function buildTranslationStorageDefinition(ContainerBuilder $container

// Create XML driver for backward compatibility
$this->createDoctrineMappingDriver($container, 'lexik_translation.orm.metadata.xml', '%doctrine.orm.metadata.xml.class%');

// Create attribute driver for models (MappedSuperclass) that now use PHP attributes
$this->createDoctrineAttributeDriver($container, 'lexik_translation.orm.metadata.attribute');

Expand Down Expand Up @@ -208,7 +166,7 @@ protected function buildTranslationStorageDefinition(ContainerBuilder $container
protected function createDoctrineMappingDriver(ContainerBuilder $container, $driverId, $driverClass)
{
$driverDefinition = new Definition($driverClass, [
[realpath(__DIR__.'/../Resources/config/model') => 'Lexik\Bundle\TranslationBundle\Model'],
[dirname(__DIR__) . '/Resources/config/model' => 'Lexik\Bundle\TranslationBundle\Model'],
SimplifiedXmlDriver::DEFAULT_FILE_EXTENSION, true
]);
$driverDefinition->setPublic(false);
Expand All @@ -229,20 +187,20 @@ protected function createDoctrineAttributeDriver(ContainerBuilder $container, $d
$bundleReflection = new \ReflectionClass(\Lexik\Bundle\TranslationBundle\LexikTranslationBundle::class);
$bundleDir = dirname($bundleReflection->getFileName());
$modelPath = $bundleDir . '/Model';

// Try to get realpath, but use the calculated path if it fails
$realModelPath = realpath($modelPath);
if ($realModelPath) {
$modelPath = $realModelPath;
}

// AttributeDriver constructor expects an array of paths (directories to scan)
// It will automatically detect classes with #[ORM\MappedSuperclass] or #[ORM\Entity] attributes
$driverDefinition = new Definition(AttributeDriver::class, [
[$modelPath]
]);
$driverDefinition->setPublic(false);

// Always set/override the definition to ensure it exists with correct arguments
$container->setDefinition($driverId, $driverDefinition);
}
Expand Down Expand Up @@ -270,19 +228,16 @@ protected function registerTranslatorConfiguration(array $config, ContainerBuild
{
// use the Lexik translator decorator as default translator service
$alias = $container->setAlias('translator', 'lexik_translation.translator');

if (Kernel::VERSION_ID >= 30400) {
$alias->setPublic(true);
}
$alias->setPublic(true);

// Get the inner translator (the actual Symfony translator) for adding resources
// The decorator will delegate to it
$innerTranslator = $container->hasDefinition('lexik_translation.translator.inner')
? $container->findDefinition('lexik_translation.translator.inner')
: $container->findDefinition('translator');

$innerTranslator->addMethodCall('setFallbackLocales', [$config['fallback_locale']]);

// For adding file resources, we'll add them to the inner translator
$translator = $innerTranslator;

Expand Down
28 changes: 13 additions & 15 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="lexik_translation.translator.class">Lexik\Bundle\TranslationBundle\Translation\Translator</parameter>
<parameter key="lexik_translation.loader.database.class">Lexik\Bundle\TranslationBundle\Translation\Loader\DatabaseLoader</parameter>
<parameter key="lexik_translation.trans_unit.manager.class">Lexik\Bundle\TranslationBundle\Manager\TransUnitManager</parameter>
<parameter key="lexik_translation.file.manager.class">Lexik\Bundle\TranslationBundle\Manager\FileManager</parameter>
Expand Down Expand Up @@ -48,29 +47,28 @@

<parameter key="lexik_translation.importer.case_insensitive">false</parameter>
<parameter key="lexik_translation.token_finder.limit">15</parameter>

<parameter key="lexik_translation.translator.options" type="collection">
<parameter key="cache_dir">%kernel.cache_dir%/translations</parameter>
<parameter key="debug">%kernel.debug%</parameter>
<parameter key="resources_type">%lexik_translation.resources_type%</parameter>
</parameter>
</parameters>

<services>
<service id="Lexik\Bundle\TranslationBundle\Translation\Translator">
<argument key="$container" type="service" id="service_container" />
<argument key="$formatter" type="service" id="translator.formatter.default" />
<argument key="$defaultLocale" id="%kernel.default_locale%" />
<argument key="$loaderIds" type="collection"></argument>
<argument key="$options" type="collection" >
<argument key="cache_dir">%kernel.cache_dir%</argument>
<service id="lexik_translation.translator" class="Lexik\Bundle\TranslationBundle\Translation\Translator" public="true">
<argument type="service" id="service_container" />
<argument type="service" id="translator.formatter.default" />
<argument id="%kernel.default_locale%" />
<argument type="collection" />
<argument type="collection" >
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
<argument key="debug">%kernel.debug%</argument>
<argument key="resource_files" type="collection" />
<argument key="scanned_directories" type="collection" />
<argument key="cache_vary" type="collection" />
<argument key="resources_type">%lexik_translation.resources_type%</argument>
</argument>
<call method="setConfigCacheFactory">
<argument type="service" id="config_cache_factory" />
</call>
<tag name="kernel.locale_aware" />
</service>
<service id="lexik_translation.translator" alias="Lexik\Bundle\TranslationBundle\Translation\Translator" public="true"/>
<service id="Lexik\Bundle\TranslationBundle\Translation\Translator" alias="lexik_translation.translator" public="true"/>
<service id="Lexik\Bundle\TranslationBundle\Storage\StorageInterface" alias="lexik_translation.translation_storage" />

<!-- Loader -->
Expand Down
20 changes: 11 additions & 9 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,34 @@ parameters:
lexik_translation.importer.case_insensitive: false
lexik_translation.token_finder.limit: 15

lexik_translation.translator.options:
cache_dir: '%kernel.cache_dir%/translations'
debug: '%kernel.debug%'
resources_type: '%lexik_translation.resources_type%'

services:
_defaults:
autowire: true
autoconfigure: true

# Translator
Lexik\Bundle\TranslationBundle\Translation\Translator:
lexik_translation.translator:
class: Lexik\Bundle\TranslationBundle\Translation\Translator
arguments:
$container: '@service_container'
$formatter: '@translator.formatter.default'
$defaultLocale: '%kernel.default_locale%'
$loaderIds: []
$options:
cache_dir: '%kernel.cache_dir%'
cache_dir: '%kernel.cache_dir%/translations'
debug: '%kernel.debug%'
resource_files: []
scanned_directories: []
cache_vary: []
resources_type: '%lexik_translation.resources_type%'
calls:
- [setConfigCacheFactory, ['@config_cache_factory']]
tags:
- { name: kernel.locale_aware }
public: true

lexik_translation.translator:
alias: Lexik\Bundle\TranslationBundle\Translation\Translator
Lexik\Bundle\TranslationBundle\Translation\Translator:
alias: lexik_translation.translator
public: true

# TranslatorDecorator alias for backward compatibility
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@
"sort-packages": true
}
}