willdurand / Negotiation

Content Negotiation tools for PHP.
https://williamdurand.fr/Negotiation/
MIT License
1.41k stars 62 forks source link

Confused by LanguageNegotiator's priorities #100

Closed ruudk closed 4 years ago

ruudk commented 6 years ago

Who does the LanguageNegotiator not return fr-fr when running the following code?

$negotiator = new \Negotiation\LanguageNegotiator();

$acceptLanguageHeader = 'fr-fr';
$priorities = array('fr', 'en');

$bestLanguage = $negotiator->getBest($acceptLanguageHeader, $priorities);
// $bestLanguage == null

In version 1.x it worked easier, because the LanguageNegotiator would always just return the best language based on the input. It gives me difficulty to upgrade to 2.x because I need to supply all possible combinations upfront.

Any ideas how to solve this?

ruudk commented 6 years ago

It turns out this is related to https://github.com/willdurand/Negotiation/issues/80. That issue is said to be related to https://github.com/willdurand/Negotiation/issues/76 which was opened by me in 2015 🙈

ruudk commented 6 years ago

Ahh, it seems that the fix for those issue was merged to 2.x master but was never tagged.

See: https://github.com/willdurand/Negotiation/compare/v2.3.1...2.x

Can we please get a 2.3.2 tag?

ruudk commented 6 years ago
$negotiator   = new LanguageNegotiator();
$negotiator->getBest('es-es', ['en', 'es-es', 'es']); // es-es
$negotiator->getBest('es-es', ['en', 'es', 'es-es']); // es

How come when I specify a strict locale (es-es) it returns es? Shouldn't it look for a exact match first, and if that's not the case, use a fallback based on priority?

ruudk commented 6 years ago
<?php

declare(strict_types=1);

use Negotiation\LanguageNegotiator;
use Webmozart\Assert\Assert;

include (__DIR__ . '/vendor/autoload.php');

$negotiator   = new LanguageNegotiator();

$one = $negotiator->getBest('es-es', ['en;q=0.1', 'es-es;q=0.9', 'es;q=0.5']);
Assert::same('es-es', $one->getNormalizedValue());

$two = $negotiator->getBest('es-es', ['en;q=0.1', 'es;q=0.5', 'es-es;q=0.9', ]);
Assert::same('es-es', $two->getNormalizedValue());

$three = $negotiator->getBest('es', ['en;q=0.1', 'es-es;q=0.9', 'es;q=0.5']);
Assert::same('es', $three->getNormalizedValue());
// Uncaught InvalidArgumentException: Expected a value identical to "es-es". Got: "es"

$four = $negotiator->getBest('es', ['en;q=0.1', 'es;q=0.5', 'es-es;q=0.9', ]);
Assert::same('es', $four->getNormalizedValue());
// Uncaught InvalidArgumentException: Expected a value identical to "es-es". Got: "es"
bbalet commented 6 years ago

This project is dead sniff :cry: