Commit f32e302e authored by webchick's avatar webchick

Issue #2084057 by dawehner: Fixed Translate _title on routes.

parent b88608eb
......@@ -358,7 +358,7 @@ services:
- { name: event_subscriber }
controller.page:
class: Drupal\Core\Controller\HtmlPageController
arguments: ['@http_kernel', '@controller_resolver']
arguments: ['@http_kernel', '@controller_resolver', '@string_translation']
controller.dialog:
class: Drupal\Core\Controller\DialogController
arguments: ['@http_kernel']
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Controller;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
......@@ -30,6 +31,13 @@ class HtmlPageController {
*/
protected $controllerResolver;
/**
* The translation manager service.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* Constructs a new HtmlPageController.
*
......@@ -37,9 +45,10 @@ class HtmlPageController {
* @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
* The controller resolver.
*/
public function __construct(HttpKernelInterface $kernel, ControllerResolverInterface $controller_resolver) {
public function __construct(HttpKernelInterface $kernel, ControllerResolverInterface $controller_resolver, TranslationInterface $translation_manager) {
$this->httpKernel = $kernel;
$this->controllerResolver = $controller_resolver;
$this->translationManager = $translation_manager;
}
/**
......@@ -67,11 +76,20 @@ public function content(Request $request, $_content) {
}
// If no title was returned fall back to one defined in the route.
if (!isset($page_content['#title']) && $request->attributes->has('_title')) {
$page_content['#title'] = $request->attributes->get('_title');
$page_content['#title'] = $this->t($request->attributes->get('_title'));
}
$response = new Response(drupal_render_page($page_content));
return $response;
}
/**
* Translates a string to the current language or to a given language.
*
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->translationManager->translate($string, $args, $options);
}
}
......@@ -150,6 +150,18 @@ public function testRenderTitle() {
$this->assertTitle('Test dynamic title | Drupal');
$result = $this->xpath('//h1');
$this->assertEqual('Test dynamic title', (string) $result[0]);
// Set some custom translated strings.
variable_set('locale_custom_strings_en', array('' => array(
'Static title' => 'Static title translated'
)));
// Ensure that the title got translated.
$this->drupalGet('test-page-static-title');
$this->assertTitle('Static title translated | Drupal');
$result = $this->xpath('//h1');
$this->assertEqual('Static title translated', (string) $result[0]);
}
}
......@@ -26,4 +26,17 @@ public function renderTitle() {
return $build;
}
/**
* Renders a page.
*
* @return array
* A render array as expected by drupal_render().
*/
public function staticTitle() {
$build = array();
$build['#markup'] = 'Hello Drupal';
return $build;
}
}
......@@ -5,6 +5,14 @@ test_page_render_title:
requirements:
_access: 'TRUE'
test_page_static_title:
pattern: "/test-page-static-title"
defaults:
_content: 'Drupal\test_page_test\Controller\Test::staticTitle'
_title: 'Static title'
requirements:
_access: 'TRUE'
admin_test_page_render_title:
pattern: "/admin/test-render-title"
defaults:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment