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
This commit is contained in:
Sam Minnee 2009-07-09 01:02:43 +00:00
parent e1fa960775
commit 8ee9373807
2 changed files with 36 additions and 12 deletions

View File

@ -175,10 +175,11 @@ class i18nTextCollector extends Object {
$entitiesArr = array(); $entitiesArr = array();
$regexRule = '_t[[:space:]]*\(' . $regexRule = '_t[[:space:]]*\(' .
'[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . # namespace.entity '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]*,' . # value '[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value
'[[:space:]]*[^,)]*)?([[:space:]]*,' . # priority (optional) '([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . # comment '([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional)
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional)
'\)'; '\)';
while (ereg($regexRule, $content, $regs)) { while (ereg($regexRule, $content, $regs)) {
$entitiesArr = array_merge($entitiesArr, (array)$this->entitySpecFromRegexMatches($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()) // @todo respect template tags (< % _t() % > instead of _t())
$regexRule = '_t[[:space:]]*\(' . $regexRule = '_t[[:space:]]*\(' .
'[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . # namespace.entity '[[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,' . // namespace.entity
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]]*,' . # value '[[:space:]]*(("([^"]|\\\")*"|\'([^\']|\\\\\')*\')' . // value
'[[:space:]]*[^,)]*)?([[:space:]]*,' . # priority (optional) '([[:space:]]*\\.[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))*)' . // concatenations
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . # comment (optional) '([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional)
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional)
'\)'; '\)';
while(ereg($regexRule,$content,$regs)) { while(ereg($regexRule,$content,$regs)) {
$entitiesArr = array_merge($entitiesArr,(array)$this->entitySpecFromRegexMatches($regs, $fileName)); $entitiesArr = array_merge($entitiesArr,(array)$this->entitySpecFromRegexMatches($regs, $fileName));
@ -280,6 +282,8 @@ class i18nTextCollector extends Object {
// remove wrapping quotes // remove wrapping quotes
$value = ($regs[2]) ? substr($regs[2],1,-1) : null; $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 // 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 // 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,'\''); if(substr($regs[2],0,1) == '"') $value = addcslashes($value,'\'');
// remove starting comma and any newlines // 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 // remove wrapping quotes
$comment = ($regs[7]) ? substr($regs[7],1,-1) : null; $comment = ($regs[12]) ? substr($regs[12],1,-1) : null;
return array( return array(
"{$namespace}.{$entity}" => array( "{$namespace}.{$entity}" => array(

View File

@ -63,7 +63,27 @@ class i18nTextCollectorTest extends SapphireTest {
parent::tearDown(); parent::tearDown();
} }
function testConcatenationInEntityValues() {
$c = new i18nTextCollector();
$php = <<<PHP
_t(
'Test.CONCATENATED',
'Line 1' .
'Line 2' .
'Line 3',
PR_MEDIUM,
'Comment'
);
PHP;
$this->assertEquals(
$c->collectFromCode($php, 'mymodule'),
array(
'Test.CONCATENATED' => array("Line 1Line 2Line 3",'PR_MEDIUM','Comment')
)
);
}
function testCollectFromTemplateSimple() { function testCollectFromTemplateSimple() {
$c = new i18nTextCollector(); $c = new i18nTextCollector();
@ -275,7 +295,7 @@ PHP;
) )
); );
} }
/** /**
* Input for langArrayCodeForEntitySpec() should be suitable for insertion * Input for langArrayCodeForEntitySpec() should be suitable for insertion
* into single-quoted strings, so needs to be escaped already. * into single-quoted strings, so needs to be escaped already.