Akeneo 2.1获取活跃语言列表方法咨询(含控制台命令场景)
Hey there! Let's break down your two Akeneo 2.1 questions with practical, actionable examples:
In Akeneo 2.1, active locales (languages) are tracked in the pim_catalog_locale table via the activated flag. The simplest way to fetch them is using the built-in locale repository service provided by the platform.
Here's how you'd implement this in a controller or any service with access to the Symfony container:
<?php namespace Your\CustomBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class LocaleController extends Controller { public function getActiveLocalesAction() { // Fetch the pre-configured locale repository from the container $localeRepository = $this->get('pim_catalog.repository.locale'); // Retrieve all locales marked as active $activeLocales = $localeRepository->findBy(['activated' => true]); // If you only need the language codes (like 'en_US', 'fr_FR') $activeLocaleCodes = array_map(function($locale) { return $locale->getCode(); }, $activeLocales); // Use the locales or codes in your business logic // ... } }
The repository returns instances of Akeneo\Pim\Structure\Component\Model\LocaleInterface, so you can also access additional properties like getName() if needed.
For console commands, you have two solid approaches to access the locale repository—choose the one that aligns with your project's coding practices:
Option 1: Using ContainerAwareCommand (quick and compatible)
If you extend ContainerAwareCommand, you can directly pull the repository from the container:
<?php namespace Your\CustomBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class ListActiveLocalesCommand extends ContainerAwareCommand { protected function configure() { $this ->setName('app:list-active-locales') ->setDescription('Prints all active Akeneo locales'); } protected function execute(InputInterface $input, OutputInterface $output) { $localeRepository = $this->getContainer()->get('pim_catalog.repository.locale'); $activeLocales = $localeRepository->findBy(['activated' => true]); $activeLocaleCodes = array_map(function($locale) { return $locale->getCode(); }, $activeLocales); $output->writeln('Active locales: ' . implode(', ', $activeLocaleCodes)); } }
Option 2: Constructor Injection (modern best practice)
Since Akeneo 2.1 runs on Symfony 3.4, constructor injection is supported and recommended for cleaner dependency management. First, define your command:
<?php namespace Your\CustomBundle\Command; use Akeneo\Tool\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class ListActiveLocalesCommand extends Command { /** @var IdentifiableObjectRepositoryInterface */ private $localeRepository; public function __construct(IdentifiableObjectRepositoryInterface $localeRepository) { parent::__construct(); $this->localeRepository = $localeRepository; } protected function configure() { $this ->setName('app:list-active-locales') ->setDescription('Prints all active Akeneo locales'); } protected function execute(InputInterface $input, OutputInterface $output) { $activeLocales = $this->localeRepository->findBy(['activated' => true]); $activeLocaleCodes = array_map(function($locale) { return $locale->getCode(); }, $activeLocales); $output->writeln('Active locales: ' . implode(', ', $activeLocaleCodes)); } }
Then register the command in your services.yml to inject the repository:
services: your.custom_bundle.command.list_active_locales: class: Your\CustomBundle\Command\ListActiveLocalesCommand arguments: - '@pim_catalog.repository.locale' tags: - { name: console.command }
Both methods will give you the same result—pick the one that fits your team's workflow!
内容的提问来源于stack exchange,提问作者Giel Berkers

