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:
Johannes Meyer 2014-06-11 13:39:22 +02:00
parent fa797de05f
commit 159d765f14
5 changed files with 26 additions and 10 deletions

View File

@ -30,7 +30,6 @@
namespace Icinga\Application;
use DateTimeZone;
use Exception;
use Zend_Config;
use Icinga\Application\Modules\Manager as ModuleManager;
@ -484,7 +483,7 @@ abstract class ApplicationBootstrap
$localeDir = $this->getApplicationDir('locale');
if (file_exists($localeDir) && is_dir($localeDir)) {
Translator::registerDomain('icinga', $localeDir);
Translator::registerDomain(Translator::DEFAULT_DOMAIN, $localeDir);
}
return $this;

View File

@ -32,7 +32,7 @@ use \Icinga\Util\Translator;
if (extension_loaded('gettext')) {
function t($messageId)
{
return Translator::translate($messageId, 'icinga');
return Translator::translate($messageId, Translator::DEFAULT_DOMAIN);
}
function mt($domain, $messageId)

View File

@ -62,15 +62,9 @@ class Translator
* @param string $domain The primary domain to use
*
* @return string The translated string
*
* @throws Exception In case the given domain is unknown
*/
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);
if ($res === $text && $domain !== self::DEFAULT_DOMAIN) {
return dgettext(self::DEFAULT_DOMAIN, $text);

View File

@ -301,7 +301,7 @@ class ActionController extends Zend_Controller_Action
public function translate($text)
{
$module = $this->getRequest()->getModuleName();
$domain = $module === 'default' ? 'icinga' : $module;
$domain = $module === 'default' ? Translator::DEFAULT_DOMAIN : $module;
return Translator::translate($text, $domain);
}

View 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'));
}
}