【问题标题】:setlocale and strftime not translating monthsetlocale 和 strftime 不翻译月份
【发布时间】:2022-02-07 05:28:29
【问题描述】:

我的代码:

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

输出类似:

28 August

而不是我的期望:

28 Agost

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

setlocalestrftime 应该是这样工作的吗?

仅供参考:我的本地开发机器是 Windows 7,设置为区域设置:en-PH

【问题讨论】:

    标签: php date localization locale


    【解决方案1】:

    Windows 上的语言环境名称不同。好像默认是英文的。

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

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

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

    【讨论】:

    • @Obay 不要忘记保留'ca_ES' 以便与其他系统兼容
    • 我已经在 debian+nginx 上成功使用了这个:setlocale(LC_TIME, 'ca_ES.UTF-8', 'Catalan_Spain', 'Catalan');
    • 在 Ubuntu 15.10 中遇到同样的问题,通过在 es_ES 中添加“.UTF8”部分解决
    【解决方案2】:

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

    1. SSH 到您的服务器
    2. 检查安装了哪些语言环境:locale -a
    3. 如果您的语言环境不存在,请安装它:sudo locale-gen ca_ES
    4. 更新服务器中的语言环境:sudo update-locale
    5. 重新启动 apache 以使更改生效:sudo service apache2 restart

    就是这样!

    【讨论】:

    • 我使用的是 Ubuntu 18,这种方式对我有用。我将土耳其日期设置为“tr_TR.uft8”。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      apt-get 安装语言环境

      dpkg-重新配置语言环境

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          对于所有像我这样由于服务器限制而仍然无法使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;
          }
          

          【讨论】:

            【解决方案7】:

            我知道这是一个老问题,但我想我还是会做出贡献。

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-03-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多