Skip to content
Snippets Groups Projects
Commit 1d8c8223 authored by Demian Katz's avatar Demian Katz Committed by Robert Lange
Browse files

Eliminate static translator factory.

parent 0a912a6a
No related merge requests found
......@@ -393,7 +393,7 @@ $config = [
'VuFindHttp\HttpService' => 'VuFind\Service\Factory::getHttp',
'VuFindSearch\Service' => 'VuFind\Service\Factory::getSearchService',
'Zend\Db\Adapter\Adapter' => 'VuFind\Service\Factory::getDbAdapter',
'Zend\Mvc\I18n\Translator' => 'VuFind\Service\Factory::getTranslator',
'Zend\Mvc\I18n\Translator' => 'VuFind\I18n\Translator\TranslatorFactory',
'Zend\Session\SessionManager' => 'VuFind\Session\ManagerFactory',
],
'initializers' => [
......
<?php
/**
* Translator factory.
*
* PHP version 7
*
* Copyright (C) Villanova University 2019.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Translator
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
namespace VuFind\I18n\Translator;
use Interop\Container\ContainerInterface;
/**
* Translator factory.
*
* @category VuFind
* @package Translator
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
class TranslatorFactory extends \Zend\Mvc\I18n\TranslatorFactory
{
/**
* Create an object
*
* @param ContainerInterface $container Service manager
* @param string $requestedName Service being created
* @param null|array $options Extra options (optional)
*
* @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
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(ContainerInterface $container, $requestedName,
array $options = null
) {
$translator = parent::__invoke($container, $requestedName, $options);
// Set up the ExtendedIni plugin:
$config = $container->get(\VuFind\Config\PluginManager::class)
->get('config');
$pathStack = [
APPLICATION_PATH . '/languages',
LOCAL_OVERRIDE_DIR . '/languages'
];
$fallbackLocales = $config->Site->language == 'en'
? 'en'
: [$config->Site->language, 'en'];
try {
$pm = $translator->getPluginManager();
} catch (\Zend\Mvc\I18n\Exception\BadMethodCallException $ex) {
// If getPluginManager is missing, this means that the user has
// disabled translation in module.config.php or PHP's intl extension
// is missing. We can do no further configuration of the object.
return $translator;
}
$pm->setService(
'ExtendedIni', new Loader\ExtendedIni($pathStack, $fallbackLocales)
);
// Set up language caching for better performance:
try {
$translator->setCache(
$container->get(\VuFind\Cache\Manager::class)->getCache('language')
);
} catch (\Exception $e) {
// Don't let a cache failure kill the whole application, but make
// note of it:
$logger = $container->get(\VuFind\Log\Logger::class);
$logger->debug(
'Problem loading cache: ' . get_class($e) . ' exception: '
. $e->getMessage()
);
}
return $translator;
}
}
......@@ -111,58 +111,4 @@ class Factory
new \Zend\EventManager\EventManager($sm->get('SharedEventManager'))
);
}
/**
* Construct the translator.
*
* @param ServiceManager $sm Service manager.
*
* @return \Zend\Mvc\I18n\Translator
*/
public static function getTranslator(ServiceManager $sm)
{
$factory = new \Zend\Mvc\I18n\TranslatorFactory();
$translator = $factory->createService($sm);
// Set up the ExtendedIni plugin:
$config = $sm->get('VuFind\Config\PluginManager')->get('config');
$pathStack = [
APPLICATION_PATH . '/languages',
LOCAL_OVERRIDE_DIR . '/languages'
];
$fallbackLocales = $config->Site->language == 'en'
? 'en'
: [$config->Site->language, 'en'];
try {
$pm = $translator->getPluginManager();
} catch (\Zend\Mvc\I18n\Exception\BadMethodCallException $ex) {
// If getPluginManager is missing, this means that the user has
// disabled translation in module.config.php or PHP's intl extension
// is missing. We can do no further configuration of the object.
return $translator;
}
$pm->setService(
'ExtendedIni',
new \VuFind\I18n\Translator\Loader\ExtendedIni(
$pathStack, $fallbackLocales
)
);
// Set up language caching for better performance:
try {
$translator->setCache(
$sm->get('VuFind\Cache\Manager')->getCache('language')
);
} catch (\Exception $e) {
// Don't let a cache failure kill the whole application, but make
// note of it:
$logger = $sm->get('VuFind\Log\Logger');
$logger->debug(
'Problem loading cache: ' . get_class($e) . ' exception: '
. $e->getMessage()
);
}
return $translator;
}
}
......@@ -28,6 +28,7 @@
namespace VuFind\View\Helper\Root;
use Interop\Container\ContainerInterface;
use Zend\Mvc\I18n\Translator;
use Zend\ServiceManager\Factory\FactoryInterface;
/**
......@@ -64,8 +65,7 @@ class DisplayLanguageOptionFactory implements FactoryInterface
// We want to construct a separate translator instance for this helper,
// since it configures different language/locale than the core shared
// instance!
return new $requestedName(
\VuFind\Service\Factory::getTranslator($container)
);
$factory = new \VuFind\I18n\Translator\TranslatorFactory();
return new $requestedName($factory($container, Translator::class));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment