mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Created translator and used inside one of the commands
This commit is contained in:
30
module/Common/src/Factory/TranslatorFactory.php
Normal file
30
module/Common/src/Factory/TranslatorFactory.php
Normal 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'] : []);
|
||||
}
|
||||
}
|
||||
75
module/Common/src/Twig/Extension/TranslatorExtension.php
Normal file
75
module/Common/src/Twig/Extension/TranslatorExtension.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user