Part 1:
Let’s say I do something which throws an exception like:
function do_something($somepar)
{
if (inexistent())
return TRUE;
else
return FALSE;
}
if (do_something("SMART"))
echo "Everything went right";
else
echo "Something failed";
I got
Fatal error: Call to undefined function inexistent() in xyz.php on line 123
Is it possible to localize this message? I hoped
setlocale(LC_ALL,'it_IT');
could do that but it didn’t.
Part 2:
which is your recommendation to handle localized errors. Let’s say I want to create a general function to public it on the web and can be used by people who want it to use their own language.
function do_something($somepar)
{
if (whatever())
{
return TRUE;
}
else
{
$error_message = localizeThis("How to translate this?");
trigger_error($error_message, E_USER_ERROR);
return FALSE;
}
}
if (do_something("SMART"))
echo "Everything went right";
else
echo "Something failed";
Also how to set the correct output language?
You need to differentiate between error handling, debugging/error messages and user-visible output. The standard error messages triggered by PHP are not localized by PHP. They are exclusively for the developer to see to help the developer debug the application, and the developer is expected to speak English. The rest of the PHP language is in English anyway.
Messages triggered by
trigger_erroror by thrown exceptions are not meant for end-user consumption, as most end users won’t understand them anyway. You need to catch those errors internally and, if necessary, notify the user with any sort of localization system that something bad happened. The technical error messages can also contain sensitive information that should not be disclosed to the general public. So:and: