diff --git a/core/core.services.yml b/core/core.services.yml index 868d4ab..4fab422 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -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'] diff --git a/core/lib/Drupal/Core/Controller/HtmlPageController.php b/core/lib/Drupal/Core/Controller/HtmlPageController.php index d87b3ed..a0485b5 100644 --- a/core/lib/Drupal/Core/Controller/HtmlPageController.php +++ b/core/lib/Drupal/Core/Controller/HtmlPageController.php @@ -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; @@ -31,15 +32,23 @@ class HtmlPageController { protected $controllerResolver; /** + * The translation manager service. + * + * @var \Drupal\Core\StringTranslation\TranslationInterface + */ + protected $translationManager; + + /** * Constructs a new HtmlPageController. * * @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel * @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); + } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php index d5fead5..35621a7 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/PageTitleTest.php @@ -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]); } } diff --git a/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php b/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php index 850ca2d..c0c10d3 100644 --- a/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php +++ b/core/modules/system/tests/modules/test_page_test/lib/Drupal/test_page_test/Controller/Test.php @@ -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; + } + } diff --git a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml index 2de2a4d..3a03389 100644 --- a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml +++ b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml @@ -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: