Skip to content
This repository
Browse code

ENHANCEMENT New i18nTextCollector_Writer_RailsYaml

  • Loading branch information...
commit cbcee57496bdcfe62f1fa829780585fbbfe478eb 1 parent fca2c20
Ingo Schommer authored April 14, 2012
39  docs/en/topics/i18n.md
Source Rendered
@@ -220,13 +220,35 @@ If you want to run the text collector for just one module you can use the 'modul
220 220
 **Note**: You'll need to install PHPUnit to run the text collector (see [testing-guide](/topics/testing)).
221 221
 </div>
222 222
 
223  
-## Language definitions
  223
+## Language definitions (3.x)
224 224
 
225 225
 Each module can have one language table per locale, stored by convention in the `lang/` subfolder.
226 226
 The translation is powered by `[Zend_Translate](http://framework.zend.com/manual/en/zend.translate.html)`,
227 227
 which supports different translation adapters, dealing with different storage formats.
228 228
 
229  
-In SilverStripe 2.x, there tables are just PHP files with array notations,
  229
+By default, SilverStripe 3.x uses a YAML format (through the [Zend_Translate_RailsYAML adapter](https://github.com/chillu/zend_translate_railsyaml)). 
  230
+
  231
+Example: sapphire/lang/en.yml (extract)
  232
+
  233
+	:::yml
  234
+	en:
  235
+	  ImageUploader:
  236
+	    Attach: 'Attach %s'
  237
+	  FileIFrameField:
  238
+	    NOTEADDFILES: 'You can add files once you have saved for the first time.'
  239
+
  240
+Translation table: sapphire/lang/de.yml (extract)
  241
+
  242
+	:::yml
  243
+	de:
  244
+	  ImageUploader:
  245
+	    ATTACH: '%s anhängen'
  246
+	  FileIframeField:
  247
+	    NOTEADDFILES: 'Sie können Dateien hinzufügen sobald Sie das erste mal gespeichert haben'
  248
+
  249
+## Language definitions (2.x)
  250
+
  251
+In SilverStripe 2.x, the tables are just PHP files with array notations,
230 252
 stored based on their locale name (e.g. "en_US.php").
231 253
 
232 254
 Example: framework/lang/en_US.php (extract)
@@ -247,6 +269,19 @@ Translation table: framework/lang/de_DE.php (extract)
247 269
 	$lang['de_DE']['ImageUploader']['ATTACH'] = '%s anhängen';
248 270
 	$lang['de_DE']['FileIframeField']['NOTEADDFILES'] = 'Sie können Dateien hinzufügen sobald Sie das erste mal gespeichert haben';
249 271
 
  272
+In order to enable usage of PHP language definitions in 3.x, you need to register a legacy adapter
  273
+in your `mysite/_config.php`:
  274
+
  275
+	:::php
  276
+	i18n::register_translator(
  277
+		new Zend_Translate(array(
  278
+			'adapter' => 'i18nSSLegacyAdapter',
  279
+			'locale' => i18n::default_locale(),
  280
+			'disableNotices' => true,
  281
+		)),
  282
+		'legacy',
  283
+		9 // priority lower than standard translator
  284
+	);
250 285
 
251 286
 ## Javascript Usage
252 287
 
54  i18n/i18nTextCollector.php
@@ -61,7 +61,7 @@ public function setWriter($writer) {
61 61
 	}
62 62
 
63 63
 	public function getWriter() {
64  
-		if(!$this->writer) $this->writer = new i18nTextCollector_Writer_Php();
  64
+		if(!$this->writer) $this->writer = new i18nTextCollector_Writer_RailsYaml();
65 65
 		return $this->writer;
66 66
 	}
67 67
 	
@@ -452,3 +452,55 @@ public function langArrayCodeForEntitySpec($entityFullName, $entitySpec, $locale
452 452
 		return $php;
453 453
 	}
454 454
 }
  455
+
  456
+/**
  457
+ * Writes files compatible with {@link i18nRailsYamlAdapter}.
  458
+ */
  459
+class i18nTextCollector_Writer_RailsYaml implements i18nTextCollector_Writer {
  460
+
  461
+	public function write($entities, $locale, $path) {
  462
+		$content = '';
  463
+
  464
+		// Create folder for lang files
  465
+		$langFolder = $path . '/lang';
  466
+		if(!file_exists($langFolder)) {
  467
+			Filesystem::makeFolder($langFolder, Filesystem::$folder_create_mask);
  468
+			touch($langFolder . '/_manifest_exclude');
  469
+		}
  470
+
  471
+		// Open the English file and write the Master String Table
  472
+		$langFile = $langFolder . '/' . $locale . '.yml';
  473
+		if($fh = fopen($langFile, "w")) {
  474
+			fwrite($fh, $this->getYaml($entities,$locale));
  475
+			fclose($fh);
  476
+		} else {
  477
+			throw new LogicException("Cannot write language file! Please check permissions of $langFile");
  478
+		}
  479
+
  480
+		return true;
  481
+	}
  482
+
  483
+	public function getYaml($entities, $locale) {
  484
+		if(!class_exists('sfYamlDumper', false)) require_once 'thirdparty/symfony-yaml/lib/sfYamlDumper.php';
  485
+
  486
+		// Unflatten array
  487
+		$entitiesNested = array();
  488
+		foreach($entities as $entity => $spec) {
  489
+			// Legacy support: Don't count *.ss as namespace
  490
+			$entity = preg_replace('/\.ss\./', '___ss.', $entity);
  491
+			$parts = explode('.', $entity);
  492
+			$currLevel = &$entitiesNested;
  493
+			while($part = array_shift($parts)) {
  494
+				$part = str_replace('___ss', '.ss', $part);
  495
+				if(!isset($currLevel[$part])) $currLevel[$part] = array();
  496
+				$currLevel = &$currLevel[$part];
  497
+			}
  498
+			$currLevel = $spec[0];
  499
+		}
  500
+
  501
+		// Write YAML
  502
+		$yamlHandler = new sfYaml();
  503
+		// TODO Dumper can't handle YAML comments, so the context information is currently discarded
  504
+		return $yamlHandler->dump(array($locale => $entitiesNested), 99);
  505
+	}
  506
+}
20  tests/i18n/i18nTextCollectorTest.php
@@ -329,6 +329,26 @@ function testPhpWriterLangArrayCodeForEntity() {
329 329
 			$php
330 330
 		);
331 331
 	}
  332
+
  333
+	/**
  334
+	 * @todo Should be in a separate test suite, but don't want to duplicate setup logic
  335
+	 */
  336
+	function testYamlWriter() {
  337
+		$writer = new i18nTextCollector_Writer_RailsYaml();
  338
+		$entities = array(
  339
+			'Level1.Level2.EntityName' => array('Text', 'Context'),
  340
+			'Level1.OtherEntityName' => array('Other Text', 'Other Context'),
  341
+		);
  342
+		$yaml = <<<YAML
  343
+de:
  344
+  Level1:
  345
+    Level2:
  346
+      EntityName: Text
  347
+    OtherEntityName: 'Other Text'
  348
+
  349
+YAML;
  350
+		$this->assertEquals($yaml, $writer->getYaml($entities, 'de'));
  351
+	}
332 352
 	
333 353
 	function testCollectFromIncludedTemplates() {
334 354
 		$c = new i18nTextCollector();

0 notes on commit cbcee57

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