翻译 PHP 异常消息


Translating PHP Exceptions Messages

我正在使用Symfony2文件组件,它抛出了一个名为"FileException"的异常。问题是异常消息是根据错误号动态生成的,如下所示:

throw new FileException($this->getErrorMessage($this->getError()))

最后,我可以得到 6 条不同的消息,因此似乎无法翻译或在 catchs 块中显示自定义消息。我想要这样的东西:

    catch (FileRequiredException $e)
    {
        echo $e->getMessage();
    }
    catch (FileSizeException $e)
    {
        echo $e->getMessage();
    }
    catch (FileExistsException $e)
    {
        echo $e->getMessage();
    }

有没有人有翻译那些动态生成的异常消息的解决方案?

Try/Catch 块在 Laravel 上不起作用,您必须使用其中央错误处理程序。您可以通过添加类似以下内容来做到这一点:

App::error(function(FileRequiredException $e)
{
    return View::make('yourErrorView')->with('message', $e->getMessage());
});

到您的app/filters.php文件。

相关文章: