setlocale() 返回 false
我已经在我的网络服务器上安装了荷兰语本地(nl_NL utf8
)(当我执行locale -a
时,我看到nl_NL utf8
所以这没关系) 。
但是,我网页上的日期是英文的(我已将 setlocale(LC_ALL, 'nl_NL');
放在页面顶部)。我读过,当你在编译 php 后安装语言环境包时,我必须重新编译 php。
但是有没有其他解决方案,无需重新编译php,让它工作?
谢谢!
I have installed the dutch local (nl_NL utf8
) on my webserver (when I execute locale -a
I see nl_NL utf8
so this is allright).
However, the dates on my webpage are in english (I have put setlocale(LC_ALL, 'nl_NL');
in the top of my pages). I have read when you install a locale package after compiling php, I have to recompile php.
But is there any other solution, without recompiling php, to let it work?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在这里总结了我所发现的类似问题,因为这里显然没有建设性的答案(实际上是一个错误的答案:
setlocale()
确实改变了strftime()<的输出/code>),它对其他人可能有用。
PHP 手册说
因此,对于诊断系统,首先使用 shell 或 PHP
system('locale -a')
检查是否安装了正确的语言环境(nl_NL、nl_NL.UTF-8 等)。在某些 ubuntu 系统上,有一个用于安装语言环境的脚本,例如。/usr/share/locales/install-language-pack nl_NL
但也可以考虑使用 apt-get 安装。(对于外来语言环境,还要检查是否支持语言环境:在某些系统上
/usr/share/i18n/SUPPORTED
)。然后,您可以使用
var_dump(setlocale('nl_NL'));
获取 setlocale() 的输出(因为setlocale()
单独不会输出任何内容)。解决方法可能是使用 locale-gen nl_NL, nl_NL 重新生成语言环境。
此后,某些系统可能需要
update-locale
(和dpkg-reconfigure locales
)。I summarize here what I have found facing a similar problem, since here there have been apparently no constructive answer (a wrong one, indeed:
setlocale()
does change the output ofstrftime()
) and it can be useful for others.PHP manual says
so for the diagnosys, first check that the right locale is installed (nl_NL, nl_NL.UTF-8, etc.), either using a shell or in PHP
system('locale -a')
. On some ubuntu systems there is a script to install a locale, eg./usr/share/locales/install-language-pack nl_NL
but installing with apt-get may be considered too.(For exotic locales, also check that locale is supported: on some systems
/usr/share/i18n/SUPPORTED
).Then you can get the output of setlocale() using
var_dump(setlocale('nl_NL'));
(sincesetlocale()
alone doesn't output anything).For the cure could be to regenerate locale with locale-gen nl_NL, nl_NL
Thereafter,
update-locale
(anddpkg-reconfigure locales
) can be required on some systems.这可能是设计使然:
setlocale()
不会自动更改使用date()
和输出的日期格式 strftime()。它的作用是本地化工作日和月份名称,但没有其他作用。
您能否展示一些如何输出日期以及它们如何无法转换的示例?
This may be by design:
setlocale()
does not automatically change the output of the format of dates output usingdate()
andstrftime()
. What it does is localize the weekday and month names, but nothing else.Can you show some examples of how you output dates, and how they fail to get converted?
我想补充另一件事要牢记。
如果未安装语言环境并且您刚刚安装了新语言环境(例如通过 sudo locale-gen nl_NL.utf8 和 sudo update-locale ),您还需要确保重新启动 php-fpm,例如,如果您运行的是 PHP 8.1,请运行
sudo service php8.1-fpm restart
。我注意到设置区域设置在控制台中可以正常工作,但在网络应用程序中则不行。区域设置显然是在进程中缓存的。
I would like to add another thing to bear in mind.
If the locale wasn't installed and you just installed the new locale (e.g. via
sudo locale-gen nl_NL.utf8
andsudo update-locale
), you also need to make sure to restart php-fpm, e.g. by runningsudo service php8.1-fpm restart
if you are running PHP 8.1.I noticed setting the locale would work just fine in the console, but not in the web app. Locales are obviously cached within the process.