Search code examples
phputf-8setlocale

setLocale not working with any language, correct local is installed


I want to set the correct locale to receive the correct translation for dates and time.

I tried:

$locale = setlocale (LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');

but this won't work. I already googled and what I found didn't help in my case. I tried changing LC_ALL to LC_TIME but it has no effect.

If I execute locale -a in the shell, i receive:

C C.UTF-8 POSIX de_DE.utf8

I also took a look in /etc/default/locale

LANG="de_DE.UTF-8"

In php.ini the timezone is also set to Europe/Berlin.
So what the heck am I doing wrong? On my local machine with EasyPhp it works fine.

To get the localized next month i use strftime:

 $nextMonth = explode(" ", strftime( '%m %B %Y', strtotime( '+1 month', $date ) ));

Instead of

Juni

I always receive

June

I tried other languages as well, but no difference.

I use php7.1 if it matters.

EDIT: I already found this post PHP setlocale has no effect but nothing worked for me.


Solution

  • Don't be fooled by the ".utf8" part of the locale. Setlocale is for single byte functions only, they don't work well with Unicode. Also, it matters which operating system you are using, and depending on the operating system, it matters if you use the TS or NTS build of PHP.

    For internationalisation / Unicode I'd recommend using the Intl extention, it has full Unicode support and a very complete calendar IntlCalendar.