Browse Source

Parsing and sotring the message context in .po files, this will be used to
namespace strings inside the same translations bundle

Jose Lorenzo Rodriguez 11 years ago
parent
commit
3fd246e10f

+ 41 - 18
src/I18n/Parser/PoFileParser.php

@@ -89,10 +89,11 @@ class PoFileParser  {
 			} elseif (substr($line, 0, 7) === 'msgid "') {
 				// We start a new msg so save previous
 				$this->_addMessage($messages, $item);
-				$item = $defaults;
 				$item['ids']['singular'] = substr($line, 7, -1);
 			} elseif (substr($line, 0, 8) === 'msgstr "') {
 				$item['translated'] = substr($line, 8, -1);
+			} elseif (substr($line, 0, 9) === 'msgctxt "') {
+				$item['context'] = substr($line, 9, -1);
 			} elseif ($line[0] === '"') {
 				$continues = isset($item['translated']) ? 'translated' : 'ids';
 
@@ -108,7 +109,6 @@ class PoFileParser  {
 				$size = strpos($line, ']');
 				$item['translated'][(int)substr($line, 7, 1)] = substr($line, $size + 3, -1);
 			}
-
 		}
 		// save last item
 		$this->_addMessage($messages, $item);
@@ -125,23 +125,46 @@ class PoFileParser  {
  * @return void
  */
 	protected function _addMessage(array &$messages, array $item) {
-		if (is_array($item['translated'])) {
-			$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]);
-			if (isset($item['ids']['plural'])) {
-				$plurals = $item['translated'];
-				// PO are by definition indexed so sort by index.
-				ksort($plurals);
-				// Make sure every index is filled.
-				end($plurals);
-				$count = key($plurals);
-				// Fill missing spots with '-'.
-				$empties = array_fill(0, $count + 1, '');
-				$plurals += $empties;
-				ksort($plurals);
-				$messages[stripcslashes($item['ids']['plural'])] = array_map('stripcslashes', $plurals);
+		if (empty($item['ids']['singular']) && empty($item['ids']['plural'])) {
+			return;
+		}
+
+		$singular = stripcslashes($item['ids']['singular']);
+		$context = isset($item['context']) ? $item['context'] : null;
+		$translation = $item['translated'];
+
+		if (is_array($translation)) {
+			$translation = stripcslashes($translation[0]);
+		}
+
+		if ($context) {
+			$messages[$singular]['_context'][$context] = $translation;
+		} else {
+			$messages[$singular] = $translation;
+		}
+
+		if (isset($item['ids']['plural'])) {
+			$plurals = $item['translated'];
+			// PO are by definition indexed so sort by index.
+			ksort($plurals);
+
+			// Make sure every index is filled.
+			end($plurals);
+			$count = key($plurals);
+
+			// Fill missing spots with an empty string.
+			$empties = array_fill(0, $count + 1, '');
+			$plurals += $empties;
+			ksort($plurals);
+
+			$plurals = array_map('stripcslashes', $plurals);
+			$key = stripcslashes($item['ids']['plural']);
+
+			if ($context) {
+				$messages[$key]['_context'][$context] = $plurals;
+			} else {
+				$messages[$key] = $plurals;
 			}
-		} elseif (!empty($item['ids']['singular'])) {
-			$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);
 		}
 	}
 

+ 12 - 3
tests/TestCase/I18n/Parser/PoFileParserTest.php

@@ -35,10 +35,19 @@ class PoFileParserTest extends TestCase {
 		$this->assertCount(5, $messages);
 		$expected = [
 			'Plural Rule 1' => 'Plural Rule 1 (translated)',
-			'%d = 1' => '%d = 1 (translated)',
+			'%d = 1' => [
+				'_context' => [
+					'This is the context' => 'First Context trasnlation',
+					'Another Context' => '%d = 1 (translated)'
+				]
+			],
 			'%d = 0 or > 1' => [
-				0 => '%d = 1 (translated)',
-				1 => '%d = 0 or > 1 (translated)'
+				'_context' => [
+					'Another Context' => [
+						0 => '%d = 1 (translated)',
+						1 => '%d = 0 or > 1 (translated)'
+					]
+				]
 			],
 			'%-5d = 1' => '%-5d = 1 (translated)',
 			'%-5d = 0 or > 1' => [

+ 4 - 1
tests/test_app/TestApp/Locale/rule_1_po/default.po

@@ -12,10 +12,13 @@ msgstr ""
 "X-Poedit-Language: Two Forms of Plurals\n"
 "X-Poedit-SourceCharset: utf-8\n"
 
-msgctxt "This is the context"
 msgid "Plural Rule 1"
 msgstr "Plural Rule 1 (translated)"
 
+msgctxt "This is the context"
+msgid "%d = 1"
+msgstr "First Context trasnlation"
+
 msgctxt "Another Context"
 msgid "%d = 1"
 msgid_plural "%d = 0 or > 1"