php setlocale 和 strftime 不翻译月份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12565981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:49:02  来源:igfitidea点击:

setlocale and strftime not translating month

phpdatelocalizationlocale

提问by Obay

My code:

我的代码:

setlocale(LC_TIME, 'ca_ES');
echo strftime("%#d %B", strtotime($ticket->date_created));

outputs something like:

输出类似:

28 August

instead of my expectation:

而不是我的期望:

28 Agost

I expect "Agost" because that is Catalan language (set through setlocale()).

我期待“Agost”,因为那是加泰罗尼亚语(通过 设置setlocale())。

Is this how setlocaleand strftimeis supposed to work?

这是如何setlocale并且strftime应该如何工作?

FYI: My local development machine is Windows 7, set to locale: en-PH

仅供参考:我的本地开发机器是 Windows 7,设置为语言环境:en-PH

回答by Sherbrow

The locale names are different on Windows. It looks like it's defaulting to English.

Windows 上的区域设置名称不同。看起来它默认为英语。

Try the full length name (you can try several and it will pick the first found):

尝试全长名称(您可以尝试多个,它会选择第一个找到的):

setlocale(LC_TIME, 'ca_ES', 'Catalan_Spain', 'Catalan');

You may look at this table, which may be helpful : http://docs.moodle.org/dev/Table_of_locales

您可以查看此表,这可能会有所帮助:http: //docs.moodle.org/dev/Table_of_locales

回答by Thomas Venturini

On Ubuntu 14.04 setlocale(LC_TIME, 'de_DE.UTF8');did the trick for me to get a german month format/name :)

在 Ubuntu 14.04 上setlocale(LC_TIME, 'de_DE.UTF8');帮我获得了德国月份格式/名称:)

回答by JvdBerg

strftimegives localized versions of date formats. If you do get unexpected results, most probably the localized version you are expecting is not on your system.

strftime提供日期格式的本地化版本。如果您确实得到了意外结果,很可能您期望的本地化版本不在您的系统上。

I have no experience with windows, but on my Debian Linux server, I had to install the localized strings I wanted.

我没有使用 Windows 的经验,但是在我的 Debian Linux 服务器上,我必须安装我想要的本地化字符串。

回答by Carl

On Debian 9 setlocale(LC_TIME, 'fr_FR.UTF8');did the job for me to get nicely french formatted day/month display. Make sure to have the locales installed with the following sequence:

在 Debian 9 上setlocale(LC_TIME, 'fr_FR.UTF8');,我完成了很好的法语格式的日/月显示。确保按以下顺序安装语言环境:

apt-get install locales

dpkg-reconfigure locales

apt-get 安装语言环境

dpkg-重新配置语言环境

回答by Mario Jaramillo

You need to install the locale you want in your server.

您需要在服务器中安装所需的语言环境。

  1. SSH into your server
  2. Check which locales are installed: locale -a
  3. if your locale is not there, install it: sudo locale-gen ca_ES
  4. Update the locales in your server: sudo update-locale
  5. Restart apache for the changes to take effect: sudo service apache2 restart
  1. SSH 到您的服务器
  2. 检查安装了哪些语言环境: locale -a
  3. 如果您的语言环境不存在,请安装它: sudo locale-gen ca_ES
  4. 更新服务器中的语言环境: sudo update-locale
  5. 重新启动 apache 以使更改生效: sudo service apache2 restart

That is it!

这就对了!

回答by Zeth

I know that this is an old question, but I thought I'd contribute anyway.

我知道这是一个老问题,但我想无论如何我都会做出贡献。

If you can SSH into the server that you're working on, then write: locale -aand then you can see your available locales. If you're language isn't listed, then it need to be installed.

如果您可以通过 SSH 连接到您正在使用的服务器,请编写:locale -a然后您可以看到可用的语言环境。如果您的语言未列出,则需要安装它。

For me, I saw the one I was looking for: nb_NO.utf8in the list, so I wrote it exactly how it was written: setlocale( LC_ALL, 'nb_NO.utf8' )and that worked for me.

对我来说,我nb_NO.utf8在列表中看到了我正在寻找的那个,所以我完全按照它的写法来写:setlocale( LC_ALL, 'nb_NO.utf8' )这对我有用。

回答by user700119

For all those, like me, who still cannot make strftimefunction due to server restrictions... Not perfect but it works.

对于所有像我一样strftime由于服务器限制而仍然无法正常工作的人......不完美但它有效。

$months[1] = "gennaio";
$months[2] = "febbraio";
$months[3] = "marzo";
$months[4] = "aprile";
$months[5] = "maggio";
$months[6] = "giugno";
$months[7] = "luglio";
$months[8] = "agosto";
$months[9] = "settembre";
$months[10] = "ottobre";
$months[11] = "novembre";
$months[12] = "dicembre";

// giorni
$weekdays[0] = "domenica";
$weekdays[1] = "lunedì";
$weekdays[2] = "martedì";
$weekdays[3] = "mercoledì";
$weekdays[4] = "giovedì";
$weekdays[5] = "venerdì";
$weekdays[6] = "sabato";

function strftimeIta($format, $timestamp){

    global $weekdays, $months;

    preg_match_all('/%([a-zA-Z])/', $format, $results);

    $originals = $results[0];
    $factors = $results[1];


    foreach($factors as $key=>$factor){
        switch($factor){
            case 'a':
                /*** Abbreviated textual representation of the day ***/
                $n = date('w', $timestamp); // number of the weekday (0 for sunday, 6 for saturday);
                $replace = ucfirst($weekdays[$n]);
                $replace = substr($replace, 0, 3);
                break;
            case 'A':
                /*** Full textual representation of the day ***/
                $n = date('w', $timestamp); // number of the weekday (0 for sunday, 6 for saturday);
                $replace = ucfirst($weekdays[$n]);
                break;
            case 'h':
            case 'b':
                /*** Abbreviated month name ***/
                $n = date('n', $timestamp); // Numeric representation of a month, without leading zeros
                $replace = ucfirst($months[$n]);
                $replace = substr($replace, 0, 3);
                break;
            case 'B':
                /*** Full month name ***/
                $n = date('n', $timestamp); // Numeric representation of a month, without leading zeros
                $replace = ucfirst($months[$n]);
                break;
            default:
                /*** Use standard strftime function ***/
                $replace = strftime("%".$factor, $timestamp);
                break;
        }
        $search = $originals[$key];
        $format = str_replace($search, $replace, $format);
    }
    return $format;
}