-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Make Translator::getLocale() behave the same as TranslatorTrait::getLocale() #38230
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
jschaedl
wants to merge
1
commit into
symfony:master
from
jschaedl:translation-issue-38124-follow-up
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make Translator::getLocale() behave the same as TranslatorTrait::getL…
…ocale()
- Loading branch information
commit 59b35355815b2e27c74a82968d5cc2ce3c36bf49
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,10 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA | |
*/ | ||
public function __construct(string $locale, MessageFormatterInterface $formatter = null, string $cacheDir = null, bool $debug = false, array $cacheVary = []) | ||
{ | ||
if ('' === $locale) { | ||
trigger_deprecation('symfony/translator', '5.2', sprintf('Passing "" as the $locale argument to %s::%s() is deprecated, it will throw an InvalidArgumentException in version 6.0.', static::class, __METHOD__)); | ||
} | ||
|
||
$this->setLocale($locale); | ||
|
||
if (null === $formatter) { | ||
|
@@ -158,7 +162,7 @@ public function setLocale(string $locale) | |
*/ | ||
public function getLocale() | ||
{ | ||
return $this->locale; | ||
return $this->locale ?: \Locale::getDefault(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see also #38279, we'll introduce the same issue now :) |
||
} | ||
|
||
/** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this case makes sense. It conflicts, from a logical pov, with the change in getLocale(). It's one or the other to me, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it depends.
If we say injecting an empty locale in the constructor is ok, because we make sure that the default locale will be returned in
getLocale()
anyway, then yes a deprecation is not needed. Only Tests need some adjustments then.But if we say an empty locale is not valid, we could validate it in
assertValidLocale()
and also simplify getLocale() by removing the\?: Locale::getDefault()
. But this would be a BC break if we do it right now, so I only introduced the deprecation for now and would do the adjustments ofassertValidLocale()
andgetLocale()
in version 6.I personally would prefer the second approach, but I also might miss something :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should patch
assertValidLocale
IMHO. deprecating an empty string there, throwing in 6.0given the locale is not a nullable in this case, there's no need for fallback to
\Locale::getDefault()
as wellthus the deprecated case (an empty string) keeps working in 5.x, as currently that's a BC break.
we should proboably call
$this->setLocale($locale)
at construct (to ensure validation), meaning the deprecation will automatically happen as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personnaly take the other way, for consistency with TranslatorTrait, and because that's what provides the most power to users (they can decide to rely on
\Locale::getDefault()
)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it can also take a nullable againlet's not :)but yes,
?: getDefault()
works, at most it hides"0"
sneaking into the system. But it's consistent behavior with the trait, i agree.IMHO the service differs from the trait, in a way doing
new Translator(Locale::getDefault()
makes sense, otherwise we have to document the empty string behavior as well i figured.The trait needs a hardcoded fallback mechanism, due the lack of a constructor.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we still need a deprecation here (and in setLocale) then
and fallback as of 6.0
... so yes, the current behavior conflicts 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the point of the deprecation. Let's move on.