When using php setlocale() function it’s important to check the return value. I used to call it without checking it as in:

setlocale(LC_ALL, 'it_IT.UTF-8');

Without figuring out that if the locale it_IT.UTF-8 is not present in the system, the setlocale function fails and returns false.

The first idea I had to try to add the locale is calling:

$ dpkg-reconfigure locales

But nothing happens and the prompt is returned, because in my case the default question priority didn’t allow any question to be asked (like: what locales do you want to rebuild?). So I tried

$ dpkg-reconfigure -p low locales

With no improvements at all. So I resorted to the manual way editing the file /var/lib/locales/supported.d/local

and adding the entries I wanted in this case:

en_US.UTF-8 UTF-8
it_IT.UTF-8 UTF-8

So this time the reconfigure worked as I wanted:

# dpkg-reconfigure locales
Generating locales...
en_US.UTF-8... done
it_IT.UTF-8... done
Generation complete.