From 8ee937380717a4a4dcb0d7f2d246ef448895ca11 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 9 Jul 2009 01:02:43 +0000 Subject: [PATCH] ENHANCEMENT: Allow string concatenation in default text in _t() calls git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@81409 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/i18nTextCollector.php | 24 ++++++++++++++---------- tests/i18n/i18nTextCollectorTest.php | 24 ++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/core/i18nTextCollector.php b/core/i18nTextCollector.php index 87227be49..7d3b1d52a 100644 --- a/core/i18nTextCollector.php +++ b/core/i18nTextCollector.php @@ -175,10 +175,11 @@ class i18nTextCollector extends Object { $entitiesArr = array(); $regexRule = '_t[[:space:]]*\(' . - '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . # namespace.entity - '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]*,' . # value - '[[:space:]]*[^,)]*)?([[:space:]]*,' . # priority (optional) - '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . # comment + '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity + '[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value + '([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations + '([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional) + '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional) '\)'; while (ereg($regexRule, $content, $regs)) { $entitiesArr = array_merge($entitiesArr, (array)$this->entitySpecFromRegexMatches($regs)); @@ -211,10 +212,11 @@ class i18nTextCollector extends Object { // @todo respect template tags (< % _t() % > instead of _t()) $regexRule = '_t[[:space:]]*\(' . - '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . # namespace.entity - '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]]*,' . # value - '[[:space:]]*[^,)]*)?([[:space:]]*,' . # priority (optional) - '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . # comment (optional) + '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity + '[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value + '([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations + '([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional) + '[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional) '\)'; while(ereg($regexRule,$content,$regs)) { $entitiesArr = array_merge($entitiesArr,(array)$this->entitySpecFromRegexMatches($regs, $fileName)); @@ -280,6 +282,8 @@ class i18nTextCollector extends Object { // remove wrapping quotes $value = ($regs[2]) ? substr($regs[2],1,-1) : null; + + $value = ereg_replace("([^\\])['\"][[:space:]]*.[[:space:]]*['\"]",'\\1',$value); // only escape quotes when wrapped in double quotes, to make them safe for insertion // into single-quoted PHP code. If they're wrapped in single quotes, the string should @@ -287,10 +291,10 @@ class i18nTextCollector extends Object { if(substr($regs[2],0,1) == '"') $value = addcslashes($value,'\''); // remove starting comma and any newlines - $prio = ($regs[5]) ? trim(preg_replace('/\n/','',substr($regs[5],1))) : null; + $prio = ($regs[10]) ? trim(preg_replace('/\n/','',substr($regs[10],1))) : null; // remove wrapping quotes - $comment = ($regs[7]) ? substr($regs[7],1,-1) : null; + $comment = ($regs[12]) ? substr($regs[12],1,-1) : null; return array( "{$namespace}.{$entity}" => array( diff --git a/tests/i18n/i18nTextCollectorTest.php b/tests/i18n/i18nTextCollectorTest.php index e5f3fcb8b..4fd646bb2 100644 --- a/tests/i18n/i18nTextCollectorTest.php +++ b/tests/i18n/i18nTextCollectorTest.php @@ -63,7 +63,27 @@ class i18nTextCollectorTest extends SapphireTest { parent::tearDown(); } - + + function testConcatenationInEntityValues() { + $c = new i18nTextCollector(); + + $php = <<assertEquals( + $c->collectFromCode($php, 'mymodule'), + array( + 'Test.CONCATENATED' => array("Line 1Line 2Line 3",'PR_MEDIUM','Comment') + ) + ); + } function testCollectFromTemplateSimple() { $c = new i18nTextCollector(); @@ -275,7 +295,7 @@ PHP; ) ); } - + /** * Input for langArrayCodeForEntitySpec() should be suitable for insertion * into single-quoted strings, so needs to be escaped already.