PHP library to work with POSIX locale identifiers.
composer require xdg/locale
use Xdg\Locale\Locale;
// from a POSIX locale identifier
$locale = Locale::of('ca_ES@valencia');
// from an IETF (BCP47) language tag
$locale = Locale::of('zh-Hans-CN');
// On Windows, windows-specific locale identifiers are accepted
$locale = Locale::of('French_France.1252');
Locale matching is performed using the algorithm described in the XDG Desktop Entry specification.
use Xdg\Locale\Locale;
$locale = Locale::of('fr_FR.UTF-8')
$locale->matches('fr'); // => true
$locale->matches('fr_BE'); // => false
Use Locale::select()
to select the best match from a list of candidates.
use Xdg\Locale\Locale;
$locale = Locale::of('fr_FR.UTF-8')
$locale->select(['fr_CA', 'fr_BE', 'fr']); // => 'fr'