Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Translation] Fix test, null is valid according to the tests #32386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(TranslatorInterface $translator = null, IntlFormatte
/**
* {@inheritdoc}
*/
public function format(string $message, string $locale, array $parameters = [])
public function format(string $message, ?string $locale, array $parameters = [])
{
if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($message, $parameters, null, $locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ interface MessageFormatterInterface
*
* @return string
*/
public function format(string $message, string $locale, array $parameters = []);
public function format(string $message, ?string $locale, array $parameters = []);
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ArrayLoader implements LoaderInterface
/**
* {@inheritdoc}
*/
public function load($resource, string $locale, string $domain = 'messages')
public function load($resource, ?string $locale, string $domain = 'messages')
{
$resource = $this->flatten($resource);
$catalogue = new MessageCatalogue($locale);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class FileLoader extends ArrayLoader
/**
* {@inheritdoc}
*/
public function load($resource, string $locale, string $domain = 'messages')
public function load($resource, ?string $locale, string $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IcuDatFileLoader extends IcuResFileLoader
/**
* {@inheritdoc}
*/
public function load($resource, string $locale, string $domain = 'messages')
public function load($resource, ?string $locale, string $domain = 'messages')
{
if (!stream_is_local($resource.'.dat')) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IcuResFileLoader implements LoaderInterface
/**
* {@inheritdoc}
*/
public function load($resource, string $locale, string $domain = 'messages')
public function load($resource, ?string $locale, string $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ interface LoaderInterface
* @throws NotFoundResourceException when the resource cannot be found
* @throws InvalidResourceException when the resource cannot be loaded
*/
public function load($resource, string $locale, string $domain = 'messages');
public function load($resource, ?string $locale, string $domain = 'messages');
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Loader/QtFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class QtFileLoader implements LoaderInterface
/**
* {@inheritdoc}
*/
public function load($resource, string $locale, string $domain = 'messages')
public function load($resource, ?string $locale, string $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class XliffFileLoader implements LoaderInterface
/**
* {@inheritdoc}
*/
public function load($resource, string $locale, string $domain = 'messages')
public function load($resource, ?string $locale, string $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,10 @@ public function addLoader(string $format, LoaderInterface $loader)
*
* @param string $format The name of the loader (@see addLoader())
* @param mixed $resource The resource name
* @param string $locale The locale
* @param string $domain The domain
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function addResource(string $format, $resource, string $locale, string $domain = null)
public function addResource(string $format, $resource, ?string $locale, string $domain = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this could work. The assertValidLocale() call below will throw an exception when null is pass for the $locale argument, won't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we look at the test null is a valid locale, maybe the tests are wrong ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertValidLocale does not seems to throw on null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think we should deprecate the possibility to pass null here. Looking at how it is used it doesn't seem like it was intended to be supported.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addResource and similar methods changed here require a $locale argument which does not have a default null value and does not implement the fallback to the defaut locale like for example

if (null === $locale) {
$locale = $this->getLocale();
} else {
$this->assertValidLocale($locale);

for this reason, I agree that the $locale should not be nullable and calling $this->resources[$locale] with null is also strange. so I'm in favor of deprecating null in those places and not making it nullable.

Copy link
Contributor

@Tobion Tobion Jul 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the locale is nullable in

public function __construct(?string $locale, array $messages = [])
but the getter is not nullable.
* @return string The locale
*/
public function getLocale();

It seems usages of getLocale would also not expect null. So the nullable in the MessageCatalogue constructor should also be deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to do it

{
if (null === $domain) {
$domain = 'messages';
Expand Down Expand Up @@ -248,7 +246,7 @@ protected function getLoaders()
/**
* @param string $locale
*/
protected function loadCatalogue(string $locale)
protected function loadCatalogue(?string $locale)
{
if (null === $this->cacheDir) {
$this->initializeCatalogue($locale);
Expand All @@ -260,7 +258,7 @@ protected function loadCatalogue(string $locale)
/**
* @param string $locale
*/
protected function initializeCatalogue(string $locale)
protected function initializeCatalogue(?string $locale)
{
$this->assertValidLocale($locale);

Expand Down