Skip to content

Commit

Permalink
Implemented the __xn() function
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Sep 27, 2014
1 parent 7ad3186 commit 4a08a22
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/basics.php
Expand Up @@ -226,6 +226,8 @@ function __dn($domain, $singular, $plural, $count, $args = null) {

/**
* 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
Expand All @@ -243,3 +245,33 @@ function __x($context, $singular, $args = null) {
}

}

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 changes: 38 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -283,13 +283,51 @@ public function testBasicContextFunction() {
]);
return $package;
});

$this->assertEquals('The letter A', __x('character', 'letter', ['A']));
$this->assertEquals(
'She wrote a letter to Thomas',
__x('communication', 'letter', ['Thomas'])
);
}

/**
* 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
*
Expand Down

0 comments on commit 4a08a22

Please sign in to comment.