Switch a language programatically
In a custom module, you should use a custom LanguageNegotiator and assign it to your LanguageManager, using LanguageManager->setNegotiator();

1) First, you must define a CustomLanguageNegotiator, which overwrites initializeType() method. That method should return a Language object, which you can externally define using a custom setLanguageCode() method:

&lt;?php

namespace Drupal\custom_module\Language;


/**
 * Class responsible for performing language negotiation.
 */
class CustomLanguageNegotiator extends \Drupal\language\LanguageNegotiator {

    var $languageCode = NULL;

    /**
     * {@inheritdoc}
     */
    public function initializeType($type) {
        $language = NULL;
        $method_id = static::METHOD_ID;
        $availableLanguages = $this-&gt;languageManager-&gt;getLanguages();

        if ($this-&gt;languageCode &amp;&amp; isset($availableLanguages[$this-&gt;languageCode])) {
            $language = $availableLanguages[$this-&gt;languageCode];
        }
        else {
            // If no other language was found use the default one.
            $language = $this-&gt;languageManager-&gt;getDefaultLanguage();
        }

        return array($method_id =&gt; $language);
    }

    /**
     * @param string $languageCode
     */
    public function setLanguageCode($languageCode)
    {
        $this-&gt;languageCode = $languageCode;
    }


}


2) Define your CustomLanguageNegotiator as a service, in your custom module service file, "custom_module.services.yml":

custom_module.language_negotiator:
  class: Drupal\custom_module\Language\CustomLanguageNegotiator
  arguments: ['@language_manager', '@plugin.manager.language_negotiation_method', '@config.factory', '@settings', '@request_stack']
  calls:
    - [initLanguageManager]


3) Rebuild your cache and use your CustomLanguageNegotiator like this:

$languageManager = \Drupal::languageManager();
$customLanguageNegotiator = \Drupal::service('custom.language_negotiator');
$languageManager-&gt;setNegotiator($customLanguageNegotiator);


// Set new language by its langcode
$languageManager-&gt;reset(); // Needed to re-run language negotiation
$languageManager-&gt;getNegotiator()-&gt;setLanguageCode('fr'); // 'en', 'es', 'fr', etc

// Run any code, and its output will be displayed in french
// ...

$languageManager-&gt;reset(); 
$languageManager-&gt;getNegotiator()-&gt;setLanguageCode('en');

// Run any code, and its output will be displayed in english
// ...

// Do not forget to set language to its initial value
$languageManager-&gt;reset(); 
$languageManager-&gt;getNegotiator()-&gt;setLanguageCode('XX');