Skip to content
This repository
Browse code

API-CHANGE: parser for new i18n syntax

  • Loading branch information...
commit 189c305c8385d76ec8e8e8f9320992bbf3c90006 1 parent 45c7dfd
Julian Seidenberg authored April 16, 2012
26  tests/i18n/i18nTest.php
@@ -171,14 +171,10 @@ function testTemplateTranslation() {
171 171
 		i18n::set_locale('en_US');
172 172
 		i18n::get_translator('core')->getAdapter()->addTranslation(array(
173 173
 			'i18nTestModule.MAINTEMPLATE' => 'Main Template',
174  
-			'i18nTestModule.ss.SPRINTFNONAMESPACE' => 'My replacement no namespace: %s',
175 174
 			'i18nTestModule.LAYOUTTEMPLATE' => 'Layout Template',
176  
-			'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' => 'Layout Template no namespace',
177 175
 			'i18nTestModule.SPRINTFNAMESPACE' => 'My replacement: %s',
178 176
 			'i18nTestModule.WITHNAMESPACE' => 'Include Entity with Namespace',
179  
-			'i18nTestModuleInclude.ss.NONAMESPACE' => 'Include Entity without Namespace',
180 177
 			'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' => 'My include replacement: %s',
181  
-			'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' => 'My include replacement no namespace: %s'
182 178
 		), 'en_US');
183 179
 		
184 180
 		$viewer = new SSViewer('i18nTestModule');
@@ -187,22 +183,14 @@ function testTemplateTranslation() {
187 183
 			"Layout Template\n",
188 184
 			$parsedHtml
189 185
 		);
190  
-		$this->assertContains(
191  
-			"Layout Template no namespace\n",
192  
-			$parsedHtml
193  
-		);
194 186
 		
195 187
 		i18n::set_locale('de_DE');
196 188
 		i18n::get_translator('core')->getAdapter()->addTranslation(array(
197 189
 			'i18nTestModule.MAINTEMPLATE' => 'TRANS Main Template',
198  
-			'i18nTestModule.ss.SPRINTFNONAMESPACE' => 'TRANS My replacement no namespace: %s',
199 190
 			'i18nTestModule.LAYOUTTEMPLATE' => 'TRANS Layout Template',
200  
-			'i18nTestModule.ss.LAYOUTTEMPLATENONAMESPACE' => 'TRANS Layout Template no namespace',
201 191
 			'i18nTestModule.SPRINTFNAMESPACE' => 'TRANS My replacement: %s',
202 192
 			'i18nTestModule.WITHNAMESPACE' => 'TRANS Include Entity with Namespace',
203  
-			'i18nTestModuleInclude.ss.NONAMESPACE' => 'TRANS Include Entity without Namespace',
204 193
 			'i18nTestModuleInclude.ss.SPRINTFINCLUDENAMESPACE' => 'TRANS My include replacement: %s',
205  
-			'i18nTestModuleInclude.ss.SPRINTFINCLUDENONAMESPACE' => 'TRANS My include replacement no namespace: %s'
206 194
 		), 'de_DE');
207 195
 
208 196
 		$viewer = new SSViewer('i18nTestModule');
@@ -216,10 +204,6 @@ function testTemplateTranslation() {
216 204
 			$parsedHtml
217 205
 		);
218 206
 		$this->assertContains(
219  
-			"TRANS Layout Template no namespace",
220  
-			$parsedHtml
221  
-		);
222  
-		$this->assertContains(
223 207
 			"TRANS My replacement: TestPropertyValue",
224 208
 			$parsedHtml
225 209
 		);
@@ -228,18 +212,10 @@ function testTemplateTranslation() {
228 212
 			$parsedHtml
229 213
 		);
230 214
 		$this->assertContains(
231  
-			"TRANS Include Entity without Namespace",
232  
-			$parsedHtml
233  
-		);
234  
-		$this->assertContains(
235 215
 			"TRANS My include replacement: TestPropertyValue",
236 216
 			$parsedHtml
237 217
 		);
238  
-		$this->assertContains(
239  
-			"TRANS My include replacement no namespace: TestPropertyValue",
240  
-			$parsedHtml
241  
-		);
242  
-		
  218
+
243 219
 		i18n::set_locale($oldLocale);
244 220
 	}
245 221
 
2,939  view/SSTemplateParser.php
1600 additions, 1339 deletions not shown
53  view/SSTemplateParser.php.inc
@@ -7,7 +7,7 @@ This is the uncompiled parser for the SilverStripe template language, PHP with s
7 7
 It gets run through the php-peg parser compiler to have those comments turned into code that match parts of the template language,
8 8
 producing the executable version SSTemplateParser.php
9 9
 
10  
-To recompile after changing this file, run this from the 'framework/core' directory via command line:
  10
+To recompile after changing this file, run this from the 'framework/view' directory via command line (in most cases this is: sapphire/view):
11 11
  
12 12
 	php ../thirdparty/php-peg/cli.php SSTemplateParser.php.inc > SSTemplateParser.php 
13 13
   
@@ -76,6 +76,9 @@ forbidden)
76 76
  
77 77
 Closed Block: An SS template block that wraps content, and requires a counterpart <% end_blockname %> tag
78 78
 
  79
+Angle Bracket: angle brackets "<" and ">" are used to eat whitespace between template elements
  80
+N: eats white space including newlines (using in legacy _t support)
  81
+
79 82
 */
80 83
 class SSTemplateParser extends Parser {
81 84
 
@@ -97,9 +100,10 @@ class SSTemplateParser extends Parser {
97 100
 	/*!* SSTemplateParser
98 101
 
99 102
 	# Template is any structurally-complete portion of template (a full nested level in other words). It's the primary matcher, 
100  
-	# and is used by all enclosing blocks, as well as a base for the top level
  103
+	# and is used by all enclosing blocks, as well as a base for the top level.
  104
+	# Any new template elements need to be included in this list, if they are to work.
101 105
 	 
102  
-	Template: (Comment | If | Require | CacheBlock | UncachedBlock | OldI18NTag | Include | ClosedBlock | OpenBlock | MalformedBlock | Injection | Text)+
  106
+	Template: (Comment | Translate | If | Require | CacheBlock | UncachedBlock | OldI18NTag | ClosedBlock | OpenBlock | MalformedBlock | Injection | Text)+
103 107
 	*/
104 108
 	function Template_STR(&$res, $sub) {
105 109
 		$res['php'] .= $sub['php'] . PHP_EOL ;
@@ -177,40 +181,55 @@ class SSTemplateParser extends Parser {
177 181
 
178 182
 
179 183
 	/*!*
180  
-	# Translatable call
181 184
 
182  
-	Translate: "<%t" Entity (Default)? (!("is" "=") "is" Context)? (InjectionVariables)* "%>"
183  
-	InjectionVariables: InjectionName:Word "=" Argument
  185
+	# New Translatable Syntax
  186
+	# <%t Entity DefaultString is Context name1=string name2=$functionCall
  187
+	# (This is a new way to call translatable strings. The parser transforms this into a call to the _t() method)
  188
+
  189
+	Translate: "<%t" < Entity < (Default:QuotedString)? < (!("is" "=") < "is" < Context:QuotedString)? < (InjectionVariables)? > "%>"
  190
+	InjectionVariables: (< InjectionName:Word "=" Argument)+
  191
+	Entity: / [A-Za-z_] [\w\.]* /
184 192
 	*/
185 193
 
186 194
 	function Translate__construct(&$res) {
187  
-		$res['php'] = "_t(";
  195
+		$res['php'] = '$val .= _t(';
188 196
 	}
189 197
 
190 198
 	function Translate_Entity(&$res, $sub) {
191  
-		$res['php'] .= $sub['php'];
192  
-		Debug::show($sub);
  199
+		$res['php'] .= "'$sub[text]'";
193 200
 	}
  201
+
194 202
 	function Translate_Default(&$res, $sub) {
195  
-		Debug::show($sub);
  203
+		$res['php'] .= ",$sub[text]";
196 204
 	}
  205
+
197 206
 	function Translate_Context(&$res, $sub) {
198  
-		Debug::show($sub);
  207
+		$res['php'] .= ",$sub[text]";
199 208
 	}
200 209
 
201  
-	function Translate__finalise(&$res) {
202  
-		$res['php'] .= ')';
  210
+	function Translate_InjectionVariables(&$res, $sub) {
  211
+		$res['php'] .= ",$sub[php]";
203 212
 	}
204 213
 
  214
+	function Translate__finalise(&$res) {
  215
+		$res['php'] .= ');';
  216
+	}
205 217
 
206 218
 	function InjectionVariables__construct(&$res) {
  219
+		$res['php'] = "array(";
207 220
 	}
208 221
 
209 222
 	function InjectionVariables_InjectionName(&$res, $sub) {
  223
+		$res['php'] .= "'$sub[text]'=>";
210 224
 	}
211 225
 
212 226
 	function InjectionVariables_Argument(&$res, $sub) {
  227
+		$res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php']) . ',';
  228
+	}
213 229
 
  230
+	function InjectionVariables__finalise(&$res) {
  231
+		if (substr($res['php'], -1) == ',') $res['php'] = substr($res['php'], 0, -1); //remove last comma in the array
  232
+		$res['php'] .= ')';
214 233
 	}
215 234
 
216 235
 
@@ -303,7 +322,7 @@ class SSTemplateParser extends Parser {
303 322
 	
304 323
 	function Argument_FreeString(&$res, $sub) {
305 324
 		$res['ArgumentMode'] = 'string';
306  
-		$res['php'] = "'" . str_replace("'", "\\'", rtrim($sub['text'])) . "'";
  325
+		$res['php'] = "'" . str_replace("'", "\\'", $sub['text']) . "'";
307 326
 	}
308 327
 	
309 328
 	/*!*
@@ -546,8 +565,10 @@ class SSTemplateParser extends Parser {
546 565
 	 
547 566
 	# This is the core used by both syntaxes, without the block start & end tags
548 567
 	 
549  
-	OldTPart: "_t" < "(" < QuotedString (< "," < CallArguments)? > ")"
550  
-	
  568
+	OldTPart: "_t" N "(" N QuotedString (N "," N CallArguments)? N ")" N (";")?
  569
+
  570
+	# whitespace with a newline
  571
+	N: / [\s\n]* /
551 572
 	*/
552 573
 	function OldTPart__construct(&$res) {
553 574
 		$res['php'] = "_t(";

0 notes on commit 189c305

Please sign in to comment.
Something went wrong with that request. Please try again.