mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-27 07:44:04 +02:00
Fix that calling ActionController::translate() throws an exception
Translating strings must not throw an exception even if the given domain is not valid. fixes #6432
This commit is contained in:
parent
fa797de05f
commit
159d765f14
@ -30,7 +30,6 @@
|
|||||||
|
|
||||||
namespace Icinga\Application;
|
namespace Icinga\Application;
|
||||||
|
|
||||||
use DateTimeZone;
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Zend_Config;
|
use Zend_Config;
|
||||||
use Icinga\Application\Modules\Manager as ModuleManager;
|
use Icinga\Application\Modules\Manager as ModuleManager;
|
||||||
@ -484,7 +483,7 @@ abstract class ApplicationBootstrap
|
|||||||
|
|
||||||
$localeDir = $this->getApplicationDir('locale');
|
$localeDir = $this->getApplicationDir('locale');
|
||||||
if (file_exists($localeDir) && is_dir($localeDir)) {
|
if (file_exists($localeDir) && is_dir($localeDir)) {
|
||||||
Translator::registerDomain('icinga', $localeDir);
|
Translator::registerDomain(Translator::DEFAULT_DOMAIN, $localeDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -32,7 +32,7 @@ use \Icinga\Util\Translator;
|
|||||||
if (extension_loaded('gettext')) {
|
if (extension_loaded('gettext')) {
|
||||||
function t($messageId)
|
function t($messageId)
|
||||||
{
|
{
|
||||||
return Translator::translate($messageId, 'icinga');
|
return Translator::translate($messageId, Translator::DEFAULT_DOMAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
function mt($domain, $messageId)
|
function mt($domain, $messageId)
|
||||||
|
@ -62,15 +62,9 @@ class Translator
|
|||||||
* @param string $domain The primary domain to use
|
* @param string $domain The primary domain to use
|
||||||
*
|
*
|
||||||
* @return string The translated string
|
* @return string The translated string
|
||||||
*
|
|
||||||
* @throws Exception In case the given domain is unknown
|
|
||||||
*/
|
*/
|
||||||
public static function translate($text, $domain)
|
public static function translate($text, $domain)
|
||||||
{
|
{
|
||||||
if ($domain !== self::DEFAULT_DOMAIN && !array_key_exists($domain, self::$knownDomains)) {
|
|
||||||
throw new Exception("Cannot translate string '$text' with unknown domain '$domain'");
|
|
||||||
}
|
|
||||||
|
|
||||||
$res = dgettext($domain, $text);
|
$res = dgettext($domain, $text);
|
||||||
if ($res === $text && $domain !== self::DEFAULT_DOMAIN) {
|
if ($res === $text && $domain !== self::DEFAULT_DOMAIN) {
|
||||||
return dgettext(self::DEFAULT_DOMAIN, $text);
|
return dgettext(self::DEFAULT_DOMAIN, $text);
|
||||||
|
@ -301,7 +301,7 @@ class ActionController extends Zend_Controller_Action
|
|||||||
public function translate($text)
|
public function translate($text)
|
||||||
{
|
{
|
||||||
$module = $this->getRequest()->getModuleName();
|
$module = $this->getRequest()->getModuleName();
|
||||||
$domain = $module === 'default' ? 'icinga' : $module;
|
$domain = $module === 'default' ? Translator::DEFAULT_DOMAIN : $module;
|
||||||
return Translator::translate($text, $domain);
|
return Translator::translate($text, $domain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
test/php/regression/Bug6432Test.php
Normal file
23
test/php/regression/Bug6432Test.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Tests\Icinga\Regression;
|
||||||
|
|
||||||
|
use Icinga\Test\BaseTestCase;
|
||||||
|
use Icinga\Util\Translator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regression-Test for bug #6432
|
||||||
|
*
|
||||||
|
* Translating strings must not throw an exception even if the given domain is not valid.
|
||||||
|
*
|
||||||
|
* @see https://dev.icinga.org/issues/6432
|
||||||
|
*/
|
||||||
|
class Bug6432Test extends BaseTestCase
|
||||||
|
{
|
||||||
|
public function testWhetherTranslateReturnsTheInputStringInCaseTheGivenDomainIsNotValid()
|
||||||
|
{
|
||||||
|
$this->assertEquals('test', Translator::translate('test', 'invalid_domain'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user