Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a640c30

Browse filesBrowse files
committed
feature #32415 [Translation] deprecate passing a null locale (Simperfit)
This PR was merged into the 4.4 branch. Discussion ---------- [Translation] deprecate passing a null locale | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | none <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | not needed <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against branch 4.4. - Legacy code removals go to the master branch. --> According to the discussion in #32386 (comment) it seems that allowing null here was not the right thing to do, so we are deprecating this behaviour. Commits ------- 088615d [Translation] deprecate passing a null locale
2 parents 51cf65e + 088615d commit a640c30
Copy full SHA for a640c30

File tree

Expand file treeCollapse file tree

4 files changed

+47
-1
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+47
-1
lines changed

‎src/Symfony/Component/Translation/MessageCatalogue.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/MessageCatalogue.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
3232
*/
3333
public function __construct(?string $locale, array $messages = [])
3434
{
35+
if (null === $locale) {
36+
@trigger_error(sprintf('Passing "null" to the first argument of the "%s" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), E_USER_DEPRECATED);
37+
}
38+
3539
$this->locale = $locale;
3640
$this->messages = $messages;
3741
}

‎src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ public function testGetLocale()
2323
$this->assertEquals('en', $catalogue->getLocale());
2424
}
2525

26+
/**
27+
* @group legacy
28+
* @expectedDeprecation Passing "null" to the first argument of the "Symfony\Component\Translation\MessageCatalogue::__construct" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.
29+
*/
30+
public function testGetNullLocale()
31+
{
32+
$catalogue = new MessageCatalogue(null);
33+
34+
$this->assertNull($catalogue->getLocale());
35+
}
36+
2637
public function testGetDomains()
2738
{
2839
$catalogue = new MessageCatalogue('en', ['domain1' => [], 'domain2' => [], 'domain2+intl-icu' => [], 'domain3+intl-icu' => []]);

‎src/Symfony/Component/Translation/Tests/TranslatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/TranslatorTest.php
+28-1Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,25 @@ public function testAddResourceInvalidLocales($locale)
184184
*/
185185
public function testAddResourceValidLocales($locale)
186186
{
187+
if (null === $locale) {
188+
$this->markTestSkipped('null is not a valid locale');
189+
}
187190
$translator = new Translator('fr');
188191
$translator->addResource('array', ['foo' => 'foofoo'], $locale);
189192
// no assertion. this method just asserts that no exception is thrown
190193
$this->addToAssertionCount(1);
191194
}
192195

196+
/**
197+
* @group legacy
198+
* @expectedDeprecation Passing "null" to the third argument of the "Symfony\Component\Translation\Translator::addResource" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.
199+
*/
200+
public function testAddResourceNull()
201+
{
202+
$translator = new Translator('fr');
203+
$translator->addResource('array', ['foo' => 'foofoo'], null);
204+
}
205+
193206
public function testAddResourceAfterTrans()
194207
{
195208
$translator = new Translator('fr');
@@ -367,10 +380,13 @@ public function testTransInvalidLocale($locale)
367380
}
368381

369382
/**
370-
* @dataProvider getValidLocalesTests
383+
* @dataProvider getValidLocalesTests
371384
*/
372385
public function testTransValidLocale($locale)
373386
{
387+
if (null === $locale) {
388+
$this->markTestSkipped('null is not a valid locale');
389+
}
374390
$translator = new Translator($locale);
375391
$translator->addLoader('array', new ArrayLoader());
376392
$translator->addResource('array', ['test' => 'OK'], $locale);
@@ -379,6 +395,17 @@ public function testTransValidLocale($locale)
379395
$this->assertEquals('OK', $translator->trans('test', [], null, $locale));
380396
}
381397

398+
/**
399+
* @group legacy
400+
* @expectedDeprecation Passing "null" to the third argument of the "Symfony\Component\Translation\Translator::addResource" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.
401+
*/
402+
public function testTransNullLocale()
403+
{
404+
$translator = new Translator(null);
405+
$translator->addLoader('array', new ArrayLoader());
406+
$translator->addResource('array', ['test' => 'OK'], null);
407+
}
408+
382409
/**
383410
* @dataProvider getFlattenedTransTests
384411
*/

‎src/Symfony/Component/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function addResource($format, $resource, $locale, $domain = null)
132132
$domain = 'messages';
133133
}
134134

135+
if (null === $locale) {
136+
@trigger_error(sprintf('Passing "null" to the third argument of the "%s" method has been deprecated since Symfony 4.4 and will throw an error in 5.0.', __METHOD__), E_USER_DEPRECATED);
137+
}
138+
135139
$this->assertValidLocale($locale);
136140

137141
$this->resources[$locale][] = [$format, $resource, $domain];

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.