Browse Source

Added support to MoParser for message contexts

Jose Lorenzo Rodriguez 11 years ago
parent
commit
b460901ba5
2 changed files with 56 additions and 5 deletions
  1. 21 5
      src/I18n/Parser/MoFileParser.php
  2. 35 0
      tests/TestCase/I18n/Parser/MoFileParserTest.php

+ 21 - 5
src/I18n/Parser/MoFileParser.php

@@ -87,6 +87,8 @@ class MoFileParser {
 		for ($i = 0; $i < $count; $i++) {
 			$pluralId = null;
 			$translated = null;
+			$context = null;
+			$plurals = null;
 
 			fseek($stream, $offsetId + $i * 8);
 
@@ -100,6 +102,10 @@ class MoFileParser {
 			fseek($stream, $offset);
 			$singularId = fread($stream, $length);
 
+			if (strpos($singularId, "\x04") !== false) {
+				list($context, $singularId) = explode("\x04", $singularId);
+			}
+
 			if (strpos($singularId, "\000") !== false) {
 				list($singularId, $pluralId) = explode("\000", $singularId);
 			}
@@ -110,15 +116,24 @@ class MoFileParser {
 			fseek($stream, $offset);
 			$translated = fread($stream, $length);
 
-			if (strpos($translated, "\000") === false) {
-				$messages[$singularId] = stripcslashes($translated);
+			if (strpos($translated, "\000") !== false) {
+				$translated = explode("\000", $translated);
+				$plurals = $pluralId !== null ? array_map('stripcslashes', $translated) : null;
+				$translated = $translated[0];
+			}
+
+			$singular = stripcslashes($translated);
+			if ($context !== null) {
+				$messages[$singularId]['_context'][$context] = $singular;
+				if ($pluralId !== null) {
+					$messages[$pluralId]['_context'][$context] = $plurals;
+				}
 				continue;
 			}
 
-			$translated = explode("\000", $translated);
-			$messages[$singularId] = stripcslashes($translated[0]);
+			$messages[$singularId] = $singular;
 			if ($pluralId !== null) {
-				$messages[$pluralId] = array_map('stripcslashes', $translated);
+				$messages[$pluralId] = $plurals;
 			}
 		}
 
@@ -139,4 +154,5 @@ class MoFileParser {
 
 		return (int)substr($result, -8);
 	}
+
 }

+ 35 - 0
tests/TestCase/I18n/Parser/MoFileParserTest.php

@@ -66,4 +66,39 @@ class MoFileParserTest extends TestCase {
 		$this->assertEquals($expected, $messages);
 	}
 
+/**
+ * Tests parsing a file with plurals and message context
+ *
+ * @return void
+ */
+	public function testParseFull() {
+		$parser = new MoFileParser;
+		$file = APP . 'Locale' . DS . 'rule_0_mo' . DS . 'default.mo';
+		$messages = $parser->parse($file);
+		$this->assertCount(5, $messages);
+		$expected = [
+			'Plural Rule 1' => 'Plural Rule 1 (translated)',
+			'%d = 1' => [
+				'_context' => [
+					'This is the context' => 'First Context trasnlation',
+					'Another Context' => '%d = 1 (translated)'
+				]
+			],
+			'%d = 0 or > 1' => [
+				'_context' => [
+					'Another Context' => [
+						0 => '%d = 1 (translated)',
+						1 => '%d = 0 or > 1 (translated)'
+					]
+				]
+			],
+			'%-5d = 1' => '%-5d = 1 (translated)',
+			'%-5d = 0 or > 1' => [
+				'%-5d = 1 (translated)',
+				'%-5d = 0 or > 1 (translated)'
+			]
+		];
+		$this->assertEquals($expected, $messages);
+	}
+
 }