From 17b25388b32b063534935a01d1b43e7ee13b8bb5 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 27 Jul 2013 21:41:22 -0400 Subject: [PATCH] Throw exceptions when '' is used as translation domain. '' is never a good translation domain and often indicates developer error. Treat it as a mistake and throw an exception. Fixes #3939 --- lib/Cake/I18n/I18n.php | 9 +++++++-- lib/Cake/Test/Case/I18n/I18nTest.php | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/Cake/I18n/I18n.php b/lib/Cake/I18n/I18n.php index f81a11d3a..04a8ae795 100644 --- a/lib/Cake/I18n/I18n.php +++ b/lib/Cake/I18n/I18n.php @@ -124,12 +124,14 @@ class I18n { * * @param string $singular String to translate * @param string $plural Plural string (if any) - * @param string $domain Domain The domain of the translation. Domains are often used by plugin translations + * @param string $domain Domain The domain of the translation. Domains are often used by plugin translations. + * If null, the default domain will be used. * @param string $category Category The integer value of the category to use. * @param integer $count Count Count is used with $plural to choose the correct plural form. * @param string $language Language to translate string to. - * If null it checks for language in session followed by Config.language configuration variable. + * If null it checks for language in session followed by Config.language configuration variable. * @return string translated string. + * @throws CakeException When '' is provided as a domain. */ public static function translate($singular, $plural = null, $domain = null, $category = 6, $count = null, $language = null) { $_this = I18n::getInstance(); @@ -162,6 +164,9 @@ class I18n { if (is_null($domain)) { $domain = self::$defaultDomain; } + if ($domain === '') { + throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.')); + } $_this->domain = $domain . '_' . $_this->l10n->lang; diff --git a/lib/Cake/Test/Case/I18n/I18nTest.php b/lib/Cake/Test/Case/I18n/I18nTest.php index 80303e2c5..4c5eba26e 100644 --- a/lib/Cake/Test/Case/I18n/I18nTest.php +++ b/lib/Cake/Test/Case/I18n/I18nTest.php @@ -1854,6 +1854,16 @@ class I18nTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * Test that the '' domain causes exceptions. + * + * @expectedException CakeException + * @return void + */ + public function testTranslateEmptyDomain() { + I18n::translate('Plural Rule 1', null, ''); + } + /** * Singular method *