Created translator and used inside one of the commands

This commit is contained in:
Alejandro Celaya
2016-07-21 15:08:46 +02:00
parent b413b16c86
commit cb99130c1e
11 changed files with 219 additions and 16 deletions

View File

@@ -0,0 +1,30 @@
<?php
namespace Shlinkio\Shlink\Common\Factory;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class TranslatorFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('config');
return Translator::factory(isset($config['translator']) ? $config['translator'] : []);
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Shlinkio\Shlink\Common\Twig\Extension;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Zend\I18n\Translator\TranslatorInterface;
class TranslatorExtension extends \Twig_Extension implements TranslatorInterface
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* TranslatorExtension constructor.
* @param TranslatorInterface $translator
*
* @Inject({"translator"})
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return __CLASS__;
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('translate', [$this, 'translate']),
new \Twig_SimpleFunction('translate_plural', [$this, 'translatePlural']),
];
}
/**
* Translate a message.
*
* @param string $message
* @param string $textDomain
* @param string $locale
* @return string
*/
public function translate($message, $textDomain = 'default', $locale = null)
{
return $this->translator->translate($message, $textDomain, $locale);
}
/**
* Translate a plural message.
*
* @param string $singular
* @param string $plural
* @param int $number
* @param string $textDomain
* @param string|null $locale
* @return string
*/
public function translatePlural(
$singular,
$plural,
$number,
$textDomain = 'default',
$locale = null
) {
$this->translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
}
}