Gettext will always use system default locale

It's an issue that's been acknowledged and partially fixed by the PHP team.

It's a fairly technical thing, apparently related to the way environment variables (on which gettext heavily depends) are handled by the underlying platform. Also, something has changed in the Visual C Runtime libraries from VC9 to VC11 that affects all this.

To sum up:

  • Non thread-safe builds have been fixed in the source tree on 21st Nov. 2014.
  • Thread-safe builds (e.g. Apache module) haven't and there isn't a clear solution in sight.

I had same problem with PHP 5.6.30 VC11 Theard Safe on Windows 10. Workaround found and fix this issue here by sirio3mil.

Apparently PHP with TS can access only Locale language folder. So when setlocale and putenv function is call with another language than system's one, folder with .mo and .po cannot be read.

Workaround is to have only one language folder with system language and multiple pairs of .mo/.po files for each translated languages. Domain will be set with wanted language.

Example with Swiss French, German and Italian:

Folder structure

\Locale\fr_CH\LC_MESSAGES

  • fr_CH.mo + fr_CH.po // system language
  • de_CH.mo + de_CH.po
  • it_CH.mo + it_CH.po

Code

$lang = 'fr_CH' or 'de_CH' or 'it_CH'

bindtextdomain($lang, '.\Locale');
textdomain($lang);
bind_textdomain_codeset($lang, 'UTF-8');
setlocale (LC_ALL, $lang);
putenv('LC_ALL=' . $lang);

The key is to use a Non Thread Safe (=NTS) version of PHP.

Unfortunatelly Windows and PHP does not handle well the environments for threaded processes, so the putenv('LC_ALL='.$locale); command does not work.

Finally I end up with Apache 2.4 + FCGID + PHP 7.1 NTS, which works well now on Windows 7, and it is a non thread safe installation.

A step by step instruction on how to install a such system is here: https://www.youtube.com/watch?v=UXrJPrGaPB0

I have used the VC14 and x64 version for all components (VC is abbreviation for "Microsoft Visual C++ Redistributable"). For this I first installed VC14, downloaded from here: https://www.microsoft.com/en-us/download/details.aspx?id=48145