Skip to content

Instantly share code, notes, and snippets.

@euskadi31
Last active May 29, 2017 09:17
Show Gist options
  • Save euskadi31/684deb09b5da66530eff to your computer and use it in GitHub Desktop.
Save euskadi31/684deb09b5da66530eff to your computer and use it in GitHub Desktop.
Add translation support of error page in Symfony2
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Une erreur est survenue: {{ status_text }}</title>
</head>
<body>
<h1>Oops! Une erreur est survenue</h1>
<h2>Le serveur a renvoyé une "{{ status_code }} {{ status_text }}".</h2>
<div>
Quelque chose est cassé. Se il vous plaît laissez-nous savoir ce que vous faisiez lorsque cette erreur se est produite.
Nous allons y remédier dès que possible. Désolé pour la gêne occasionnée.
</div>
</body>
</html>
<?php
namespace Acme\Bundle\FrontBundle\Controller;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController AS Controller;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\HttpFoundation\Request;
class ExceptionController extends Controller
{
/**
* @param Request $request
* @param string $format
* @param int $code An HTTP response status code
* @param bool $showException
*
* @return TemplateReferenceInterface
*/
protected function findTemplate(Request $request, $format, $code, $showException)
{
$locale = $request->getLocale();
$name = $showException ? 'exception' : 'error';
if ($showException && 'html' == $format) {
$name = 'exception_full';
}
$templates = array();
// For error pages, add names of template for the specific HTTP status code and format
if (!$showException) {
if (!empty($locale)) {
$templates[] = $name.$code.'.'.$locale;
}
$templates[] = $name.$code;
}
if (!empty($locale)) {
$templates[] = $name.'.'.$locale;
}
$templates[] = $name;
// try to find a template for the given name
foreach ($templates as $templateName) {
$template = new TemplateReference('TwigBundle', 'Exception', $templateName, $format, 'twig');
if ($this->templateExists($template)) {
return $template;
}
}
// default to a generic HTML exception
$request->setRequestFormat('html');
return new TemplateReference('TwigBundle', 'Exception', $showException ? 'exception_full' : $name, 'html', 'twig');
}
}
parameters:
twig.controller.exception.class: Acme\Bundle\FrontBundle\Controller\ExceptionController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment