Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

API-CHANGE: parser for new i18n syntax

  • Loading branch information...
commit 189c305c8385d76ec8e8e8f9320992bbf3c90006 1 parent 45c7dfd
@candidasa candidasa authored
Showing with 1,638 additions and 1,380 deletions.
  1. +1 −25 tests/i18n/i18nTest.php
  2. +1,600 −1,339 view/SSTemplateParser.php
  3. +37 −16 view/SSTemplateParser.php.inc
View
26 tests/i18n/i18nTest.php
@@ -171,14 +171,10 @@ function testTemplateTranslation() {
i18n::set_locale('en_US');
i18n::get_translator('core')->getAdapter()->addTranslation(array(
'i18nTestModule.MAINTEMPLATE' => 'Main Template',
- 'i18nTestModule.ss.SPRINTFNONAMESPACE' => 'My replacement no namespace: %s',
'i18nTestModule.LAYOUTTEMPLATE' => 'Layout Template',
- 'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' => 'Layout Template no namespace',
'i18nTestModule.SPRINTFNAMESPACE' => 'My replacement: %s',
'i18nTestModule.WITHNAMESPACE' => 'Include Entity with Namespace',
- 'i18nTestModuleInclude.ss.NONAMESPACE' => 'Include Entity without Namespace',
'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' => 'My include replacement: %s',
- 'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' => 'My include replacement no namespace: %s'
), 'en_US');
$viewer = new SSViewer('i18nTestModule');
@@ -187,22 +183,14 @@ function testTemplateTranslation() {
"Layout Template\n",
$parsedHtml
);
- $this->assertContains(
- "Layout Template no namespace\n",
- $parsedHtml
- );
i18n::set_locale('de_DE');
i18n::get_translator('core')->getAdapter()->addTranslation(array(
'i18nTestModule.MAINTEMPLATE' => 'TRANS Main Template',
- 'i18nTestModule.ss.SPRINTFNONAMESPACE' => 'TRANS My replacement no namespace: %s',
'i18nTestModule.LAYOUTTEMPLATE' => 'TRANS Layout Template',
- 'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' => 'TRANS Layout Template no namespace',
'i18nTestModule.SPRINTFNAMESPACE' => 'TRANS My replacement: %s',
'i18nTestModule.WITHNAMESPACE' => 'TRANS Include Entity with Namespace',
- 'i18nTestModuleInclude.ss.NONAMESPACE' => 'TRANS Include Entity without Namespace',
'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' => 'TRANS My include replacement: %s',
- 'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' => 'TRANS My include replacement no namespace: %s'
), 'de_DE');
$viewer = new SSViewer('i18nTestModule');
@@ -216,10 +204,6 @@ function testTemplateTranslation() {
$parsedHtml
);
$this->assertContains(
- "TRANS Layout Template no namespace",
- $parsedHtml
- );
- $this->assertContains(
"TRANS My replacement: TestPropertyValue",
$parsedHtml
);
@@ -228,18 +212,10 @@ function testTemplateTranslation() {
$parsedHtml
);
$this->assertContains(
- "TRANS Include Entity without Namespace",
- $parsedHtml
- );
- $this->assertContains(
"TRANS My include replacement: TestPropertyValue",
$parsedHtml
);
- $this->assertContains(
- "TRANS My include replacement no namespace: TestPropertyValue",
- $parsedHtml
- );
-
+
i18n::set_locale($oldLocale);
}
View
2,939 view/SSTemplateParser.php
1,600 additions, 1,339 deletions not shown
View
53 view/SSTemplateParser.php.inc
@@ -7,7 +7,7 @@ This is the uncompiled parser for the SilverStripe template language, PHP with s
It gets run through the php-peg parser compiler to have those comments turned into code that match parts of the template language,
producing the executable version SSTemplateParser.php
-To recompile after changing this file, run this from the 'framework/core' directory via command line:
+To recompile after changing this file, run this from the 'framework/view' directory via command line (in most cases this is: sapphire/view):
php ../thirdparty/php-peg/cli.php SSTemplateParser.php.inc > SSTemplateParser.php
@@ -76,6 +76,9 @@ forbidden)
Closed Block: An SS template block that wraps content, and requires a counterpart <% end_blockname %> tag
+Angle Bracket: angle brackets "<" and ">" are used to eat whitespace between template elements
+N: eats white space including newlines (using in legacy _t support)
+
*/
class SSTemplateParser extends Parser {
@@ -97,9 +100,10 @@ class SSTemplateParser extends Parser {
/*!* SSTemplateParser
# Template is any structurally-complete portion of template (a full nested level in other words). It's the primary matcher,
- # and is used by all enclosing blocks, as well as a base for the top level
+ # and is used by all enclosing blocks, as well as a base for the top level.
+ # Any new template elements need to be included in this list, if they are to work.
- Template: (Comment | If | Require | CacheBlock | UncachedBlock | OldI18NTag | Include | ClosedBlock | OpenBlock | MalformedBlock | Injection | Text)+
+ Template: (Comment | Translate | If | Require | CacheBlock | UncachedBlock | OldI18NTag | ClosedBlock | OpenBlock | MalformedBlock | Injection | Text)+
*/
function Template_STR(&$res, $sub) {
$res['php'] .= $sub['php'] . PHP_EOL ;
@@ -177,40 +181,55 @@ class SSTemplateParser extends Parser {
/*!*
- # Translatable call
- Translate: "<%t" Entity (Default)? (!("is" "=") "is" Context)? (InjectionVariables)* "%>"
- InjectionVariables: InjectionName:Word "=" Argument
+ # New Translatable Syntax
+ # <%t Entity DefaultString is Context name1=string name2=$functionCall
+ # (This is a new way to call translatable strings. The parser transforms this into a call to the _t() method)
+
+ Translate: "<%t" < Entity < (Default:QuotedString)? < (!("is" "=") < "is" < Context:QuotedString)? < (InjectionVariables)? > "%>"
+ InjectionVariables: (< InjectionName:Word "=" Argument)+
+ Entity: / [A-Za-z_] [\w\.]* /
*/
function Translate__construct(&$res) {
- $res['php'] = "_t(";
+ $res['php'] = '$val .= _t(';
}
function Translate_Entity(&$res, $sub) {
- $res['php'] .= $sub['php'];
- Debug::show($sub);
+ $res['php'] .= "'$sub[text]'";
}
+
function Translate_Default(&$res, $sub) {
- Debug::show($sub);
+ $res['php'] .= ",$sub[text]";
}
+
function Translate_Context(&$res, $sub) {
- Debug::show($sub);
+ $res['php'] .= ",$sub[text]";
}
- function Translate__finalise(&$res) {
- $res['php'] .= ')';
+ function Translate_InjectionVariables(&$res, $sub) {
+ $res['php'] .= ",$sub[php]";
}
+ function Translate__finalise(&$res) {
+ $res['php'] .= ');';
+ }
function InjectionVariables__construct(&$res) {
+ $res['php'] = "array(";
}
function InjectionVariables_InjectionName(&$res, $sub) {
+ $res['php'] .= "'$sub[text]'=>";
}
function InjectionVariables_Argument(&$res, $sub) {
+ $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php']) . ',';
+ }
+ function InjectionVariables__finalise(&$res) {
+ if (substr($res['php'], -1) == ',') $res['php'] = substr($res['php'], 0, -1); //remove last comma in the array
+ $res['php'] .= ')';
}
@@ -303,7 +322,7 @@ class SSTemplateParser extends Parser {
function Argument_FreeString(&$res, $sub) {
$res['ArgumentMode'] = 'string';
- $res['php'] = "'" . str_replace("'", "\\'", rtrim($sub['text'])) . "'";
+ $res['php'] = "'" . str_replace("'", "\\'", $sub['text']) . "'";
}
/*!*
@@ -546,8 +565,10 @@ class SSTemplateParser extends Parser {
# This is the core used by both syntaxes, without the block start & end tags
- OldTPart: "_t" < "(" < QuotedString (< "," < CallArguments)? > ")"
-
+ OldTPart: "_t" N "(" N QuotedString (N "," N CallArguments)? N ")" N (";")?
+
+ # whitespace with a newline
+ N: / [\s\n]* /
*/
function OldTPart__construct(&$res) {
$res['php'] = "_t(";
Please sign in to comment.
Something went wrong with that request. Please try again.