I have defined a route as follow:

  path: '/node/{node}/summary'
        type: 'entity:node'
    _controller: '\Drupal\mymodule\Controller\NodeSummaryController::content'
    _title: 'Summary'
    node: \d+
    _entity_access: 'node.view'

Then I have created the following controller:

class NodeSummaryController {

  public function content(Node $node = NULL) {
    $view_builder = Drupal::entityTypeManager()
    $summary = $view_builder->view($node, 'summary');
    $render = Drupal::service('renderer')
    $response = new Response($render);
    return $response;

If I visit the page /nl/node/xxx/summary everything is fine, my node's summary is displayed in dutch

But if I try to get this content with a sub-request, I get the result in the default language of my site, not in dutch

  $kernel = \Drupal::service('http_kernel.basic');
  $sub_request = \Symfony\Component\HttpFoundation\Request::create("/nl/node/xxx/summary");
  $response = $kernel->handle($sub_request, \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST);
  $html = $response->getContent();
  dpm($html); // ERROR THIS IS NOT IN DUTCH
  • How is language negotiation handled on your site? Is it either URL or language prefix? If so, you'll need to pass the path/URL for that language, to get content in that language.
    – Jaypan
    Commented Mar 8, 2023 at 14:49
  • This is URL: Language from the URL (Path prefix or domain). As you can see in $sub_request = \Symfony\Component\HttpFoundation\Request::create("/nl/node/xxx/summary"); I am passing the language code. Note that visiting this url give me to good language. Only the subrequest failed
    – Baud
    Commented Mar 8, 2023 at 14:57

1 Answer 1


I was a bit overly optimistic in the previous question suggesting a sub-request. The language manager service doesn't store data in such a way that it can run within a sub-request scope. So it seems you need to do the same as in the previous question, only that now the interface translation works without a custom negotiator by resetting the language manager (and also the string translation service, see the comment) before running the sub-request:

$language_manager = \Drupal::languageManager();

$langcode = 'nl';
$language = $language_manager->getLanguage($langcode);
$original_language = $language_manager->getConfigOverrideLanguage();

$kernel = \Drupal::service('http_kernel.basic');
$sub_request = \Symfony\Component\HttpFoundation\Request::create('/' . $langcode . '/node/1/summary');
$response = $kernel->handle($sub_request, \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST);

$current_language = $language_manager->getCurrentLanguage();

$html = $response->getContent(); 
  • Many thanks for your help! This is working except for the computed fields...like the other question : drupal.stackexchange.com/a/314993/61398 I will create manually the text like the ones produced by this controller. I'm giving up
    – Baud
    Commented Mar 8, 2023 at 17:36
  • I've found some time to debug, the reason t() doesn't work is a core issue that the service string_translation is initialized once with the language of the main request and not updated when a different language is negotiated. I've added another workaround to the code. I think now it's really translating everything of the rendered node.
    – 4uk4
    Commented Mar 10, 2023 at 9:05
  • Wonderful it is now translating the computed fields as well! Many thanks for your hard work. Don't you think I should add a bug/new feature in the Drupal queue to cache the fields definitions by language? [If this is the original problem... ]
    – Baud
    Commented Mar 10, 2023 at 10:18
  • We solved this already in the previous question. This topic here is more general, to translate everything in the sub-request, translated strings in code, config, or content. Your computed fields are code, the other fields config. A lot of core issues need to be solved. To start with, why is the current language stored in string translation. They just could get it from the language manager with the same efficiency, this service has a static cache as well. The same is for config. When all depends on the language manager it needs to get a stacked storage to store per request.
    – 4uk4
    Commented Mar 10, 2023 at 10:38
  • ok... it is a little bit over my head... Anyway it is working nicely.
    – Baud
    Commented Mar 10, 2023 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.