vluzrmos / laravel-language-detector

Detect the language for your application using browser preferences, subdomains or route prefixes.
108 stars 25 forks source link

LanguageDetector::apply() does not update cookie (enhancement suggestion) #24

Closed swayok closed 4 years ago

swayok commented 7 years ago

I've just had misunderstanding with usage of LanguageDetector::apply() method when I tried to replace locale manually due to user preferences. I was wondering why it did not work after page reload and digged into code where I found that cookie is not updated in apply() if it is already exist and I need to call addCookieToQueue() method to do that. I understand the reason behind this but it is not clear that I need to call addCookieToQueue() manually. I suggest to add $updateCookie = false argument to apply() so it will look like:

public function apply($locale, $updateCookie = false)
{
    $this->translator->setLocale($locale);

    if ($updateCookie || !$this->getLanguageFromCookie()) {
        $this->addCookieToQueue($locale);
    }

    $this->applyCallbacks($locale);
}

This will clarify the usage of apply() method

vluzrmos commented 7 years ago

Thanks for that suggestion, could you make a PR? 😄

vluzrmos commented 7 years ago

You should use:


public function apply($locale)
{
    $this->translator->setLocale($locale);

    $this->addCookieToQueue($locale);

    $this->applyCallbacks($locale);
}

The method addCookieToQueue already verify the usage of cookies.

vluzrmos commented 7 years ago

Oops, my mistake!

Do as you wrote first