BUGFIX: More fixes to i18n text collection with escaping of quotes

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@81420 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-07-09 02:37:01 +00:00
parent 8ee9373807
commit 5a9ca7d682
2 changed files with 20 additions and 7 deletions

View File

@ -181,6 +181,7 @@ class i18nTextCollector extends Object {
'([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,' . // priority (optional)
'[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*' . // comment (optional)
'\)';
while (ereg($regexRule, $content, $regs)) {
$entitiesArr = array_merge($entitiesArr, (array)$this->entitySpecFromRegexMatches($regs));
@ -282,13 +283,19 @@ class i18nTextCollector extends Object {
// remove wrapping quotes
$value = ($regs[2]) ? substr($regs[2],1,-1) : null;
$value = ereg_replace("([^\\])['\"][[:space:]]*.[[:space:]]*['\"]",'\\1',$value);
$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
// be properly escaped already
if(substr($regs[2],0,1) == '"') $value = addcslashes($value,'\'');
if(substr($regs[2],0,1) == '"') {
// Double quotes don't need escaping
$value = str_replace('\\"','"', $value);
// But single quotes do
$value = str_replace("'","\\'", $value);
}
// remove starting comma and any newlines
$prio = ($regs[10]) ? trim(preg_replace('/\n/','',substr($regs[10],1))) : null;

View File

@ -70,17 +70,23 @@ class i18nTextCollectorTest extends SapphireTest {
$php = <<<PHP
_t(
'Test.CONCATENATED',
'Line 1' .
'Line 2' .
'Line 3',
'Line 1 and ' .
'Line \'2\' and ' .
'Line "3"',
PR_MEDIUM,
'Comment'
);
_t(
'Test.CONCATENATED2',
"Line \"4\" and " .
"Line 5");
PHP;
$this->assertEquals(
$c->collectFromCode($php, 'mymodule'),
array(
'Test.CONCATENATED' => array("Line 1Line 2Line 3",'PR_MEDIUM','Comment')
'Test.CONCATENATED' => array("Line 1 and Line \\'2\\' and Line \"3\"",'PR_MEDIUM','Comment'),
'Test.CONCATENATED2' => array("Line \"4\" and Line 5",null,null)
)
);
}