Skip to content

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

Conversation

jschaedl
Copy link
Contributor

@jschaedl jschaedl commented Sep 18, 2020

Q A
Branch? master
Bug fix? yes
New feature? no
Deprecations? yes
Tickets Relates to #38124
License MIT
Doc PR -

This is a follow-up PR on #38127 and a proposal for discussion.

While working on #38127 I realized that Translator::getLocale() and TranslatorTrait::getLocale() behave differently. For the Translator an empty locale is a valid locale and for the TranslatorTrait an empty locale will result in getting the default locale when calling getLocale().

To make things more consistent, I thought it would be a good idea to deprecate the injection of en empty locale, so we can add a proper validation in assertValidLocale(string $locale) and throw an InvalidArgumentException in version 6.

/cc @ro0NL

@jschaedl jschaedl force-pushed the translation-issue-38124-follow-up branch from fdb5163 to 59b3535 Compare September 18, 2020 09:08
@jschaedl jschaedl changed the title Make Translator::getLocale() behave the same like TranslatorTrait::getLocale() Make Translator::getLocale() behave the same as TranslatorTrait::getLocale() Sep 18, 2020
@@ -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__));
Copy link
Member

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?

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 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 of assertValidLocale() and getLocale() in version 6.

I personally would prefer the second approach, but I also might miss something :-)

Copy link
Contributor

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.0

given the locale is not a nullable in this case, there's no need for fallback to \Locale::getDefault() as well

thus 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.

Copy link
Member

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())

Copy link
Contributor

@ro0NL ro0NL Sep 18, 2020

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 again let'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.

Copy link
Contributor

@ro0NL ro0NL Sep 18, 2020

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

if (!$locale) {
    trigger_deprecation('will be Locale::getDefault() (currently "...") in 6.0');
    // $locale = Locale::getDefault()
}

and fallback as of 6.0

... so yes, the current behavior conflicts 😅

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 the point of the deprecation. Let's move on.

@xabbuh xabbuh added this to the next milestone Sep 18, 2020
@@ -158,7 +162,7 @@ public function setLocale(string $locale)
*/
public function getLocale()
{
return $this->locale;
return $this->locale ?: \Locale::getDefault();
Copy link
Contributor

@ro0NL ro0NL Sep 23, 2020

Choose a reason for hiding this comment

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

see also #38279, we'll introduce the same issue now :)

fabpot added a commit that referenced this pull request Sep 24, 2020
This PR was merged into the 4.4 branch.

Discussion
----------

[Contracts][Translation] Optional Intl dependency

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->
same as #38283, and should help streamlining #38230

Commits
-------

d65d39d [Contracts][Translation] Optional Intl dependency
@fabpot
Copy link
Member

fabpot commented Sep 24, 2020

@jschaedl Can you remove the deprecation and fix the getLocale method?

@nicolas-grekas
Copy link
Member

On second thought, I agree with @ro0NL: this is a behavioral BC break.

The only path forward would be deprecating the empty locale but still returning the empty string when passed. Then in v6 we would return the global value.

BUT, this looks like a quite costly path when the argument in favor is "consistency".

I'm therefor closing, we need to focus on actual use cases.

@jschaedl
Copy link
Contributor Author

@fabpot

Can you remove the deprecation and fix the getLocale method?

I actually already did: master...jschaedl:translation-issue-38124-follow-up but the PR is closed now.

@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
fabpot added a commit that referenced this pull request Jul 20, 2021
…s-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Translation] fix fallback to Locale::getDefault()

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Replaces #42128 and #38230

Provides consistent behavior with `TranslatorTrait::getLocale()`.

Commits
-------

20120a3 [Translator] fix fallback to Locale::getDefault()
@nicolas-grekas
Copy link
Member

Fixed in #42130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants