Setlocale (LC_CTYPE, "de_DE.UTF8") or setlocale (LC_CTYPE, "de_DE.UTF-8")?

setlocale(LC_CTYPE, "de_DE.UTF8") 

or

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

With or without dashes? I saw both in the answers.

+6
source share
4 answers

I would run locale -a and use the line it prints. 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 options if it doesn't work.

However, all versions should work: with a dash or without a line, in upper or lower case.

+3
source

The name of the encoding is UTF-8.

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

+2
source

Here's a pragmatic solution for its software programming. 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, it would be possible to work on our Mac computers, and the other would work on Linux CI boxes, so this solution forces the tests to pass on both.

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

0
source

In fact, you can pass an array of setLocale() values. I just ran into this problem when developing on OSX and testing on 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 that works is go. Docs: https://secure.php.net/setlocale

0
source

All Articles