我假设“on the web”指的是 Linux 下的 Apache Web 服务器...
在这种情况下setlocale(LC_ALL, 0)返回C的原因如下:
如果您从 Apache 服务器检查 /etc/apache2/envvars,您会发现以下几行:
## The locale used by some modules like mod_dav
export LANG=C
## Uncomment the following line to use the system default locale instead:
#. /etc/default/locale
因此,phpinfo() 将产生以下输出:
如果修改/etc/apache2/envvars中的行如下...
## The locale used by some modules like mod_dav
# export LANG=C
## Uncomment the following line to use the system default locale instead:
. /etc/default/locale
并重新启动 Apache。然后 Apache 将使用您操作系统中的区域设置和语言设置(您可以在终端中使用 locale 命令检查它们的操作系统设置)。作为上述更改的结果,phpinfo() 将在定义了 de_CH.UTF-8 语言环境的系统上产生例如以下输出:
但您最终可能仍会让setlocale(LC_ALL, 0) 返回C,或者至少将一些LC_ 变量设置为C 例如LC_CTYPE=de_CH.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C"
为了确保 PHP 使用操作系统中的区域设置,您必须调用 setlocale(LC_ALL, "")。
在https://www.php.net/manual/en/function.setlocale.php 下的setlocale 的手册声明如下:
// If locales is the empty string "", the locale names will be set from
// the values of environment variables with the same names as the above
// categories, or from "LANG".
// On Windows, setlocale(LC_ALL, '') sets the locale names from the
// system's regional/language settings (accessible via Control Panel).
最后,您的 setlocale(LC_ALL, 0) 返回在您的操作系统上配置的值,例如de_CH.UTF-8 :-)