これは私のコードです:
$db = new PDO('mysql:host=192.168.1.1;dbname=mydb');
「'192.168.1.1' で MySQL サーバーに接続できません」という例外メッセージの言語を変更することはできますか?
setlocale(LC_ALL, 'ru_RU.utf8') で php ロケールを変更すると、メッセージが英語であるにもかかわらず、日付に影響します。
class Cp1251ErrorExeption extends ErrorException {
public function getUtfMessage() {
return iconv('cp1251', 'utf-8', $this->getMessage());
}
function handleError($errno, $errstr, $errfile, $errline, array $errcontext){
if (0 === error_reporting())
return false;
throw new self($errstr, 0, $errno, $errfile, $errline);
}
}
try {
try {
set_error_handler('Cp1251ErrorExeption::handleError');
$db = new PDO('mysql:host=192.168.1.1;dbname=mydb');
} catch (PDOException $e) {
throw new Cp1251ErrorExeption($e->getMessage());
}
} catch (Cp1251ErrorExeption $e) {
echo $e->getUtfMessage();
}
restore_error_handler();