What are common pitfalls to avoid when using setlocale in PHP for localization purposes?

Common pitfalls to avoid when using setlocale in PHP for localization purposes include not checking if the desired locale is available on the server, not handling errors properly, and not setting the LC_ALL category for complete localization. To solve these issues, always check if the desired locale is available, handle errors using try-catch blocks, and set the LC_ALL category for complete localization.

$locale = 'en_US.UTF-8';

if (in_array($locale, Locale::getAvailableLocales())) {
    try {
        setlocale(LC_ALL, $locale);
    } catch (Exception $e) {
        echo 'Error setting locale: ' . $e->getMessage();
    }
} else {
    echo 'Locale not available on this server';
}