Use ipl-i18n's `Translation` trait where applicable

This commit is contained in:
Johannes Meyer 2021-05-20 09:34:50 +02:00
parent cb92e86b99
commit 2e5abbc832
5 changed files with 33 additions and 97 deletions

View File

@ -17,6 +17,7 @@ use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Widget;
use ipl\I18n\GettextTranslator;
use ipl\I18n\StaticTranslator;
use ipl\I18n\Translation;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Zend_Controller_Router_Route;
@ -30,6 +31,11 @@ use Zend_Controller_Router_Route_Regex;
*/
class Module
{
use Translation {
translate as protected;
translatePlural as protected;
}
/**
* Module name
*
@ -301,6 +307,8 @@ class Module
$this->runScript = $basedir . '/run.php';
$this->configScript = $basedir . '/configuration.php';
$this->metadataFile = $basedir . '/module.info';
$this->translationDomain = $name;
}
/**
@ -1635,22 +1643,4 @@ class Module
$this->routes[$name] = $route;
return $this;
}
/**
* (non-PHPDoc)
* @see Translator::translate() For the function documentation.
*/
protected function translate($string, $context = null)
{
return mt($this->name, $string, $context);
}
/**
* (non-PHPDoc)
* @see Translator::translatePlural() For the function documentation.
*/
protected function translatePlural($textSingular, $textPlural, $number, $context = null)
{
return mtp($this->name, $textSingular, $textPlural, $number, $context);
}
}

View File

@ -8,10 +8,12 @@ use Icinga\Application\Config;
use Icinga\Application\Logger;
use Icinga\Exception\IcingaException;
use Icinga\Exception\NotReadableError;
use Icinga\Util\Translator;
use ipl\I18n\Translation;
abstract class Command
{
use Translation;
protected $app;
protected $docs;
@ -61,6 +63,8 @@ abstract class Command
$this->isDebugging = $this->params->shift('debug', false);
$this->configs = [];
$this->translationDomain = $moduleName ?: 'icinga';
if ($this->loadEnabledModules) {
try {
$app->getModuleManager()->loadEnabledModules();
@ -134,21 +138,6 @@ abstract class Command
return $this->trace;
}
/**
* Translate a string
*
* Autoselects the module domain, if any, and falls back to the global one if no translation could be found.
*
* @param string $text The string to translate
*
* @return string The translated string
*/
public function translate($text)
{
$domain = $this->moduleName === null ? 'icinga' : $this->moduleName;
return Translator::translate($text, $domain);
}
public function fail($msg)
{
throw new IcingaException('%s', $msg);

View File

@ -3,6 +3,7 @@
namespace Icinga\Web\Controller;
use ipl\I18n\Translation;
use Zend_Controller_Action;
use Zend_Controller_Action_HelperBroker;
use Zend_Controller_Request_Abstract;
@ -16,7 +17,6 @@ use Icinga\Exception\ProgrammingError;
use Icinga\File\Pdf;
use Icinga\Forms\AutoRefreshForm;
use Icinga\Security\SecurityException;
use Icinga\Util\Translator;
use Icinga\Web\Session;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
@ -40,6 +40,8 @@ use Icinga\Web\Window;
*/
class ActionController extends Zend_Controller_Action
{
use Translation;
/**
* The login route to use when requiring authentication
*/
@ -131,7 +133,8 @@ class ActionController extends Zend_Controller_Action
$moduleName = $this->getModuleName();
$this->view->defaultTitle = static::DEFAULT_TITLE;
$this->view->translationDomain = $moduleName !== 'default' ? $moduleName : 'icinga';
$this->translationDomain = $moduleName !== 'default' ? $moduleName : 'icinga';
$this->view->translationDomain = $this->translationDomain;
$this->_helper->layout()->isIframe = $request->getUrl()->shift('isIframe');
$this->_helper->layout()->showFullscreen = $request->getUrl()->shift('showFullscreen');
$this->_helper->layout()->moduleName = $moduleName;
@ -305,42 +308,6 @@ class ActionController extends Zend_Controller_Action
return $this->view->tabs;
}
/**
* Translate a string
*
* Autoselects the module domain, if any, and falls back to the global one if no translation could be found.
*
* @param string $text The string to translate
* @param string|null $context Optional parameter for context based translation
*
* @return string The translated string
*/
public function translate($text, $context = null)
{
return Translator::translate($text, $this->view->translationDomain, $context);
}
/**
* Translate a plural string
*
* @param string $textSingular The string in singular form to translate
* @param string $textPlural The string in plural form to translate
* @param string $number The number to get the plural or singular string
* @param string|null $context Optional parameter for context based translation
*
* @return string The translated string
*/
public function translatePlural($textSingular, $textPlural, $number, $context = null)
{
return Translator::translatePlural(
$textSingular,
$textPlural,
$number,
$this->view->translationDomain,
$context
);
}
protected function ignoreXhrBody()
{
if ($this->isXhr()) {

View File

@ -4,6 +4,7 @@
namespace Icinga\Web;
use Icinga\Web\Form\Element\DateTimePicker;
use ipl\I18n\Translation;
use Zend_Config;
use Zend_Form;
use Zend_Form_Element;
@ -12,7 +13,6 @@ use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Exception\ProgrammingError;
use Icinga\Security\SecurityException;
use Icinga\Util\Translator;
use Icinga\Web\Form\ErrorLabeller;
use Icinga\Web\Form\Decorator\Autosubmit;
use Icinga\Web\Form\Element\CsrfCounterMeasure;
@ -27,6 +27,11 @@ use Icinga\Web\Form\Element\CsrfCounterMeasure;
*/
class Form extends Zend_Form
{
use Translation {
translate as i18nTranslate;
translatePlural as i18nTranslatePlural;
}
/**
* The suffix to append to a field's hidden default field name
*/
@ -1519,7 +1524,9 @@ class Form extends Zend_Form
*/
protected function translate($text, $context = null)
{
return Translator::translate($text, $this->getTranslationDomain(), $context);
$this->translationDomain = $this->getTranslationDomain();
return $this->i18nTranslate($text, $context);
}
/**
@ -1534,13 +1541,9 @@ class Form extends Zend_Form
*/
protected function translatePlural($textSingular, $textPlural, $number, $context = null)
{
return Translator::translatePlural(
$textSingular,
$textPlural,
$number,
$this->getTranslationDomain(),
$context
);
$this->translationDomain = $this->getTranslationDomain();
return $this->i18nTranslatePlural($textSingular, $textPlural, $number, $context);
}
/**

View File

@ -5,10 +5,10 @@ namespace Icinga\Web;
use Closure;
use Icinga\Application\Icinga;
use ipl\I18n\Translation;
use Zend_View_Abstract;
use Icinga\Authentication\Auth;
use Icinga\Exception\ProgrammingError;
use Icinga\Util\Translator;
/**
* Icinga view
@ -54,6 +54,8 @@ use Icinga\Util\Translator;
*/
class View extends Zend_View_Abstract
{
use Translation;
/**
* Charset to be used - we only support UTF-8
*/
@ -178,21 +180,6 @@ class View extends Zend_View_Abstract
);
}
public function translate($text, $context = null)
{
return Translator::translate($text, $this->translationDomain, $context);
}
/**
* Translate a plural string
*
* @see Translator::translatePlural()
*/
public function translatePlural($textSingular, $textPlural, $number, $context = null)
{
return Translator::translatePlural($textSingular, $textPlural, $number, $this->translationDomain, $context);
}
/**
* Load helpers
*/