Skip to content
Permalink
Browse files

Handle explict null values in translation functions.

When we get an explicit null we should use that as a translation value
that is interpolated into the message. This had to be done in 3.4 as we
have access to `...` which makes this change simpler to implement.

Refs #10087
  • Loading branch information...
markstory committed Jan 28, 2017
1 parent 35aa70e commit 90fb16d543dc90b9aceb24b7e18f6aefbf70c11a
Showing with 64 additions and 23 deletions.
  1. +32 −23 src/I18n/functions.php
  2. +32 −0 tests/TestCase/I18n/I18nTest.php
@@ -28,10 +28,11 @@ function __($singular, ...$args)
if (!$singular) {
return null;
}
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
$arguments = func_num_args() === 2 ? (array)$args[0] : $args;
return I18n::translator()->translate($singular, $arguments);
return I18n::translator()->translate($singular, $args);
}
}
@@ -53,12 +54,13 @@ function __n($singular, $plural, $count, ...$args)
if (!$singular) {
return null;
}
$arguments = func_num_args() === 4 ? (array)$args[0] : $args;
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
return I18n::translator()->translate(
$plural,
['_count' => $count, '_singular' => $singular] + $arguments
['_count' => $count, '_singular' => $singular] + $args
);
}
@@ -79,9 +81,11 @@ function __d($domain, $msg, ...$args)
if (!$msg) {
return null;
}
$arguments = func_num_args() === 3 ? (array)$args[0] : $args;
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
return I18n::translator($domain)->translate($msg, $arguments);
return I18n::translator($domain)->translate($msg, $args);
}
}
@@ -105,12 +109,13 @@ function __dn($domain, $singular, $plural, $count, ...$args)
if (!$singular) {
return null;
}
$arguments = func_num_args() === 5 ? (array)$args[0] : $args;
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
return I18n::translator($domain)->translate(
$plural,
['_count' => $count, '_singular' => $singular] + $arguments
['_count' => $count, '_singular' => $singular] + $args
);
}
@@ -133,10 +138,11 @@ function __x($context, $singular, ...$args)
if (!$singular) {
return null;
}
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
$arguments = func_num_args() === 3 ? (array)$args[0] : $args;
return I18n::translator()->translate($singular, ['_context' => $context] + $arguments);
return I18n::translator()->translate($singular, ['_context' => $context] + $args);
}
}
@@ -161,12 +167,13 @@ function __xn($context, $singular, $plural, $count, ...$args)
if (!$singular) {
return null;
}
$arguments = func_num_args() === 5 ? (array)$args[0] : $args;
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
return I18n::translator()->translate(
$plural,
['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments
['_count' => $count, '_singular' => $singular, '_context' => $context] + $args
);
}
@@ -190,12 +197,13 @@ function __dx($domain, $context, $msg, ...$args)
if (!$msg) {
return null;
}
$arguments = func_num_args() === 4 ? (array)$args[0] : $args;
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
return I18n::translator($domain)->translate(
$msg,
['_context' => $context] + $arguments
['_context' => $context] + $args
);
}
@@ -222,12 +230,13 @@ function __dxn($domain, $context, $singular, $plural, $count, ...$args)
if (!$singular) {
return null;
}
$arguments = func_num_args() === 6 ? (array)$args[0] : $args;
if (isset($args[0]) && is_array($args[0])) {
$args = $args[0];
}
return I18n::translator($domain)->translate(
$plural,
['_count' => $count, '_singular' => $singular, '_context' => $context] + $arguments
['_count' => $count, '_singular' => $singular, '_context' => $context] + $args
);
}
@@ -247,6 +247,38 @@ public function testBasicTranslateFunction()
$this->assertEquals('The red dog, and blue cat', __('The %s dog, and %s cat', 'red', 'blue'));
}
/**
* Tests the __() functions with explict null params
*
* @return void
*/
public function testBasicTranslateFunctionsWithNullParam()
{
$this->assertEquals('text {0}', __('text {0}'));
$this->assertEquals('text ', __('text {0}', null));
$this->assertEquals('text {0}', __n('text {0}', 'texts {0}', 1));
$this->assertEquals('text ', __n('text {0}', 'texts {0}', 1, null));
$this->assertEquals('text {0}', __d('default', 'text {0}'));
$this->assertEquals('text ', __d('default', 'text {0}', null));
$this->assertEquals('text {0}', __dn('default', 'text {0}', 'texts {0}', 1));
$this->assertEquals('text ', __dn('default', 'text {0}', 'texts {0}', 1, null));
$this->assertEquals('text {0}', __x('default', 'text {0}'));
$this->assertEquals('text ', __x('default', 'text {0}', null));
$this->assertEquals('text {0}', __xn('default', 'text {0}', 'texts {0}', 1));
$this->assertEquals('text ', __xn('default', 'text {0}', 'texts {0}', 1, null));
$this->assertEquals('text {0}', __dx('default', 'words', 'text {0}'));
$this->assertEquals('text ', __dx('default', 'words', 'text {0}', null));
$this->assertEquals('text {0}', __dxn('default', 'words', 'text {0}', 'texts {0}', 1));
$this->assertEquals('text ', __dxn('default', 'words', 'text {0}', 'texts {0}', 1, null));
}
/**
* Tests the __() function on a plural key
*

0 comments on commit 90fb16d

Please sign in to comment.
You can’t perform that action at this time.