Render a view in a given language in a custom command

I have written a Drupal Console command that creates a huge batch of HTML snippets. This is done by rendering a view programmatically (as in this StackExchange answer)

The task

Now I have to create these snippets in several languages.

This works

If I call the view in my browser I can easily chose the language with the respective path prefix:

http://www.example.com/de/path/to/the/view (german version)

http://www.example.com/en/path/to/the/view (english version)

This does not work

If I try to render the very same view from a Drupal Console command I am not able to set the target language. The rendered HTML is always in the default language.

At first I thought passing the langcode in the view() method should suffice:

// Now render the node as HTML $viewBuilder = $this->entityTypeManager->getViewBuilder('node'); $build       = $viewBuilder->view($node, 'full', $langcode); $html        = $this->renderer->renderPlain($build); 

Then I tried setConfigOverrideLanguage():

$this->languageManager->setConfigOverrideLanguage($language);  // … render the view here …  $this->languageManager->setConfigOverrideLanguage($originalLanguage); 

But this did not work either. So it tried:

$this->configFactory->getEditable('system.site')->set('langcode', $langcode)->save();  // … render the view here …  $this->configFactory->getEditable('system.site')->set('langcode', $originalLangcode)->save(); 

But I did not succeed. I even tried using a custom LanguageNegotiator, but I ran into a fatal error due to the fact that the service ‘language_manager’ does not implement the ConfigurableLanguageManagerInterface, so it is missing the setNegotiator() and getNegotiator() methods.

Once again I am totally stuck and I don’t know what I am missing (most likely just a tiny bit). How in the world is it possible to change the language of my rendered HTML???

Edit 2017-01-05:

I have to admit I am quite desperate atm. I even tried everything I ruled out above again. Guess what, it still does not work. Any ideas? I am so shure that I am missing only a tiny piece. But I don’t have any idea where to search for 🙁

Edit 2017-01-17:

Obviously the console command is not aware of (parts of) the context, even if I set the language. Just to get it done I am considering putting together a request object which I would then have been handled by the kernel. If this should turn out to be too complicated I could still curl the respective pages, but I have a feeling that there should be another way of doing this.

Edit 2017-03-14:

First of all there there has to be a translation (even if the node data is in no way language specific). If you want the complete context you have to use the Kernel’s handle method with a sub request.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Render a view in a given language in a custom command

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.