Ubuntu Php Locale ru_RU not working

旧城冷巷雨未停 提交于 2019-12-11 14:25:50

问题


Code:

<?php

      setlocale(LC_ALL, 'ru_RU');

      $date_ru = strftime('%d %B %Y', strtotime('2018-01-31'));

      echo($date_ru);

?>

Outputs:

1) Local Server: 31 января 2018

2) Remote Server: 31 ������ 2018

(exactly those strange symbols instead of month-name).

Settings:

I've checked locales installed with local -a and run php -i to get info about both servers.

Local Server:

  • ru_RU
  • ru_RU.CP1251
  • ru_RU.CP866
  • ru_RU.ISO8859-5
  • ru_RU.KOI8-R
  • ru_RU.UTF-8

... and all the rest possible locales in all languages...

  • $_SERVER['LANG'] => ru_RU.UTF-8

  • PHP Version => 7.1.12

Remote Server:

  • C
  • C.UTF-8
  • en_US.utf8
  • POSIX
  • ru_RU
  • ru_RU.iso88595
  • ru_RU.utf8

  • $_SERVER['LANG'] => ru_RU.UTF-8

  • PHP Version => 7.0.22-0ubuntu0.16.04.1

How can I get output on the remote server to be the same as on the local one ?


Update: If I change to setlocale(LC_ALL, 'ru_RU.UTF-8');

Local Server: 31 января 2018 (as before change)

Remote Server: 31 Январь 2018

Although I don't get strange symbols here, the result is not satisfactory. the last letter here is the most important. 'января' is genetive case (what I want) when 'Январь' is nominative (not what I need).


回答1:


The problem was that Ubuntu's server package used different locales than those I had in my local environment. I couldn't import locales from my local to Ubuntu (wonder if it's possible at all), so i just wrote a workaround (simple function to convert month-names like 'Январь' to 'января' etc..)

function convert($str) {

      $rus = array('/январь/ui', '/февраль/ui', '/март/ui', '/апрель/ui', '/май/ui', '/июнь/ui', '/июль/ui', '/август/ui', '/сентябрь/ui', '/октябрь/ui', '/ноябрь/ui', '/декабрь/ui');
      $lat = array('января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря');
      // return str_ireplace($rus, $lat, $str);
      return preg_replace($rus, $lat, $str);

}


来源:https://stackoverflow.com/questions/48346246/ubuntu-php-locale-ru-ru-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!