42 lines
1017 B
PHP
42 lines
1017 B
PHP
<?php
|
|
|
|
namespace KupShop\I18nBundle\Util;
|
|
|
|
use KupShop\I18nBundle\Exception\TranslationException;
|
|
use KupShop\I18nBundle\Translations\BaseTranslation;
|
|
use Symfony\Component\DependencyInjection\ServiceLocator;
|
|
|
|
class TranslationLocator
|
|
{
|
|
private $locator;
|
|
private $servicesList;
|
|
|
|
public function __construct(ServiceLocator $locator, array $servicesList)
|
|
{
|
|
$this->locator = $locator;
|
|
$this->servicesList = $servicesList;
|
|
}
|
|
|
|
public function getTranslations(): array
|
|
{
|
|
$result = [];
|
|
foreach ($this->servicesList as $service) {
|
|
$shortName = explode('\\', $service);
|
|
$shortName = end($shortName);
|
|
|
|
$result[$shortName] = $service;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function getTranslation(string $class): BaseTranslation
|
|
{
|
|
if (!$this->locator->has($class)) {
|
|
throw new TranslationException('Translation class not found');
|
|
}
|
|
|
|
return $this->locator->get($class);
|
|
}
|
|
}
|