Skip to content
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

Allow customizing picked up cart's locale #644

Merged
merged 2 commits into from
Jun 1, 2020
Merged

Conversation

dnna
Copy link
Contributor

@dnna dnna commented Apr 15, 2020

Currently when picking up a new cart, the default channel locale is always used.

This PR fixes this by enabling the use of an optional ?locale URL parameter to customize the cart's locale.

@dnna dnna requested a review from a team as a code owner April 15, 2020 14:39
@dnna dnna changed the title Added locale from request in cart creation Allow customizing picked up cart's locale Apr 15, 2020
Copy link
Member

@lchrusciel lchrusciel left a comment

Choose a reason for hiding this comment

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

Hey Dimosthenis,

thanks for your proposal. Changing cart locale makes absolute sense. However, instead of adding query parameter to the request, it should be fetched from LocaleContext to allow different ways of fetching it.

@dnna
Copy link
Contributor Author

dnna commented Apr 16, 2020

I have made the requested changes so that LocaleContext is used instead of request query. To make this work, I had to create a new ChannelAndLocaleBasedRequestInterface and a ChannelAndLocaleBasedCommandProvider.

For ChannelAndLocaleBasedRequestInterface I extended the original ChannelBasedRequestInterface instead of having a completely separate interface in order to minimize BC breaks.

@mamazu
Copy link
Member

mamazu commented Apr 17, 2020

Yeah localization is indeed still a problem in the ShopApi. But this looks good. I would prefer to move it in to the main provider and then add an interface that only specifies the setLocale function. And then in the provider if the interface is implemented call the function.

class PickUpCartCommand implements LocaleNeededInterface {
    private $localeCode;

    // Normal code
    public function setLocale(string $localeCode): void
    {
        $this->localeCode = $localeCode; 
    }
}

and then

public function getCommand(Request $httpRequest): CommandInterface
{
    $command = $this->transformHttpRequest($httpRequest)->getCommand();
    if ($command instanceof LocaleNeededInterface) {
        // Get locale somehow
        $command->setLocale($locale);
    }
    return $command;
}

Maybe even the LocaleAwareInterface from the Symfony translator. What do you think?

@dnna
Copy link
Contributor Author

dnna commented Apr 18, 2020

That approach makes a lot of sense. I'll try to implement it that way and get back.

@dnna
Copy link
Contributor Author

dnna commented Apr 18, 2020

@mamazu I have reimplemented it the way you mentioned. I didn't use Symfony's LocaleAwareInterface because I was afraid it might be somehow integrated with Symfony and get unexpectedly triggered by the container or request listeners, so I created a new LocaleAwareCommandInterface.

I implemented it in a way that allows localeContext to be nullable to avoid BC breaks if someone has decorated the provider or extended the command. I can't see a way that it might break but if you do please let me know.

@mamazu
Copy link
Member

mamazu commented Apr 19, 2020

Looks good. This could be then extended to all other commands.

@mamazu mamazu merged commit f658169 into Sylius:master Jun 1, 2020
@mamazu
Copy link
Member

mamazu commented Jun 1, 2020

Thank you, Dimosthenis! 🎉

{
Assert::implementsInterface($requestClass, ChannelBasedRequestInterface::class);

$this->requestClass = $requestClass;
$this->validator = $validator;
$this->localeContext = $localeContext;
Copy link
Member

Choose a reason for hiding this comment

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

We should trigger error if it won't be defined

Copy link
Member

Choose a reason for hiding this comment

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

You mean because of backwards compatibility?

Copy link
Member

Choose a reason for hiding this comment

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

Just to inform people that it will be in future required and not passing it is deprecated

Copy link
Member

Choose a reason for hiding this comment

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

Agreed.

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

Successfully merging this pull request may close these issues.

3 participants