Skip to content

Commit

Permalink
Implemented __dx() function
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Sep 27, 2014
1 parent 4a08a22 commit c51bf58
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/basics.php
Expand Up @@ -275,3 +275,31 @@ function __xn($context, $singular, $plural, $count, $args = null) {
}

}

if (!function_exists('__dx')) {

/**
* Allows you to override the current domain for a single message lookup.
* The context is a unique identifier for the translations string that makes it unique
* for in the same domain.
*
* @param string $domain Domain
* @param string $context Context of the text
* @param string $msg String to translate
* @param mixed $args Array with arguments or multiple arguments in function
* @return string translated string
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dx
*/
function __dx($domain, $context, $msg, $args = null) {
if (!$msg) {
return;
}

$arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 2);
return I18n::translator($domain)->translate(
$msg,
['_context' => $context] + $arguments
);
}

}
26 changes: 26 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -328,6 +328,32 @@ public function testPluralContextFunction() {
);
}

/**
* Tests the __dx() function
*
* @return void
*/
public function testDomainContextFunction() {
I18n::translator('custom', 'en_US', function () {
$package = new Package('default');
$package->setMessages([
'letter' => [
'_context' => [
'character' => 'The letter {0}',
'communication' => 'She wrote a letter to {0}'
]
]
]);
return $package;
});

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

/**
* Tests that translators are cached for performance
*
Expand Down

0 comments on commit c51bf58

Please sign in to comment.