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

View File

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

View File

@ -7,6 +7,8 @@ require_once dirname(__FILE__) . '/ApplicationBootstrap.php';
use Icinga\Web\Request;
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
@ -72,6 +74,7 @@ class EmbeddedWeb extends ApplicationBootstrap
->setupRequest()
->setupResponse()
->setupTimezone()
->prepareFakeInternationalization()
->setupModuleManager()
->loadEnabledModules();
}
@ -97,4 +100,16 @@ class EmbeddedWeb extends ApplicationBootstrap
$this->response = new Response();
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';
use ErrorException;
use ipl\I18n\GettextTranslator;
use ipl\I18n\Locale;
use ipl\I18n\StaticTranslator;
use Zend_Controller_Action_HelperBroker;
use Zend_Controller_Front;
use Zend_Controller_Router_Route;
@ -16,7 +19,6 @@ use Icinga\Authentication\Auth;
use Icinga\User;
use Icinga\Util\DirectoryIterator;
use Icinga\Util\TimezoneDetect;
use Icinga\Util\Translator;
use Icinga\Web\Controller\Dispatcher;
use Icinga\Web\Menu;
use Icinga\Web\Navigation\Navigation;
@ -91,6 +93,7 @@ class Web extends EmbeddedWeb
->setupNotifications()
->setupResponse()
->setupZendMvc()
->prepareInternationalization()
->setupModuleManager()
->loadSetupModuleIfNecessary()
->loadEnabledModules()