Chris@17: createUser([], '', TRUE); Chris@17: $this->drupalLogin($user); Chris@17: ConfigurableLanguage::createFromLangcode('es')->save(); Chris@17: Chris@17: // Create a page node type and make it translatable. Chris@17: NodeType::create([ Chris@17: 'type' => 'page', Chris@17: 'name' => t('Page'), Chris@17: ])->save(); Chris@17: Chris@17: $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page'); Chris@17: $config->setDefaultLangcode('en') Chris@17: ->setLanguageAlterable(TRUE) Chris@17: ->save(); Chris@17: Chris@17: // Create a Node with title 'English' and translate it to Spanish. Chris@17: $node = Node::create([ Chris@17: 'type' => 'page', Chris@17: 'title' => 'English', Chris@17: ]); Chris@17: $node->save(); Chris@17: $node->addTranslation('es', ['title' => 'Español']); Chris@17: $node->save(); Chris@17: Chris@17: // Enable both language_interface and language_content language negotiation. Chris@17: \Drupal::getContainer()->get('language_negotiator')->updateConfiguration([ Chris@17: 'language_interface', Chris@17: 'language_content', Chris@17: ]); Chris@17: Chris@17: // Set the preferred language of the user for admin pages to English. Chris@17: $user->set('preferred_admin_langcode', 'en')->save(); Chris@17: Chris@17: // Make sure node edit pages are administration pages. Chris@17: $this->config('node.settings')->set('use_admin_theme', '1')->save(); Chris@17: $this->container->get('router.builder')->rebuild(); Chris@17: Chris@17: // Place a Block with a translatable string on the page. Chris@17: $this->placeBlock('system_powered_by_block', ['region' => 'content']); Chris@17: Chris@17: // Load the Spanish Node page once, to register the translatable string. Chris@17: $this->drupalGet('/es/node/1'); Chris@17: Chris@17: // Translate the Powered by string. Chris@17: /** @var \Drupal\locale\StringStorageInterface $string_storage */ Chris@17: $string_storage = \Drupal::getContainer()->get('locale.storage'); Chris@17: $source = $string_storage->findString(['source' => 'Powered by Drupal']); Chris@17: $string_storage->createTranslation([ Chris@17: 'lid' => $source->lid, Chris@17: 'language' => 'es', Chris@17: 'translation' => 'Funciona con ...', Chris@17: ])->save(); Chris@17: // Invalidate caches so that the new translation will be used. Chris@17: Cache::invalidateTags(['rendered', 'locale']); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test translation with URL and Preferred Admin Language negotiators. Chris@17: * Chris@17: * The interface language uses the preferred language for admin pages of the Chris@17: * user and after that the URL. The Content uses just the URL. Chris@17: */ Chris@17: public function testUrlContentTranslationWithPreferredAdminLanguage() { Chris@17: $assert_session = $this->assertSession(); Chris@17: // Set the interface language to use the preferred administration language Chris@17: // and then the URL. Chris@17: /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */ Chris@17: $language_negotiator = \Drupal::getContainer()->get('language_negotiator'); Chris@17: $language_negotiator->saveConfiguration('language_interface', [ Chris@17: 'language-user-admin' => 1, Chris@17: 'language-url' => 2, Chris@17: 'language-selected' => 3, Chris@17: ]); Chris@17: // Set Content Language Negotiator to use just the URL. Chris@17: $language_negotiator->saveConfiguration('language_content', [ Chris@17: 'language-url' => 4, Chris@17: 'language-selected' => 5, Chris@17: ]); Chris@17: Chris@17: // See if the full view of the node in english is present and the Chris@17: // string in the Powered By Block is in English. Chris@17: $this->drupalGet('/node/1'); Chris@17: $assert_session->pageTextContains('English'); Chris@17: $assert_session->pageTextContains('Powered by'); Chris@17: Chris@17: // Load the spanish node page again and see if both the node and the string Chris@17: // are translated. Chris@17: $this->drupalGet('/es/node/1'); Chris@17: $assert_session->pageTextContains('Español'); Chris@17: $assert_session->pageTextContains('Funciona con'); Chris@17: $assert_session->pageTextNotContains('Powered by'); Chris@17: Chris@17: // Check if the Powered by string is shown in English on an Chris@17: // administration page, and the node content is shown in Spanish. Chris@17: $this->drupalGet('/es/node/1/edit'); Chris@17: $assert_session->pageTextContains('Español'); Chris@17: $assert_session->pageTextContains('Powered by'); Chris@17: $assert_session->pageTextNotContains('Funciona con'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test translation with URL and Session Language Negotiators. Chris@17: */ Chris@17: public function testUrlContentTranslationWithSessionLanguage() { Chris@17: $assert_session = $this->assertSession(); Chris@17: /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */ Chris@17: $language_negotiator = \Drupal::getContainer()->get('language_negotiator'); Chris@17: // Set Interface Language Negotiator to Session. Chris@17: $language_negotiator->saveConfiguration('language_interface', [ Chris@17: 'language-session' => 1, Chris@17: 'language-url' => 2, Chris@17: 'language-selected' => 3, Chris@17: ]); Chris@17: Chris@17: // Set Content Language Negotiator to URL. Chris@17: $language_negotiator->saveConfiguration('language_content', [ Chris@17: 'language-url' => 4, Chris@17: 'language-selected' => 5, Chris@17: ]); Chris@17: Chris@17: // See if the full view of the node in english is present and the Chris@17: // string in the Powered By Block is in English. Chris@17: $this->drupalGet('/node/1'); Chris@17: $assert_session->pageTextContains('English'); Chris@17: $assert_session->pageTextContains('Powered by'); Chris@17: Chris@17: // The language session variable has not been set yet, so Chris@17: // The string should be in Spanish. Chris@17: $this->drupalGet('/es/node/1'); Chris@17: $assert_session->pageTextContains('Español'); Chris@17: $assert_session->pageTextNotContains('Powered by'); Chris@17: $assert_session->pageTextContains('Funciona con'); Chris@17: Chris@17: // Set the session language to Spanish but load the English node page. Chris@17: $this->drupalGet('/node/1', ['query' => ['language' => 'es']]); Chris@17: $assert_session->pageTextContains('English'); Chris@17: $assert_session->pageTextNotContains('Español'); Chris@17: $assert_session->pageTextContains('Funciona con'); Chris@17: $assert_session->pageTextNotContains('Powered by'); Chris@17: Chris@17: // Set the session language to English but load the node page in Spanish. Chris@17: $this->drupalGet('/es/node/1', ['query' => ['language' => 'en']]); Chris@17: $assert_session->pageTextNotContains('English'); Chris@17: $assert_session->pageTextContains('Español'); Chris@17: $assert_session->pageTextNotContains('Funciona con'); Chris@17: $assert_session->pageTextContains('Powered by'); Chris@17: } Chris@17: Chris@17: }