I'm writing a test for something that returns a Url from an entity. This should be language-aware, so the test needs to change the current language.

I started off writing a Kernel test for this, because they're faster, but I can't figure out how to make the test code change the current language:

  public static $modules = [

  protected function setUp() {


    $this->languages['de'] = ConfigurableLanguage::createFromLangcode('de');

    $config = $this->config('language.negotiation');
    $config->set('url.prefixes', [
      'en' => 'en',
      'de' => 'de',

    $this->languageManager = $this->container->get('language_manager');


  public function testCurrentLanguageChange() {
    // Change the site's current language.




In the test code above, both dump() calls output the 'English' language object.

1 Answer 1


Looks like I needed to call:


No, wait, that doesn't seem to be the whole story.... ARGH.


For the entity to be treated as translatable and so return the right URL, I also needed to set the bundle as translatable:

      'target_entity_type_id' => 'node',
      'target_bundle' => 'page',
    $this->container->get('content_translation.manager')->setEnabled('node', 'page', TRUE);

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.