bootstrap: Setup internationalization using ipl-i18n

This commit is contained in:
Johannes Meyer 2021-05-20 09:09:41 +02:00
parent cc0c6fc71b
commit bfd2449e5d
4 changed files with 41 additions and 5 deletions

View File

@ -6,13 +6,14 @@ namespace Icinga\Application;
use DirectoryIterator; use DirectoryIterator;
use ErrorException; use ErrorException;
use Exception; use Exception;
use ipl\I18n\GettextTranslator;
use ipl\I18n\StaticTranslator;
use LogicException; use LogicException;
use Icinga\Application\Modules\Manager as ModuleManager; use Icinga\Application\Modules\Manager as ModuleManager;
use Icinga\Authentication\User\UserBackend; use Icinga\Authentication\User\UserBackend;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
use Icinga\Util\Translator;
use Icinga\Exception\IcingaException; use Icinga\Exception\IcingaException;
/** /**
@ -699,6 +700,19 @@ abstract class ApplicationBootstrap
return null; return null;
} }
/**
* Prepare internationalization using gettext
*
* @return $this
*/
protected function prepareInternationalization()
{
StaticTranslator::$instance = (new GettextTranslator())
->setDefaultDomain('icinga');
return $this;
}
/** /**
* Set up internationalization using gettext * Set up internationalization using gettext
* *
@ -706,17 +720,20 @@ abstract class ApplicationBootstrap
*/ */
final protected function setupInternationalization() final protected function setupInternationalization()
{ {
/** @var GettextTranslator $translator */
$translator = StaticTranslator::$instance;
if ($this->hasLocales()) { if ($this->hasLocales()) {
Translator::registerDomain(Translator::DEFAULT_DOMAIN, $this->getLocaleDir()); $translator->addTranslationDirectory($this->getLocaleDir(), 'icinga');
} }
$locale = $this->detectLocale(); $locale = $this->detectLocale();
if ($locale === null) { if ($locale === null) {
$locale = Translator::DEFAULT_LOCALE; $locale = $translator->getDefaultLocale();
} }
try { try {
Translator::setupLocale($locale); $translator->setLocale($locale);
} catch (Exception $error) { } catch (Exception $error) {
Logger::error($error); Logger::error($error);
} }

View File

@ -41,6 +41,7 @@ class Cli extends ApplicationBootstrap
->loadLibraries() ->loadLibraries()
->loadConfig() ->loadConfig()
->setupTimezone() ->setupTimezone()
->prepareInternationalization()
->setupInternationalization() ->setupInternationalization()
->parseBasicParams() ->parseBasicParams()
->setupLogger() ->setupLogger()

View File

@ -7,6 +7,8 @@ require_once dirname(__FILE__) . '/ApplicationBootstrap.php';
use Icinga\Web\Request; use Icinga\Web\Request;
use Icinga\Web\Response; use Icinga\Web\Response;
use ipl\I18n\NoopTranslator;
use ipl\I18n\StaticTranslator;
/** /**
* Use this if you want to make use of Icinga functionality in other web projects * Use this if you want to make use of Icinga functionality in other web projects
@ -72,6 +74,7 @@ class EmbeddedWeb extends ApplicationBootstrap
->setupRequest() ->setupRequest()
->setupResponse() ->setupResponse()
->setupTimezone() ->setupTimezone()
->prepareFakeInternationalization()
->setupModuleManager() ->setupModuleManager()
->loadEnabledModules(); ->loadEnabledModules();
} }
@ -97,4 +100,16 @@ class EmbeddedWeb extends ApplicationBootstrap
$this->response = new Response(); $this->response = new Response();
return $this; return $this;
} }
/**
* Prepare fake internationalization
*
* @return $this
*/
protected function prepareFakeInternationalization()
{
StaticTranslator::$instance = new NoopTranslator();
return $this;
}
} }

View File

@ -6,6 +6,9 @@ namespace Icinga\Application;
require_once __DIR__ . '/EmbeddedWeb.php'; require_once __DIR__ . '/EmbeddedWeb.php';
use ErrorException; use ErrorException;
use ipl\I18n\GettextTranslator;
use ipl\I18n\Locale;
use ipl\I18n\StaticTranslator;
use Zend_Controller_Action_HelperBroker; use Zend_Controller_Action_HelperBroker;
use Zend_Controller_Front; use Zend_Controller_Front;
use Zend_Controller_Router_Route; use Zend_Controller_Router_Route;
@ -16,7 +19,6 @@ use Icinga\Authentication\Auth;
use Icinga\User; use Icinga\User;
use Icinga\Util\DirectoryIterator; use Icinga\Util\DirectoryIterator;
use Icinga\Util\TimezoneDetect; use Icinga\Util\TimezoneDetect;
use Icinga\Util\Translator;
use Icinga\Web\Controller\Dispatcher; use Icinga\Web\Controller\Dispatcher;
use Icinga\Web\Menu; use Icinga\Web\Menu;
use Icinga\Web\Navigation\Navigation; use Icinga\Web\Navigation\Navigation;
@ -91,6 +93,7 @@ class Web extends EmbeddedWeb
->setupNotifications() ->setupNotifications()
->setupResponse() ->setupResponse()
->setupZendMvc() ->setupZendMvc()
->prepareInternationalization()
->setupModuleManager() ->setupModuleManager()
->loadSetupModuleIfNecessary() ->loadSetupModuleIfNecessary()
->loadEnabledModules() ->loadEnabledModules()