问题
We're teaching a class in chinese (Mandarin). For our students, it would be easier (and make more sense to them) if the error output running php (on the commandline) was localizable to Mandarin. Is there a way to configure our php setup to support this (i.e. edit a php.ini file)?
回答1:
I have downloaded the PHP source code and searched how the source handles it's errors. After going through a couple of (randomly picked) files and found these kinds of lines all over the place:
// warning for the sleep() function
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of seconds must be greater than or equal to 0");
// warning for the constant() function
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't find constant %s", const_name);
// warning for the mysql_free_result() function
php_error_docref("function.mysql-free-result" TSRMLS_CC, E_WARNING, "%lu result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query()", MySG(result_allocated));
// error for the mysql-escape-string function()
php_error_docref("function.mysql-escape-string" TSRMLS_CC, E_DEPRECATED, "This function is deprecated; use mysql_real_escape_string() instead.");
This goes on and on forever which makes me conclude that errors are hardcoded and thus not available in any other language...
Too bad :(
After hours of searching (5 hours and 45 minutes to be exact) - I have found a couple of solutions that probably aren't worthwhile but who am I to judge.
Translations are often saved in files with the .po extension. These files contain a bunch of original strings and the translated strings. This means that you have to translate every string each, manually. You can create files with PoEdit.
Then you need to parse these files. You can build a PHP version that has the Gettext module installed. Alternatively, the Zend Framework is able to parse these files as well.
Clearly, you don't want to translate every possible string manually. Last option is using a translator service such as Google Translate or Microsoft Translate.
In all cases, you have to replace the existing error string with the new error string. There's a native PHP function called set_error_handler(). This allows you to set a custom error handling function. If you want to translate the existing error string you should do it there. Here's a set up that you could use (I haven't tested this thoroughly):
<?php
set_error_handler('error_handler');
function error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
// Get the correct string for the error type
switch($errno){
case E_ERROR:
$type = 'Error';
break;
case E_WARNING:
$type = 'Warning';
break;
case E_PARSE:
$type = 'Parse Error';
break;
case E_NOTICE:
$type = 'Notice';
break;
case E_CORE_ERROR:
$type = 'Core Error';
break;
case E_CORE_WARNING:
$type = 'Core Warning';
break;
case E_COMPILE_ERROR:
$type = 'Compile Error';
break;
case E_COMPILE_WARNING:
$type = 'Compile Warning';
break;
case E_USER_ERROR:
$type = 'User Error';
break;
case E_USER_WARNING:
$type = 'User Warning';
break;
case E_USER_NOTICE:
$type = 'User Notice';
break;
case E_STRICT:
$type = 'Strict Notice';
break;
case E_RECOVERABLE_ERROR:
$type = 'Recoverable Error';
break;
default:
$type = 'Unknown error (' . $errno . ')';
break;
}
// translate text
$type = error_handler_translate($type);
$str = error_handler_translate($errstr);
$in = error_handler_translate('in');
$line = error_handler_translate('on line');
// echo the error in "default" PHP format
echo '<br /><b>'. $type .':</b> ' . $str . ' '. $in .' <b>' . $errfile . '</b> '. $line .' <b>' . $errline . '</b><br />';
// ignore the original PHP error handling
return true;
}
function error_handler_translate($str) {
// translate $str here
return $str;
}
?>
All that remains is including this code to your script and you should be all good!
回答2:
you can try, this may work
/* Set locale to Dutch */
setlocale(LC_ALL, 'nl_NL');
来源:https://stackoverflow.com/questions/11556375/is-there-a-way-to-localize-phps-error-output