PHP setlocale 没有效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10909911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP setlocale has no effect
提问by toesslab
The setlocale()function doesn't set the desired language (German).
该setlocale()函数未设置所需的语言(德语)。
The goal is to output month names.
目标是输出月份名称。
This is my test code with trials so far:
这是我到目前为止的试验测试代码:
<?php
date_default_timezone_set('Europe/Berlin');
setlocale(LC_ALL, 'de_DE.utf8');
// Or
setlocale(LC_ALL, 'de_DE@euro');
// Or
setlocale(LC_ALL, 'de_DE');
// Or
setlocale(LC_ALL, 'de');
// Or
setlocale(LC_ALL, 'ge');
echo strftime('%B');
Output:
输出:
June
六月
instead of
代替
Juni
朱尼
Any suggestions?
有什么建议?
- I don't have ssh or other shell access.
- The script is running on a linux server.
- 我没有 ssh 或其他 shell 访问权限。
- 该脚本在 linux 服务器上运行。
PHP version 5.6
PHP 5.6 版
采纳答案by toesslab
This solution might help if you don't have shell access to the server.
如果您没有对服务器的 shell 访问权限,此解决方案可能会有所帮助。
If you have shell access, then Benjamin Seiller's answeris the best!
如果您有 shell 访问权限,那么Benjamin Seiller 的答案是最好的!
As I don't have any other possibilities (shell), I've found a solution with only PHP by using the IntlDateFormatter class.
由于我没有任何其他可能性(shell),我通过使用IntlDateFormatter 类找到了一个只有 PHP 的解决方案。
<?php
// Example vars
$month = '6';
$year = '2014';
$fmt = new IntlDateFormatter('de_DE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Berlin',
IntlDateFormatter::GREGORIAN);
$lastMonth = mktime(0, 0, 0, $month -1, 1, $year);
$showLastMonth = $fmt->format($lastMonth);
echo $showLastMonth;
回答by Benjamin Seiller
Is is quite likely that the German locale is not installed on the server your running the script on - do you have shell access to the server? Then try
很可能在您运行脚本的服务器上未安装德语区域设置 - 您是否具有对服务器的 shell 访问权限?然后试试
locale -a
to see which locales are installed. Also have a look here Is it feasible to rely on setlocale, and rely on locales being installed?
查看安装了哪些语言环境。也看看这里依赖 setlocale 和依赖安装的语言环境是否可行?
回答by svassr
Your code is correct. You probably just have to install the correct language package on the server you are running the script on.
你的代码是正确的。您可能只需要在运行脚本的服务器上安装正确的语言包。
In the terminal, if the language you want to use is not listed after running the command sudo locale -a, then you'll have to install the missing language by running the following command :
在终端中,如果运行命令后未列出您要使用的语言sudo locale -a,则必须通过运行以下命令来安装缺少的语言:
sudo /usr/share/locales/install-language-pack de_DE
(sudohere is optional if your user has root permissions)
(sudo如果您的用户具有 root 权限,这里是可选的)
Then if you double check with sudo locale -ayou should see de_DE.utf8.
然后,如果您仔细检查,sudo locale -a您应该会看到de_DE.utf8.
If you want to install french language package run
如果要安装法语包,请运行
sudo /usr/share/locales/install-language-pack fr_FR
Then you'll be allowed to set your language to these in PHP by using setlocale(...)exactly like you did it.
然后,您将被允许使用setlocale(...)与您所做的完全一样的方式在 PHP 中将您的语言设置为这些。
Note:If you are in a non utf8 project you'll need to generate other formats from installed packages. Here is how to proceed on ubuntu (this work on debian as well) :
注意:如果您在非 utf8 项目中,则需要从已安装的包生成其他格式。以下是如何在 ubuntu 上进行(这项工作也适用于 debian):
edit /var/lib/locales/supported.d/csand add the following lines
编辑/var/lib/locales/supported.d/cs并添加以下几行
fr_FR.iso88591 ISO-8859-1
fr_CA.iso88591 ISI-8859-1
and run
并运行
sudo dpkg-reconfigure locales
Then by running again sudo locale -ayou should see both fr_FR.iso88591and fr_CA.iso88591in the list and you can use it in php by calling setlocale(LC_ALL, 'fr_FR.iso88591');
然后再次运行,sudo locale -a您应该在列表中看到fr_FR.iso88591和fr_CA.iso88591,您可以通过调用在 php 中使用它setlocale(LC_ALL, 'fr_FR.iso88591');
回答by Rico Neitzel
For those coming here looking for date()doesn't localize month and weekday names:
对于那些来这里寻找date()不本地化月份和工作日名称的人:
== Pay Attention ==
== 注意==
date() is only able to return month/day names in English and won't be able to give you translations for other languages.
date() 只能返回英文的月/日名称,无法为您提供其他语言的翻译。
Use strftime()instead!
使用strftime(),而不是!
回答by sephoy08
Try this one:
试试这个:
date_default_timezone_set('Europe/Berlin');
$loc=setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
echo strftime('%B');
回答by trojan
PHP manual page for 'setlocale' sais:
Note: The return value of setlocale() depends on the system that PHP is running. It returns exactly what the system setlocale function returns.
注意:setlocale() 的返回值取决于 PHP 运行的系统。它准确地返回系统 setlocale 函数返回的内容。
If you have root access (debian) here is the solution:
如果您有 root 访问权限(debian),这里是解决方案:
edit
编辑
/etc/locale.gen
You can add/remove which locales you need to use. After saving the file run:
您可以添加/删除需要使用的语言环境。保存文件后运行:
locale-gen
and you should be fine. On my server I had to restart Apache to see changes.
你应该没事的。在我的服务器上,我必须重新启动 Apache 才能看到更改。
回答by Michael K?fer
In my case this does not work:
在我的情况下,这不起作用:
setlocale(LC_ALL, 'de_AT'); // false
while this does work:
虽然这确实有效:
Locale::setDefault('de_AT'); // true
and this does work:
这确实有效:
setlocale(LC_ALL, 'de_AT.utf-8'); // true
Output of locale program:
语言环境程序的输出:
$ locale -a
[...]
C.UTF-8
de_AT.utf-8
de_DE.utf-8
en_AG
[...]
回答by Bernd Ott
Depending on the underlying OS "de_DE"and others maybe the wrong string.
取决于底层操作系统"de_DE"和其他人,可能是错误的字符串。
Under Windows refer this lists:
在 Windows 下,请参阅此列表:
- http://msdn.microsoft.com/en-us/library/39cwe7zf(v=vs.90).aspx
- http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.90%29.aspx
- http://msdn.microsoft.com/en-us/library/39cwe7zf(v=vs.90).aspx
- http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.90%29.aspx
Usually it's "DEU" or "GERMAN" under Win.
Win下通常是“DEU”或“GERMAN”。
Already mentioned:
已经提到:
Under Linux you can see all locales with the shell command:
在 Linux 下,您可以使用 shell 命令查看所有语言环境:
locale -a
回答by Lauris Kuznecovs
apt-get install -y locales locales-all
回答by Salvi Pascual
This was such pain.
这真是太痛苦了。
I wanted to translate dates to Spanish, and it was working on my localhost but not in the server (Ubuntu 18). It turned out I did not have the correct locale packages.
我想将日期翻译成西班牙语,它在我的本地主机上工作,但不在服务器(Ubuntu 18)上。结果我没有正确的语言环境包。
First I checked the installed packages to ensure they were missing
首先,我检查了已安装的软件包以确保它们丢失
locale -a
Then I installed the Spanish UTF one using the script
然后我使用脚本安装了西班牙 UTF 一个
sudo pkg-reconfigure locales
And then I spent hours hitting my head against the table until I realized YOU NEED TO RESTART APACHE!
然后我花了几个小时把头撞在桌子上,直到我意识到你需要重新启动 APACHE!
sudo service apache2 restart
And that's the end of my story. Hope it helps.
我的故事到此结束。希望能帮助到你。

