Browse Source

Implemented the __xn() function

Jose Lorenzo Rodriguez 11 years ago
parent
commit
4a08a221bb
2 changed files with 70 additions and 0 deletions
  1. 32 0
      src/basics.php
  2. 38 0
      tests/TestCase/I18n/I18nTest.php

+ 32 - 0
src/basics.php

@@ -226,6 +226,8 @@ if (!function_exists('__x')) {
 
 /**
  * Returns a translated string if one is found; Otherwise, the submitted message.
+ * The context is a unique identifier for the translations string that makes it unique
+ * for in the same domain.
  *
  * @param string $context Context of the text
  * @param string $singular Text to translate
@@ -243,3 +245,33 @@ if (!function_exists('__x')) {
 	}
 
 }
+
+if (!function_exists('__xn')) {
+
+/**
+ * Returns correct plural form of message identified by $singular and $plural for count $count.
+ * Some languages have more than one form for plural messages dependent on the count.
+ * The context is a unique identifier for the translations string that makes it unique
+ * for in the same domain.
+ *
+ * @param string $context Context of the text
+ * @param string $singular Singular text to translate
+ * @param string $plural Plural text
+ * @param int $count Count
+ * @param mixed $args Array with arguments or multiple arguments in function
+ * @return mixed plural form of translated string
+ * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__xn
+ */
+	function __xn($context, $singular, $plural, $count, $args = null) {
+		if (!$singular) {
+			return;
+		}
+
+		$arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 2);
+		return I18n::translator()->translate(
+			$singular,
+			['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments
+		);
+	}
+
+}

+ 38 - 0
tests/TestCase/I18n/I18nTest.php

@@ -283,6 +283,7 @@ class I18nTest extends TestCase {
 			]);
 			return $package;
 		});
+
 		$this->assertEquals('The letter A', __x('character', 'letter', ['A']));
 		$this->assertEquals(
 			'She wrote a letter to Thomas',
@@ -291,6 +292,43 @@ class I18nTest extends TestCase {
 	}
 
 /**
+ * Tests the __xn() function
+ *
+ * @return void
+ */
+	public function testPluralContextFunction() {
+		I18n::translator('default', 'en_US', function () {
+			$package = new Package('default');
+			$package->setMessages([
+				'letter' => [
+					'_context' => [
+						'character' => [
+							'The letter {0}',
+							'The letters {0} and {1}'
+						],
+						'communication' => [
+							'She wrote a letter to {0}',
+							'She wrote a letter to {0} and {1}'
+						]
+					]
+				]
+			]);
+			return $package;
+		});
+		$this->assertEquals('The letters A and B', __xn('character', 'letter', 'letters', 2, ['A', 'B']));
+		$this->assertEquals('The letter A', __xn('character', 'letter', 'letters', 1, ['A']));
+
+		$this->assertEquals(
+			'She wrote a letter to Thomas and Sara',
+			__xn('communication', 'letter', 'letters', 2, ['Thomas', 'Sara'])
+		);
+		$this->assertEquals(
+			'She wrote a letter to Thomas',
+			__xn('communication', 'letter', 'letters', 1, ['Thomas'])
+		);
+	}
+
+/**
  * Tests that translators are cached for performance
  *
  * @return void