Skip to content

Commit

Permalink
Handle explict null values in translation functions.
Browse files Browse the repository at this point in the history
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 90fb16d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/I18n/functions.php
Expand Up @@ -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);
}

}
Expand All @@ -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
);
}

Expand All @@ -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);
}

}
Expand All @@ -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
);
}

Expand All @@ -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);
}

}
Expand All @@ -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
);
}

Expand All @@ -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
);
}

Expand All @@ -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
);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/I18n/I18nTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 90fb16d

Please sign in to comment.