mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Fix i18nTextCollector handling of special string characters
This commit is contained in:
parent
2ae8fde2d3
commit
699d5d6a42
@ -60,7 +60,8 @@ class Parser extends SSTemplateParser
|
|||||||
|
|
||||||
public function Translate_Entity(&$res, $sub)
|
public function Translate_Entity(&$res, $sub)
|
||||||
{
|
{
|
||||||
$this->currentEntityKey = $sub['text']; // key
|
// Collapse escaped slashes
|
||||||
|
$this->currentEntityKey = str_replace('\\\\', '\\', $sub['text']); // key
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Translate_Default(&$res, $sub)
|
public function Translate_Default(&$res, $sub)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace SilverStripe\i18n\TextCollection;
|
namespace SilverStripe\i18n\TextCollection;
|
||||||
|
|
||||||
|
use LogicException;
|
||||||
use SilverStripe\Core\ClassInfo;
|
use SilverStripe\Core\ClassInfo;
|
||||||
use SilverStripe\Core\Injector\Injectable;
|
use SilverStripe\Core\Injector\Injectable;
|
||||||
use SilverStripe\Core\Manifest\ClassLoader;
|
use SilverStripe\Core\Manifest\ClassLoader;
|
||||||
@ -618,14 +619,24 @@ class i18nTextCollector
|
|||||||
// Check text
|
// Check text
|
||||||
if ($id == T_CONSTANT_ENCAPSED_STRING) {
|
if ($id == T_CONSTANT_ENCAPSED_STRING) {
|
||||||
// Fixed quoting escapes, and remove leading/trailing quotes
|
// Fixed quoting escapes, and remove leading/trailing quotes
|
||||||
if (preg_match('/^\'/', $text)) {
|
if (preg_match('/^\'(?<text>.*)\'$/s', $text, $matches)) {
|
||||||
$text = str_replace("\\'", "'", $text);
|
$text = preg_replace_callback(
|
||||||
$text = preg_replace('/^\'/', '', $text);
|
'/\\\\([\\\\\'])/s', // only \ and '
|
||||||
$text = preg_replace('/\'$/', '', $text);
|
function ($input) {
|
||||||
|
return stripcslashes($input[0]);
|
||||||
|
},
|
||||||
|
$matches['text']
|
||||||
|
);
|
||||||
|
} elseif (preg_match('/^\"(?<text>.*)\"$/s', $text, $matches)) {
|
||||||
|
$text = preg_replace_callback(
|
||||||
|
'/\\\\([nrtvf\\\\$"]|[0-7]{1,3}|\x[0-9A-Fa-f]{1,2})/s', // rich replacement
|
||||||
|
function ($input) {
|
||||||
|
return stripcslashes($input[0]);
|
||||||
|
},
|
||||||
|
$matches['text']
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
$text = str_replace('\"', '"', $text);
|
throw new LogicException("Invalid string escape: " .$text);
|
||||||
$text = preg_replace('/^"/', '', $text);
|
|
||||||
$text = preg_replace('/"$/', '', $text);
|
|
||||||
}
|
}
|
||||||
} elseif ($id === T_CLASS_C) {
|
} elseif ($id === T_CLASS_C) {
|
||||||
// Evaluate __CLASS__ . '.KEY' concatenation
|
// Evaluate __CLASS__ . '.KEY' concatenation
|
||||||
|
@ -86,7 +86,8 @@ PHP;
|
|||||||
<%t i18nTestModule.INJECTIONS_3 name="Cat" greeting='meow' goodbye="meow" %>
|
<%t i18nTestModule.INJECTIONS_3 name="Cat" greeting='meow' goodbye="meow" %>
|
||||||
<%t i18nTestModule.INJECTIONS_4 name=\$absoluteBaseURL greeting=\$get_locale goodbye="global calls" %>
|
<%t i18nTestModule.INJECTIONS_4 name=\$absoluteBaseURL greeting=\$get_locale goodbye="global calls" %>
|
||||||
<%t i18nTestModule.INJECTIONS_9 "An item|{count} items" is "Test Pluralisation" count=4 %>
|
<%t i18nTestModule.INJECTIONS_9 "An item|{count} items" is "Test Pluralisation" count=4 %>
|
||||||
<%t SilverStripe\TestModule\i18nTestModule.INJECTIONS_10 "This string is namespaced" %>
|
<%t SilverStripe\\TestModule\\i18nTestModule.INJECTIONS_10 "This string is namespaced" %>
|
||||||
|
<%t SilverStripe\\\\TestModule\\\\i18nTestModule.INJECTIONS_11 "Escaped namespaced string" %>
|
||||||
SS;
|
SS;
|
||||||
$c->collectFromTemplate($html, null, $mymodule);
|
$c->collectFromTemplate($html, null, $mymodule);
|
||||||
|
|
||||||
@ -105,7 +106,8 @@ SS;
|
|||||||
'other' => '{count} items',
|
'other' => '{count} items',
|
||||||
'comment' => 'Test Pluralisation'
|
'comment' => 'Test Pluralisation'
|
||||||
],
|
],
|
||||||
'SilverStripe\\TestModule\\i18nTestModule.INJECTIONS_10' => 'This string is namespaced'
|
'SilverStripe\\TestModule\\i18nTestModule.INJECTIONS_10' => 'This string is namespaced',
|
||||||
|
'SilverStripe\\TestModule\\i18nTestModule.INJECTIONS_11' => 'Escaped namespaced string'
|
||||||
],
|
],
|
||||||
$c->collectFromTemplate($html, null, $mymodule)
|
$c->collectFromTemplate($html, null, $mymodule)
|
||||||
);
|
);
|
||||||
@ -328,10 +330,26 @@ class MyClass extends Base implements SomeService {
|
|||||||
'New Lines'
|
'New Lines'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
public function getAnotherString() {
|
||||||
|
return _t(
|
||||||
|
'SilverStripe\\\\Framework\\\\MyClass.ANOTHER_STRING',
|
||||||
|
'Slash=\\\\, Quote=\\''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
public function getDoubleQuotedString() {
|
||||||
|
return _t(
|
||||||
|
"SilverStripe\\\\Framework\\\\MyClass.DOUBLE_STRING",
|
||||||
|
"Slash=\\\\, Quote=\\""
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
PHP;
|
PHP;
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
[ 'SilverStripe\\Framework\\Core\\MyClass.NEWLINES' => "New Lines" ],
|
[
|
||||||
|
'SilverStripe\\Framework\\Core\\MyClass.NEWLINES' => "New Lines",
|
||||||
|
'SilverStripe\\Framework\\MyClass.ANOTHER_STRING' => 'Slash=\\, Quote=\'',
|
||||||
|
'SilverStripe\\Framework\\MyClass.DOUBLE_STRING' => 'Slash=\\, Quote="'
|
||||||
|
],
|
||||||
$c->collectFromCode($php, null, $mymodule)
|
$c->collectFromCode($php, null, $mymodule)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user