Question

setlocale(LC_CTYPE, "de_DE.UTF8")

or

setlocale(LC_CTYPE, "de_DE.UTF-8")

With or without dash? I've seen both in answers.

Était-ce utile?

La solution

I'd run locale -a and use the string it outputs. On OS X 10.9, I get de_DE.UTF-8. On Debian 6, I get de_DE.utf8. dpkg-reconfigure locales uses de_DE.UTF-8 on the other hand. The safest way is probably to check the return value of setlocale and try different variants if it fails.

That said, all versions should usually work: with or without dash, uppercase or lowercase.

Autres conseils

The name of the encoding is UTF-8.

but in this case both parameters are correct and they are doing same job for you

Here's a pragmatic solution for resolving it programatically. When setlocale fails, it returns false.

if (!setlocale(LC_ALL, str_replace('utf-8', 'utf8', $locale)) 
 && !setlocale(LC_ALL, str_replace('utf8', 'utf-8', $locale))) {
   /* fail gracefully */
}
/* your locale has been set up properly

In my case one would work on our Macs and the other would work on the Linux CI boxes, so this solution makes tests pass on both.

Btw; beware of LC_ALL. Set the locale for the weakest scope you need it.

You can actually pass an array of values to setLocale(). I've just run into this problem while developing on OSX and testing at the hosting running on Linux. So now I do this: setLocale(LC_ALL, ["de.utf", "de_DE.utf", "de_DE.UTF-8", "de", "de_DE"]) The first one to match is a go. Docs: https://secure.php.net/setlocale

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top